forked from twitter/summingbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request twitter#136 from ccsevers/add_foldM
Adding foldM to Monad and a test
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
26 changes: 26 additions & 0 deletions
26
algebird-test/src/test/scala/com/twitter/algebird/MonadFoldMTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.twitter.algebird | ||
|
||
import org.specs._ | ||
|
||
class MonadFoldMTest extends Specification { | ||
noDetailedDiffs() | ||
|
||
def binSmalls(x: Int, y: Int) : Option[Int] = if(y > 9) None else Some(x+y) | ||
"A monad foldM" should { | ||
"fold correctly" in { | ||
|
||
// nice easy example from Learn You a Haskell | ||
|
||
val first = Monad.foldM(0,List(2,8,3,1))(binSmalls) | ||
first must be_==(Some(14)) | ||
def binSmalls2(x: Int, y: String) : Option[Int] = if(y == "11") None else Some(x+y.toInt) | ||
|
||
val second = Monad.foldM(0, List("2","11","3","1"))(binSmalls2) | ||
second must be_==(None) | ||
} | ||
"handle an empty list" in { | ||
val third = Monad.foldM(0,List.empty)(binSmalls) | ||
third must be_==(Some(0)) | ||
} | ||
} | ||
} |