Skip to content

Instantly share code, notes, and snippets.

@sullivan-
Last active December 13, 2015 22:18
Show Gist options
  • Save sullivan-/4983151 to your computer and use it in GitHub Desktop.
Save sullivan-/4983151 to your computer and use it in GitHub Desktop.
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