Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how scala folding works?

I am trying to understand the following code but can't.

it is supposed to create a child actor for an Event if it does not exist, otherwise says that the Event exist as it as an associated child actor.

context.child(name).fold(create())(_ => sender() ! EventExists)

But the fold here does not make sense to me. If the context.child is emtpty we get the creation and i understand that. However if there is children we are still going to create why ?

like image 288
MaatDeamon Avatar asked May 01 '16 19:05

MaatDeamon


People also ask

How does fold left work?

foldLeft() method is a member of TraversableOnce trait, it is used to collapse elements of collections. It navigates elements from Left to Right order. It is primarily used in recursive functions and prevents stack overflow exceptions.

What is foldLeft and foldRight in Scala?

In Scala, we can use foldLeft and foldRight methods for collection types like List . Both methods recursively combine items into another item. foldLeft combines items from left one to right one, on the other hand foldRight does this from right one to left one.

What is foldLeft in spark?

The Scala foldLeft method can be used to iterate over a data structure and perform multiple operations on a Spark DataFrame. foldLeft can be used to eliminate all whitespace in multiple columns or convert all the column names in a DataFrame to snake_case.

What is reduceLeft in Scala?

2.2. reduceLeft is used to reduce a collection by applying a function to each element in order to combine them and return a single result. Let's have a look at the reduceLeft signature: def reduceLeft[B >: A](op: (B, A) ⇒ B): B.


1 Answers

Akka's child returns an Option

As you can see from Option's scaladoc:

fold[B](ifEmpty: ⇒ B)(f: (A) ⇒ B): B Returns the result of applying f to this scala.Option's value if the scala.Option is nonempty. Otherwise, evaluates expression ifEmpty.

Or making it more clear:

This (fold) is equivalent to scala.Option map f getOrElse ifEmpty.

So the first parameter of fold is lazy (call-by-name) and evaluates only if Option is empty. The second parameter (a function) is called only if Option is not empty.

Experiment:

scala> Some(0).fold({println("1");1}){_ => println("2"); 2}
2
res0: Int = 2

scala> None.fold({println("1");1}){_ => println("2"); 2}
1
res1: Int = 1

Here's some readings about:

https://kwangyulseo.com/2014/05/21/scala-option-fold-vs-option-mapgetorelse/

And some critics of that approach:

http://www.nurkiewicz.com/2014/06/optionfold-considered-unreadable.html

But in Option.fold() the contract is different: folding function takes just one parameter rather than two. If you read my previous article about folds you know that reducing function always takes two parameters: current element and accumulated value (initial value during first iteration). But Option.fold() takes just one parameter: current Option value! This breaks the consistency, especially when realizing Option.foldLeft() and Option.foldRight() have correct contract (but it doesn't mean they are more readable).

like image 106
dk14 Avatar answered Sep 26 '22 17:09

dk14