Functional composition of both success and error paths - scala

TLDR: I'm looking for functional programming patterns for composing both the success and failure paths of recursive, failable functions.
Scastie link for the example code: https://scastie.scala-lang.org/apUioyJsSdaziPfiE14CoQ
Given a recursive datatype and failable method that operates on it:
sealed trait Expr
case class Lit(x: Int) extends Expr
case class Div(lhs: Expr, rhs: Expr) extends Expr
def evaluate(expr: Expr): Either[String, Int] = ???
Typical examples of functional composition show how to elegantly implement these things:
def evaluate(expr: Expr): Either[String, Int] = expr match {
case Lit(x) => Right(x)
case Div(l, r) =>
for {
x <- evaluate(l)
y <- evaluate(r)
res <- if (y != 0) Right(x / y) else Left("Divide by 0!")
} yield res
}
evaluate(Div(Lit(8), Div(Lit(3), Lit(0)))) // Left("Divide by 0!")
This is great, except that sometimes you also want to do some kind of composition of the error messages. This is especially useful if you want parent nodes to add information to errors propagated from their children.
Perhaps I want to return an error message with context about the entire expression rather than just the information that there was a divide by 0 somewhere:
def evaluate2(expr: Expr): Either[String, Int] = expr match {
case Lit(x) => Right(x)
case Div(lhs, rhs) =>
val l = evaluate2(lhs)
val r = evaluate2(rhs)
val result = for {
x <- l
y <- r
res <- if (y != 0) Right(x / y) else Left(s"$x / 0")
} yield res
result.orElse {
Left(s"(${l.merge}) / (${r.merge})") // take advantage of Any.toString
}
}
evaluate2(Div(Lit(8), Div(Lit(3), Lit(0)))) // Left("(8) / (3 / 0)")
This isn't terrible, but it isn't great either. It's 4 lines of business logic vs. 5-6 of boiler plate.
Now, I'm not the best at functional programming, and I don't know much about Cats and Scalaz, but I do now that this smells like a reusable higher-order function. From the type that I want, I can derive a pretty useful utility function:
def flatMap2OrElse[R, A, B](x: Either[R, A], y: Either[R, A])
(f: (A, A) => Either[R, B])
(g: (Either[R, A], Either[R, A]) => R): Either[R, B] =
(x, y) match {
case (Right(a), Right(b)) => f(a, b)
case _ => Left(g(x, y))
}
Then it's trivial to write a concise form:
def evaluate3(expr: Expr): Either[String, Int] = expr match {
case Lit(x) => Right(x)
case Div(lhs, rhs) =>
flatMap2OrElse(evaluate3(lhs), evaluate3(rhs)) {
(x, y) => if (y != 0) Right(x / y) else Left(s"$x / 0")
} {
(x, y) => s"(${x.merge}) / (${y.merge})"
}
}
evaluate3(Div(Lit(8), Div(Lit(3), Lit(0)))) // Left("(8) / (3 / 0)")
The orElse function taking Eithers is a bit weird, but it's my function and it can be weird if I want it to.
In any case, it seems to me that there should be a pattern here. Is the style of evaluate2 the canonical way of doing it or are there utilities/abstractions I should be looking at to better handle this kind of thing?
EDIT
New Scastie: https://scastie.scala-lang.org/p0odf16PTLOTJPYSF9CGMA
This is a partial answer, but still requires a custom function that feels like it should exist somewhere. I think with just flatMap2 we can do this pretty clearly without the boutique flatMap2OrElse:
def flatMap2[R, A, B](x: Either[R, A], y: Either[R, A])
(f: (A, A) => Either[R, B]): Either[R, B] =
(x, y) match {
case (Right(a), Right(b)) => f(a, b)
case (Left(a), _) => Left(a) // Need Either[R, B]
case (_, Left(b)) => Left(b)
}
def evaluate4(expr: Expr): Either[String, Int] = expr match {
case Lit(x) => Right(x)
case Div(lhs, rhs) =>
val l = evaluate4(lhs)
val r = evaluate4(rhs)
flatMap2(l, r)((x, y) => if (y != 0) Right(x / y) else Left(s"$x / 0"))
.orElse(Left(s"(${l.merge}) / (${r.merge})"))
}
evaluate4(Div(Lit(8), Div(Lit(3), Lit(0)))) // Left("(8) / (3 / 0)")
That being said, this concept should generalize beyond flatMap2. It just feels like this is already a thing.

Related

Scala foldLeft while some conditions are true

How to emulate following behavior in Scala? i.e. keep folding while some certain conditions on the accumulator are met.
def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B
For example
scala> val seq = Seq(1, 2, 3, 4)
seq: Seq[Int] = List(1, 2, 3, 4)
scala> seq.foldLeftWhile(0, _ < 3) { (acc, e) => acc + e }
res0: Int = 1
scala> seq.foldLeftWhile(0, _ < 7) { (acc, e) => acc + e }
res1: Int = 6
UPDATES:
Based on #Dima answer, I realized that my intention was a little bit side-effectful. So I made it synchronized with takeWhile, i.e. there would be no advancement if the predicate does not match. And add some more examples to make it clearer. (Note: that will not work with Iterators)
First, note that your example seems wrong. If I understand correctly what you describe, the result should be 1 (the last value on which the predicate _ < 3 was satisfied), not 6
The simplest way to do this is using a return statement, which is very frowned upon in scala, but I thought, I'd mention it for the sake of completeness.
def foldLeftWhile[A, B](seq: Seq[A], z: B, p: B => Boolean)(op: (B, A) => B): B = foldLeft(z) { case (b, a) =>
val result = op(b, a)
if(!p(result)) return b
result
}
Since we want to avoid using return, scanLeft might be a possibility:
seq.toStream.scanLeft(z)(op).takeWhile(p).last
This is a little wasteful, because it accumulates all (matching) results.
You could use iterator instead of toStream to avoid that, but Iterator does not have .last for some reason, so, you'd have to scan through it an extra time explicitly:
seq.iterator.scanLeft(z)(op).takeWhile(p).foldLeft(z) { case (_, b) => b }
It is pretty straightforward to define what you want in scala. You can define an implicit class which will add your function to any TraversableOnce (that includes Seq).
implicit class FoldLeftWhile[A](trav: TraversableOnce[A]) {
def foldLeftWhile[B](init: B)(where: B => Boolean)(op: (B, A) => B): B = {
trav.foldLeft(init)((acc, next) => if (where(acc)) op(acc, next) else acc)
}
}
Seq(1,2,3,4).foldLeftWhile(0)(_ < 3)((acc, e) => acc + e)
Update, since the question was modified:
implicit class FoldLeftWhile[A](trav: TraversableOnce[A]) {
def foldLeftWhile[B](init: B)(where: B => Boolean)(op: (B, A) => B): B = {
trav.foldLeft((init, false))((a,b) => if (a._2) a else {
val r = op(a._1, b)
if (where(r)) (op(a._1, b), false) else (a._1, true)
})._1
}
}
Note that I split your (z: B, p: B => Boolean) into two higher-order functions. That's just a personal scala style preference.
What about this:
def foldLeftWhile[A, B](z: B, xs: Seq[A], p: B => Boolean)(op: (B, A) => B): B = {
def go(acc: B, l: Seq[A]): B = l match {
case h +: t =>
val nacc = op(acc, h)
if(p(nacc)) go(op(nacc, h), t) else nacc
case _ => acc
}
go(z, xs)
}
val a = Seq(1,2,3,4,5,6)
val r = foldLeftWhile(0, a, (x: Int) => x <= 3)(_ + _)
println(s"$r")
Iterate recursively on the collection while the predicate is true, and then return the accumulator.
You cand try it on scalafiddle
After a while I received a lot of good looking answers. So, I combined them to this single post
a very concise solution by #Dima
implicit class FoldLeftWhile[A](seq: Seq[A]) {
def foldLeftWhile[B](z: B)(p: B => Boolean)(op: (B, A) => B): B = {
seq.toStream.scanLeft(z)(op).takeWhile(p).lastOption.getOrElse(z)
}
}
by #ElBaulP (I modified a little bit to match comment by #Dima)
implicit class FoldLeftWhile[A](seq: Seq[A]) {
def foldLeftWhile[B](z: B)(p: B => Boolean)(op: (B, A) => B): B = {
#tailrec
def foldLeftInternal(acc: B, seq: Seq[A]): B = seq match {
case x :: _ =>
val newAcc = op(acc, x)
if (p(newAcc))
foldLeftInternal(newAcc, seq.tail)
else
acc
case _ => acc
}
foldLeftInternal(z, seq)
}
}
Answer by me (involving side effects)
implicit class FoldLeftWhile[A](seq: Seq[A]) {
def foldLeftWhile[B](z: B)(p: B => Boolean)(op: (B, A) => B): B = {
var accumulator = z
seq
.map { e =>
accumulator = op(accumulator, e)
accumulator -> e
}
.takeWhile { case (acc, _) =>
p(acc)
}
.lastOption
.map { case (acc, _) =>
acc
}
.getOrElse(z)
}
}
Fist exemple: predicate for each element
First you can use inner tail recursive function
implicit class TravExt[A](seq: TraversableOnce[A]) {
def foldLeftWhile[B](z: B, f: A => Boolean)(op: (A, B) => B): B = {
#tailrec
def rec(trav: TraversableOnce[A], z: B): B = trav match {
case head :: tail if f(head) => rec(tail, op(head, z))
case _ => z
}
rec(seq, z)
}
}
Or short version
implicit class TravExt[A](seq: TraversableOnce[A]) {
#tailrec
final def foldLeftWhile[B](z: B, f: A => Boolean)(op: (A, B) => B): B = seq match {
case head :: tail if f(head) => tail.foldLeftWhile(op(head, z), f)(op)
case _ => z
}
}
Then use it
val a = List(1, 2, 3, 4, 5, 6).foldLeftWhile(0, _ < 3)(_ + _)
//a == 3
Second example: for accumulator value:
implicit class TravExt[A](seq: TraversableOnce[A]) {
def foldLeftWhile[B](z: B, f: A => Boolean)(op: (A, B) => B): B = {
#tailrec
def rec(trav: TraversableOnce[A], z: B): B = trav match {
case _ if !f(z) => z
case head :: tail => rec(tail, op(head, z))
case _ => z
}
rec(seq, z)
}
}
Or short version
implicit class TravExt[A](seq: TraversableOnce[A]) {
#tailrec
final def foldLeftWhile[B](z: B, f: A => Boolean)(op: (A, B) => B): B = seq match {
case _ if !f(z) => z
case head :: tail => tail.foldLeftWhile(op(head, z), f)(op)
case _ => z
}
}
Simply use a branch condition on the accumulator:
seq.foldLeft(0, _ < 3) { (acc, e) => if (acc < 3) acc + e else acc}
However you will run every entry of the sequence.

compiler error message when using State monad for memoization

I have a problem to make a working version of the Euler project problem 31 with the use of State trait (inspired from scalaz)
First, I have a solution with a mutable HashMap for memoization. It works but i would like to use the State monad, to understand it and to improve my skills.
I have used it with the fibonacci example, but when i attempt to apply the same technique to my case, i have a compiler error that i don't understand.
I use this implementation for State :
trait State[S, A] {
val run: S => (S, A)
def apply(s: S): (S, A) = run(s)
def eval(s: S): A = run(s)._2
def map[B](f: A => B): State[S, B] =
State { s: S =>
val (s1, a) = run(s)
(s1, f(a))
}
def flatMap[B](f: A => State[S, B]): State[S, B] =
State { s: S =>
val (s1, a) = run(s)
f(a)(s1)
}
}
object State {
def apply[S, A](f: S => (S, A)): State[S, A] = new State[S, A] {
final val run = f
}
def init[S, A](a: A) = State { s: S => (s, a) }
def update[S, A](f: S => S): State[S, Unit] = State { s: S => (f(s), ()) }
def gets[S, A](f: S => A): State[S, A] = State { s: S => (s, f(s)) }
}
my attempt to use it is here :
val coins = List(1, 2, 5, 10, 20, 50, 100, 200)
type MemoKey = (List[Int], Int)
type MemoType = Map[MemoKey, Int]
def ways(listCoins: List[Int], amount: Int): Int = {
def ways_impl(coins: List[Int], sum: Int): State[MemoType, Int] = (coins, sum) match {
case (Nil, 0) => State.init(1)
case (Nil, _) => State.init(0)
case (c :: cs, _) =>
for {
memoed <- State.gets { m: MemoType => m.get((coins, sum)) }
res <- memoed match {
case Some(way) => State.init[MemoType, Int](way)
case None =>
(for {
i <- 0 to sum / c
r <- ways_impl(cs, sum - i * c)
_ <- State.update { m: MemoType => m + ((coins, sum) -> r) }
} yield r).sum
}
} yield res
}
ways_impl(listCoins, amount) eval (Map())
I have a compiler error at this line :
r <- ways_impl(cs, sum - i * c)
The compiler said :
type mismatch; found : State[MemoType,Int] (which expands to) State[scala.collection.immutable.Map[(List[Int], Int),Int],Int] required: scala.collection.GenTraversableOnce[?]
For information, here is my first version with mutable map :
import scala.collection.mutable._
val memo = HashMap[(List[Int], Int), Int]()
val coins = List(1, 2, 5, 10, 20, 50, 100, 200)
def memoWays(coins: List[Int], sum: Int): Int = {
memo.getOrElse((coins, sum), {
val y = ways(coins, sum)
memo += ((coins, sum) -> y)
y
})
}
// brute force method with memoization
def ways(coins: List[Int], sum: Int): Int = (coins, sum) match {
case (Nil, 0) => 1
case (Nil, _) => 0
case (c :: cs, n) =>
(for {
i <- 0 to n / c
r = memoWays(cs, n - i * c)
} yield r).sum
}
println(s"result=${Mesure(ways(coins, 200))}")
What does that error mean ? Why the compiler want a GenTraversableOnce instead of State ?
What kind of thing i don't understand on State monad ?
And, if i may, I have an optional question :
Is my way to memoize with State Monad, is a good choice, or my first implementation with mutable map is better anyway ?
The problem is that your for comprehension is attempting to flatMap two unrelated types: a Range and a State. You're going to have to refactor, although off the top of my head, it's not clear to me how you'll be able to leverage State in a simple way. I'd probably use an immutable Map for the memo, a List to represent the future iterations to be tried, and simple recursion to iterate.

Implement fold with for-comprehension

How can a fold be implemented as a for-comprehension in Scala? I see the only way is to use some recursive call? This is a try that is failing, not sure how to do this? What is the best way to implement fold as a for-comprehension
val nums = List(1,2,3)
nums.fold(0)(_+_)
def recFold(acc: Int = 0): Int = {
(for {
a <- nums
b = recFold(a + acc)
} yield b).head
}
recFold(0) //Stack overflow
If you really want to use for, you don't need recursion, but you would need a mutable variable:
val nums = List(1,2,3)
def recFold(zero: Int)(op: (Int, Int) => Int): Int = {
var result: Int = zero
for { a <- nums } result = op(result, a)
result
}
recFold(0)(_ + _) // 6
Which is pretty similar to how foldLeft is actually implemented in TraversableOnce:
def foldLeft[B](z: B)(op: (B, A) => B): B = {
var result = z
this foreach (x => result = op(result, x))
result
}
Fold can be implemented both ways right to left or left to right. No need to use for plus recursion. Recursion is enough.
def foldRight[A, B](as: List[A], z: B)(f: (A, B) => B): B = {
as match {
case Nil => z
case x :: xs => f(x, foldRight(xs, z)(f))
}
}
#annotation.tailrec
def foldLeft[A, B](as: List[A], z: B)(f: (A, B) => B): B = {
as match {
case Nil => z
case x :: xs => foldLeft(xs, f(x, z))(f)
}
}

Implementing foldRight using Custom Operator

Continuing to work on Functional Programming in Scala exercises, I'm working on implementing foldRight on an IndexedSeq type.
Since foldRight will evaluate with right associativity, I created the following operator for pattern matching.
object :++ {
def unapply[T](s: Seq[T]) = s.lastOption.map(last =>
(last, s.take(s.length - 1)))
}
And then implemented as so:
object IndexedSeqFoldable extends Foldable[IndexedSeq] {
override def foldRight[A, B](as: IndexedSeq[A])(z: B)(f: (A, B) => B): B = {
def go(bs: Seq[A], acc: B): B = bs match {
case x :++ xs => go(xs, f(x, acc))
case _ => acc
}
go(as, z)
}
Ignoring the fact that foldRight can be written with foldLeft, how does my approach hold up?

"call-cc" patterns in Scala?

I found a good article, about call with current continuation patterns. As I understand, they use Scheme and undelimited continuations. Can the patterns from the article be implemented in Scala? Is there any article about delimited continuations patterns in Scala ?
Yes, they absolutely can. callCC looks like this in Scala:
def callCC[R, A, B](f: (A => Cont[R, B]) => Cont[R, A]): Cont[R, A] =
Cont(k => f(a => Cont(_ => k(a))) run k)
Where Cont is a data structure that captures a continuation:
case class Cont[R, A](run: (A => R) => R) {
def flatMap[B](f: A => Cont[R, B]): Cont[R, B] =
Cont(k => run(a => f(a) run k))
def map[B](f: A => B): Cont[R, B] =
Cont(k => run(a => k(f(a))))
}
Here's how you might use it to simulate checked exceptions:
def divExcpt[R](x: Int, y: Int, h: String => Cont[R, Int]): Cont[R, Int] =
callCC[R, Int, String](ok => for {
err <- callCC[R, String, Unit](notOK => for {
_ <- if (y == 0) notOK("Denominator 0") else Cont[R, Unit](_(()))
r <- ok(x / y)
} yield r)
r <- h(err)
} yield r)
You would call this function as follows:
scala> divExcpt(10, 2, error) run println
5
scala> divExcpt(10, 0, error) run println
java.lang.RuntimeException: Denominator 0
Scala has an implementation of typed delimited continuations which used to be shipped with the compiler and standard library, but has been extracted to an external module and pretty much left to rot since then. It's a great shame, and I encourage anyone who's interested in delimited continuations to show that they care about its existence by using and contributing to it.