1

I was looking at the scalaz tutorial at http://eed3si9n.com/learning-scalaz/Applicative.html

and i saw this code :(List("ha", "heh", "hmm") |@| List("?", "!", ".")) {_ + _} the result was res63: List[String] = List(ha?, ha!, ha., heh?, heh!, heh., hmm?, hmm!, hmm.)

i could write this code more readable by using a for loop i.e

for {
    a1 <- List("ha", "heh", "hmm") 
    a2 <- List("?", "!", ".")
} yield {a1 +a2}

i have saw more examples and tried to understand why should i use applicative at all. mostly i can use the map\flatMap functions to deal with all applicative examples.

Can someone give a truely usefull example why should i use it at all?

David H
  • 1,346
  • 3
  • 16
  • 29
  • The main idea is that if you see that programming pattern in different contexts, you will want to generalize it. It's basically the same reason why you have generic list functions rather than functions for int list, float list etc. Of course, it also depends on the surrounding algorithms. – didierc Jul 05 '14 at 13:19
  • 1
    See my [answer here](http://stackoverflow.com/a/19881777/334519) to a very similar question. The applicative functor is a very young abstraction ([2008](http://www.soi.city.ac.uk/~ross/papers/Applicative.html)), and the fact that we have nicer syntax for monadic sequencing is largely historical accident. – Travis Brown Jul 05 '14 at 14:14

1 Answers1

1

Your last example shows why you need applicative itself. To combine actions. Applicative functors are useful when you need sequencing of actions, but don't need to name any intermediate results between executing.

Applicative programming with effects

0xAX
  • 20,957
  • 26
  • 117
  • 206