Skip to content

Instantly share code, notes, and snippets.

@mstksg
Created June 6, 2018 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstksg/89fcb48f4b5c5b64f981c4cd3b0f37e4 to your computer and use it in GitHub Desktop.
Save mstksg/89fcb48f4b5c5b64f981c4cd3b0f37e4 to your computer and use it in GitHub Desktop.
Init and Last
import Data.Bifunctor (first)
import Data.List (foldl')
type Diff a = [a] -> [a]
match :: [a] -> Either () ([a], a)
match = (fmap . first) ($[]) -- extract the Diff list
. foldl' go (Left ())
where
go :: Either () (Diff a, a)
-> a
-> Either () (Diff a, a)
go (Left _ ) x = Right (id , x)
go (Right (ys, y)) x = Right (ys . (y:), x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment