And then on the scala list

Does anyone have an example of how to use andThen with lists? I noticed that andThen is defined for List, but there is no example in the docs to show how to use it.

I understand that f and Ten g means executing function f and then executing function g. The input of the function g is derived from the function f. It's right?

Question 1 - I wrote the following code, but I do not understand why I should use andThen, because I can achieve the same result with the map.

scala> val l = List(1,2,3,4,5) l: List[Int] = List(1, 2, 3, 4, 5) //simple function that increments value of element of list scala> def f(l:List[Int]):List[Int] = {l.map(x=>x-1)} f: (l: List[Int])List[Int] //function which decrements value of elements of list scala> def g(l:List[Int]):List[Int] = {l.map(x=>x+1)} g: (l: List[Int])List[Int] scala> val p = f _ andThen g _ p: List[Int] => List[Int] = <function1> //printing original list scala> l res75: List[Int] = List(1, 2, 3, 4, 5) //p works as expected. scala> p(l) res74: List[Int] = List(1, 2, 3, 4, 5) //but I can achieve the same with two maps. What is the point of andThen? scala> l.map(x=>x+1).map(x=>x-1) res76: List[Int] = List(1, 2, 3, 4, 5) 

Can anyone share practical examples where andThen is more useful than methods like filter, map, etc. One example that I could see above is that with andThen I could create a new function p, which is a combination of other functions. But this use reveals the usefulness of andThen, not List and andThen

+5
source share
2 answers

andThen inherited from PartialFunction several parents to the inheritance tree for List . You use List as a PartialFunction when accessing your items by index. That is, you can think of List as a function of the index (from zero) to the element that occupies this index in the list itself.

If we have a list:

 val list = List(1, 2, 3, 4) 

We can call List as a function (because it is one):

 scala> list(0) res5: Int = 1 

andThen allows us to compose one PartialFunction with another. For example, maybe I want to create a List where I can access its elements by index, and then multiply the element by 2 .

 val list2 = list.andThen(_ * 2) scala> list2(0) res7: Int = 2 scala> list2(1) res8: Int = 4 

This is essentially the same as using map in a list, except that the calculation is lazy. Of course, you could do the same with view , but there could be some general case where you would like to treat List as just a PartialFunction , instead (I can't think of it at the top of my head).


In your code, you are not actually using andThen for the List itself. Rather, you use it for functions that you go to map , etc. Differences in results between matching a List twice over f and g and a single display over f andThen g , using the composition is preferred when multiple matching becomes expensive. In the case of List s, moving several times can become too expensive if the list is large.

+11
source

When solving l.map(x=>x+1).map(x=>x-1) you go to the list twice. When composing 2 functions using the andThen combinator, and then applying it to the list, you go through only once.

 val h = ((x:Int) => x+1).andThen((x:Int) => x-1) l.map(h) //traverses it only once 
+2
source

Source: https://habr.com/ru/post/1259919/


All Articles