Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active December 24, 2015 14:07
Show Gist options
  • Save ElectricCoffee/db780f0045b4b4308bbc to your computer and use it in GitHub Desktop.
Save ElectricCoffee/db780f0045b4b4308bbc to your computer and use it in GitHub Desktop.
File I'll be dumping useful functions into when I come across them
module ECUtils where
-- applies a function f to a string
-- the string gets picked apart, modified, then stitched back together
mapWords :: (String -> String) -> String -> String
mapWords f = unwords . map f . words
-- port of Scala's function of the same name,
-- returns the default value 'e' if Nothing
getOrElse :: Maybe a -> a -> a
getOrElse Nothing e = e
getOrElse (Just x) _ = x
-- same as uncurry, but works on functions taking 3 arguments
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f = \(a, b, c) -> f a b c
(<||) = (.) -- alternate syntax for .
(||>) = flip (<||)-- composes functions left to right, reads as "then"
(<|) = ($) -- alternate syntax for $
(|>) = flip (<|) -- applies functions in reverse
-- examples
mnub = words ||> map nub ||> unwords -- words then map nub then unwords
res = "hello there" -- res = "reht oleh"
|> mnub
|> reverse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment