Created
June 17, 2014 07:42
-
-
Save blouerat/b32a4d0fb3735a380f2b to your computer and use it in GitHub Desktop.
1HaskellADay: collectReverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative | |
{- We still have yesterday's room | |
-} | |
data Room a = Room {doors :: (Bool, Bool), content :: a} | |
deriving (Eq) | |
{- | Given a list of rooms, we want to go as far as we can and collect the elements | |
in he room, **starting from the last room**. | |
Examples: | |
>>> collectReverse [Room (True, True) 1, Room (True, False) 2, Room (True, False) 3] | |
[] | |
>>> collectReverse [Room (False, True) 1, Room (True, False) 2, Room (True, True) 3] | |
[3] | |
>>> collectReverse [Room (True, True) 1, Room (False, True) 2, Room (True, True) 3] | |
[3,2] | |
-} | |
collectWith :: ((Bool, Bool) -> Bool) -> [Room a] -> [a] | |
collectWith p = map content . uncurry (++) . (fmap $ filter (p . doors) . take 1) . span (uncurry (&&) . doors) | |
collect :: [Room a] -> [a] | |
collect = collectWith fst | |
collectReverse :: [Room a] -> [a] | |
collectReverse = collectWith snd . reverse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment