Last active
December 13, 2015 22:18
-
-
Save sullivan-/4983151 to your computer and use it in GitHub Desktop.
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
def bothGrandfathersFlatMap(p: Person): Maybe[(Person, Person)] = | |
p.mother flatMap { m => | |
m.father flatMap { fm => | |
p.father flatMap { f => | |
f.father flatMap { ff => | |
Just(fm, ff) | |
} | |
} | |
} | |
} | |
def bothGrandfathersNoFlatMap(p: Person): Maybe[(Person, Person)] = | |
(p.mother, p.father) match { | |
case (Just(m), Just(f)) => | |
(m.father, f.father) match { | |
case (Just(fm), Just(ff)) => Just((fm, ff)) | |
case _ => MaybeNot | |
} | |
case _ => MaybeNot | |
} | |
def assertBothGrandfathers( | |
bothGrandfathers1: Person => Maybe[(Person, Person)], | |
bothGrandfathers2: Person => Maybe[(Person, Person)]) = | |
Person.persons foreach { p => | |
assert(bothGrandfathers1(p) == bothGrandfathers2(p)) | |
} | |
assertBothGrandfathers(bothGrandfathersFlatMap, bothGrandfathersNoFlatMap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment