David H
David H

Reputation: 1356

Why to use Applicative

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?

Upvotes: 1

Views: 137

Answers (1)

0xAX
0xAX

Reputation: 21817

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

Upvotes: 1

Related Questions