Questions tagged [applicative]

In Haskell, Applicative functors are functors such that two functorial values can be combined into one, whilst the two values inside are combined via a functional application. An applicative functor has more structure than a functor but less than a monad.

In Haskell, applicative functors are functors such that two functorial values (of type Applicative f => f a) can be combined into one, and the two values inside will be combined via a functional application (so the types "on the inside" must be compatible).

Definition

 class (Functor f) => Applicative f where
   pure  :: a -> f a                          -- injection
   (<*>) :: f (a -> b) -> f a -> f b          -- combination

The pure function lifts any value into the functor. (<*>) changes a functorial function into a function over functorial values. Applicative functor should satisfy some laws:

 pure id <*> v = v                            -- Identity
 pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition
 pure f <*> pure x = pure (f x)               -- Homomorphism
 u <*> pure y = pure ($ y) <*> u              -- Interchange

And the Functor instance should satisfy the following law:

 fmap f x = pure f <*> x                      -- Fmap
572 questions
1
vote
1 answer

Can we reverse Applicative of List?

I'm reading about scalaz and noticed that we can make a list of Applicatives to be an Applicative of List. def sequenceA[F[_]: Applicative, A](list: List[F[A]]): F[List[A]] = list match { case Nil => (Nil: List[A]).point[F] …
St.Antario
  • 26,175
  • 41
  • 130
  • 318
1
vote
1 answer

Understanding FoldLeft in ScalaZ

I'm reading some articles about ScalaZ and have a question about understanding it. In this article, we generalize the sum function, to abstract away the type to be summed. def sum[T](xs: List[T])(implicit m: Monoid[T]) = //... Where trait…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
1
vote
1 answer

Why the third function doesn't get called in a pipeK when all of them are futures?

I can't figure out why the third function (i.e. doStuff3) isn't being called, so the console.log on fork should print "hello world!!!!" const doStuff = () => Future.of(["hello", "world"]), doStuff2 = (x, y) => Future((resolve, reject) =>…
1
vote
1 answer

Haskell Applicative Functor in F#

In Haskell we can write code like this: // (<*>) :: Applicative f => f (a -> b) -> f a -> f b // const :: a -> b -> a // id :: b -> b let id = const <*> const How to do same thing in F#? I try write code something like this, but it isn't same let…
giokoguashvili
  • 2,013
  • 3
  • 18
  • 37
1
vote
1 answer

How to apply a function to a matrix?

This is a follow-up to my previous question Now I know that cats provides an instance of Apply for Vector. So I can write: import cats.implicits._ scala> val f: Int => Int = _ + 2 f: Int => Int = scala> Vector(f) ap Vector(1, 2,…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

How does this Haskell code work? fmap fmap (,) <*> Just . reverse

Would someone help explain/breakdown the following code: λ> fmap fmap (,) <*> Just . reverse $ "stackoverflow" Just ("stackoverflow","wolfrevokcats") It would be great if someone could walk me through their process of understanding the mechanics of…
matt
  • 1,817
  • 14
  • 35
1
vote
3 answers

Example of functions as applicatives, in foldMap and filter for foldables

In the first hunk, filterF is implemented with foldMap import Data.List pred :: a -> Bool pred = undefined wrapperOfA :: (Applicative f, Monoid (f a)) => a -> Bool -> f a wrapperOfA a condition = if condition then pure a else mempty -- foldMap…
dcl04
  • 89
  • 8
1
vote
1 answer

Scala MapN with conditions

Hi I have the following Scala code with cats library results = patrons.map(p => { (verifyCardId(p.cardId), verifyAddress(p.address)).map2( (maybeValidCard, maybeValidAddress) => { val result = for { idCheck <- maybeValidCard …
Don Djoe
  • 705
  • 1
  • 10
  • 21
1
vote
1 answer

How to quickcheck applicative homomorphism property?

As an exercise, I am trying to quickCheck the applicative's homomorphism property: pure f <*> pure x = pure (f x) When I try to write the property in a general way using phantom types, I seem to run into endless 'Could not deduce' errors. At this…
mherzl
  • 5,624
  • 6
  • 34
  • 75
1
vote
0 answers

Can Applicative be implemented "nicely" in kotlin?

I have defined a Result type like this: sealed class BadResult { data class Description(val description: String): BadResult() data class Exception (val exception : Throwable): BadResult() } sealed class BadTree { data class Leaf(val…
1
vote
1 answer

Monad Transformer and applicative Maybe

Inside a do block of a ExceptT String IO () I have a function that produces a ReaderT like so: type UDCEnv = (AWS.Env, Bool) uploadVersionFilesToCaches :: S3.BucketName -> FilePath ->…
tmpz
  • 368
  • 3
  • 12
1
vote
2 answers

Predefined Haskell operator for applying pure values in an Applicative?

If I have a function (e.g. of type a -> b) wrapped in an Applicative and a value that can be applied to it (i.e. a value of type a in the above example), I can apply it as follows: doSomething :: Applicative f => f (a -> b) -> a -> f b doSomething…
Jules
  • 14,841
  • 9
  • 83
  • 130
1
vote
1 answer

Understanding how the pure function is resolved in Haskell

In GHCi when I type pure 2 it returns 2; or pure "aa" returns "aa". I wonder how this applicative instance is resolved for 2 or "aa" by GHCi.
milad zahedi
  • 787
  • 6
  • 21
1
vote
2 answers

Ziplists with longest-style applicative behaviour

Is it possible in Haskell do define a type similar to ziplist, in which operation a <*> b will produce list which is as long as the longest of a and b. It is clear that in this case we must assume that a and b are lists over something like Monoid,…
1
vote
1 answer

Applicative validation syntax scalaz + shapeless 2.0

I'm trying to convert this Applicative validation syntax example into Scalaz 7 + Shapeless 2.0 //for jupyter-scala kernel //classpath.add("org.scalaz" %% "scalaz-core" % "7.2.7") //classpath.add("com.chuusai" %% "shapeless" % "2.3.2") case class…
alphageek
  • 770
  • 4
  • 15