Skip to content

Instantly share code, notes, and snippets.

@mstksg
Created June 6, 2018 07:34
Show Gist options
  • 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