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 Data.Char (toUpper) | |
| up :: [Char] -> [Char] | |
| up s = [toUpper c | c <- s] |
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
| boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x] | |
| boomBangs [7..13] -- ["BOOM!","BOOM!","BANG!","BANG!"] |
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
| def even(from: Int, to: Int): List[Int] = for (i <- List.range(from, to) if i % 2 == 0) yield i | |
| Console.println(even(0, 20)) // List(0, 2, 4, 6, 8, 10, 12, 14, 16, 18) | |
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
| for (i <- Iterator.range(0, 20); | |
| j <- Iterator.range(i + 1, 20) if i + j == 32) | |
| println("(" + i + ", " + j + ")") | |
| // (13, 19) | |
| // (14, 18) | |
| // (15, 17) |
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
| for( x <- ( 1 to 5 ) ) yield x*x // scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25) |
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
| Just 5 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Just 6 | |
| Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Nothing | |
| Nothing >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Nothing |
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
| def funcMonadic[A](f1: () => A) = new { | |
| def andThen28[B](f2: () => B) = { f1() ; f2() } | |
| } | |
| def monadic[A](f1: () => A) = new { | |
| def ->[B](f2: () => B) = { f1() ; f2() } | |
| } | |
| def bigMonadicPipe() = { | |
| monadic( () => print("A") ) -> ( () => print("B") ) -> ( () => print("C") )() |
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
| fmap (++"!") (Just "wisdom") -- Just "wisdom!" | |
| fmap (++"!") Nothing -- Nothing | |
| applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b | |
| applyMaybe Nothing f = Nothing | |
| applyMaybe (Just x) f = f x | |
| Just "smile" `applyMaybe` \x -> Just (x ++ " :)") -- Just "smile :)" |
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
| class Monad m where | |
| return :: a -> m a | |
| (>>=) :: m a -> (a -> m b) -> m b | |
| (>>) :: m a -> m b -> m b | |
| x >> y = x >>= \_ -> y | |
| fail :: String -> m a | |
| fail msg = error msg |
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
| instance Monad Maybe where | |
| return x = Just x | |
| Nothing >>= f = Nothing | |
| Just x >>= f = f x | |
| fail _ = Nothing |