Skip to content

Instantly share code, notes, and snippets.

@derekmahar
Created March 6, 2014 14:45
Show Gist options
  • Save derekmahar/9391282 to your computer and use it in GitHub Desktop.
Save derekmahar/9391282 to your computer and use it in GitHub Desktop.
Custom function definitions for Haskell AWK (Hawk). Store in .hawk/prelude.gs. See https://github.com/gelisam/hawk for details.
{- .hawk/prelude.hs -}
{-# LANGUAGE ExtendedDefaultRules, OverloadedStrings #-}
import Prelude
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Search as S
import qualified Data.List as L
replace :: String -> String -> BL.ByteString -> BL.ByteString
replace search replacement input = S.replace (B.pack search) (B.pack replacement) input
-- See https://github.com/gelisam/hawk/blob/master/doc/conversions/prelude.hs
to :: (Read a) => B.ByteString -> a
to = read . B.unpack
toBool :: B.ByteString -> Bool
toBool = to
toDouble :: B.ByteString -> Double
toDouble = to
toFloat :: B.ByteString -> Float
toFloat = to
toInt :: B.ByteString -> Int
toInt = to
toString :: B.ByteString -> String
toString = show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment