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
2
votes
2 answers

Swift optional binding in generic function

Lovely buggy swift amazed me once again. In my new project I used optional applicative functor with <*> operator, described here. infix operator <*> { associativity left precedence 150 } func <*>(lhs: (A -> B)?, rhs: A?) -> B? { if let…
Aleksey G.
  • 171
  • 8
2
votes
2 answers

haskell join multi-level monad

I'm learning haskell, and trying to use applicative functors as much as possible instead of monad. It is very neat and easy to compose. However, occasionally some types like IO [IO [a]] or IO Maybe IO Maybe a will rise in the code, which cause me…
ccaapton
  • 33
  • 1
  • 3
2
votes
1 answer

Is there bindN in default libraries?

I wonder if there are bind2, bind3 etc kind of functions defined somewhere in standard packages? bind2 :: (Monad m, Applicative m) => (a -> b -> m c) -> m a -> m b -> m c bind2 f a b = join (liftA2 a b) Why I want that? Because I'd like to reduce…
phadej
  • 11,947
  • 41
  • 78
2
votes
3 answers

A way to replicateA like replicateM?

Have: getSdr = Sdr <$> u1 <*> u1 <*> u1 <*> getU1List <*> mcn <*> mcn␣ <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn <*> mcn Want: getSdr = Sdr <$> u1 <*> u1 <*> u1…
Michael Fox
  • 3,632
  • 1
  • 17
  • 27
2
votes
3 answers

Scala Applicatives?

In Scala, how can I append an Option[A] to Option[A]: Meaning: Some("foo") ??? Some("bar") == Some("foobar") In Haskell I'd use an applicative: ghci>import Control.Applicative ghci> (++) <$> (Just "foo") <*> (Just "bar") Just "foobar" Are there…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
2
votes
2 answers

Applicative vs monadic style for simple IO example

Here's two really simple functions f and g. {-# LANGUAGE ScopedTypeVariables #-} module Test where import Control.Applicative f :: IO () f = do y <- (<*>) (pure (show . (*10))) (read <$> readFile "data") writeFile "out" y g :: IO () g =…
Rob Stewart
  • 1,812
  • 1
  • 12
  • 25
2
votes
1 answer

Safe read in Aeson parseJSON

I'm using Aeson to parse json quote data from Yahoo's API. A quote might look like this: { "date": "2010-03-10", "Date": "2010-03-10", "Open": "0.37", "High": "0.37", "Low": "0.34", "Close": "0.35", "Volume": "443000", "Adj_Close":…
Dave
  • 1,031
  • 1
  • 8
  • 23
2
votes
1 answer

How to implement this Traversable use pattern?

When use Data.Traversable I frequently requires some code like import Control.Applicative (Applicative,(<*>),pure) import Data.Traversable (Traversable,traverse,sequenceA) import Control.Monad.State (state,runState) traverseF :: Traversable t =>…
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
2
votes
1 answer

Applicative style parser for constructor with two arguments

I want to write a parser for a comma separated pair of values in angle brackets. I got it to work with the following approach: pair p1 p2 = do x1 <- p1 comma x2 <- p2 return (x1, x2) data Foo = Foo (Bar, Bar) foo :: Parser Foo foo…
kruipen
  • 299
  • 2
  • 10
2
votes
3 answers

applicative rewrite (Haskell)

When I don't grasp how an expression in Haskell works I often find it helps to decompose it into a more basic form. Using the following definitions sequenceA :: (Applicative f) => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) <$> x…
planarian
  • 2,047
  • 18
  • 18
1
vote
2 answers

F# Sort array of tuples

let standard = (0, 4.5M, 4L) let tuples = [| ("A", -2, 1.0M, 2L); ("B", -1, 2.0M, 3L); ("C", 0, 3.0M, 4L); ("D", 1, 4.0M, 5L); ("E", 2, 5.0M, 6L) |] let qualified = tuples …
John John
  • 377
  • 1
  • 11
1
vote
1 answer

How to remove a suffix from a string with Applicative regex?

I use regex-applicative library. I want to remove suffix ".end" but only if it's really a suffix (a tail of a string, not somewhere in the middle of the string) and to use replace for it. replace (some anySym <* ".end") "start.end" It works:…
RandomB
  • 3,367
  • 19
  • 30
1
vote
2 answers

Haskell: Implementing an Applicative List

This is a self-motivated exercise. To get the final case of <*> to work, I resorted to implementing concatenation as an auxiliary function. Have I missed a trick? That is, could that case have been written without needing an auxiliary function or…
Rik
  • 55
  • 7
1
vote
0 answers

Can I get the compiler to realize there is an Applicative for the superclass?

I've got a trait which has an implicit cats.Applicative, and I have a case class which implements that trait. I want to be able to call methods from cats.syntax.apply on tuples of that case class, but the compiler doesn't realize it needs to go…
Dylan
  • 13,645
  • 3
  • 40
  • 67
1
vote
1 answer

QuickCheck returns "0 tests" when checking Applicative homomorphism property (Binary Tree)

I would like to check that homomorphism Applicative law holds for datatype BinTree: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE ViewPatterns #-} module Laws where import…
F. Zer
  • 1,081
  • 7
  • 9