Skip to content

Instantly share code, notes, and snippets.

@rburgosnavas
Last active August 29, 2015 13:57
Show Gist options
  • Save rburgosnavas/9839129 to your computer and use it in GitHub Desktop.
Save rburgosnavas/9839129 to your computer and use it in GitHub Desktop.
folds
-- this
foldr (\x xs -> (x * 10):xs) [] $ foldr (\x xs -> if x `mod` 2 == 0 then x:xs else xs) [] $ foldr (\x xs -> if x > 3 then x:xs else xs) [] [5,9,8,3,6,7,1,5,2]
-- that
[x * 10 | x <- [5,9,8,3,6,7,1,5,2], x > 3, x `mod` 2 == 0]
-- or
foldr (\x xs -> (x * 10):xs) [] $ filter (\x -> x `mod` 2 == 0) $ filter (> 3) [5,9,8,3,6,7,1,5,2]
// this
List(5,9,8,3,6,7,1,5,2).foldRight(Nil:List[Int])((i, xs) => if (i > 3) i::xs else xs)
.foldRight(Nil:List[Int])((i, xs) => if (i % 2 == 0) i::xs else xs)
.foldRight(Nil:List[Int])((i, xs) => (i * 10)::xs)
// that
for {
i <- List(5,9,8,3,6,7,1,5,2)
if (i > 3)
if (i % 2 == 0)
} yield (i * 10)
// or
List(5,9,8,3,6,7,1,5,2).filter(_ > 3)
.filter(_ % 2 == 0)
.foldRight(Nil:List[Int])((i, xs) => (i * 10)::xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment