applicative

Typeclass instances for functions

随声附和 提交于 2019-12-12 17:35:20
问题 I've just realized, that Functions have instances for Monad, Functor and Applicative. What I usually do, when I see some typeclass instance I don't get, is write some well-typed expression and see what it returns: Can somebody explain these instances? You usually hear about the instances for List and Maybe, which by now are natural to me, but I don't understand how Functions can be a Functor or even a Monad. EDIT: Okay, this is a valid well-typed expression that doesn't compile: fmap (+) (+)

Example of Applicative composition in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-12 12:33:05
问题 This is a followup to my old questions: I know that monads are not composable, i.e. if M1[_] and M2[_] are monads M2[M1[_]] is not necessarily a monad. For instance, List[Int] and Option[Int] are monads but Option[List[Int]] is not automatically a monad and therefore I need a monad transformer to use it as a monad (as in here) I know that applicative functors are composable. I guess it means that if A1[_] and A2[_] are applicatives then A2[A1[_]] is always an applicative. Is it correct ?

Functors and Applicatives for types of kind (* -> *) -> *

你离开我真会死。 提交于 2019-12-12 09:31:45
问题 I ran into a situation where my code would benefit from using Functor and Applicative -like abstractions, but for types of kind (* -> *) -> * . Defining a higher-kinded functor can be done with RankNTypes like this class HFunctor f where hfmap :: (forall x. a x -> b x) -> f a -> f b But the higher kind version of Applicative is a bit trickier. This is the best I could come up with: class HFunctor f => HApplicative f where hpure :: (forall x. a x) -> f a (<**>) :: f (a :-> b) -> f a -> f b

How to parse an optional flag as a Maybe value?

自作多情 提交于 2019-12-12 07:32:05
问题 I'm trying to use optparse-applicative to parse a Maybe String but I can't find anywhere how to deal with Maybe . The only thing I found is to add a default value but I really need a Nothing if user didn't supply an option instead of "" . Is there any way to achieve this ? Here is an example of working code: import Options.Applicative data Config = Config { cIn :: String , cOut :: String } deriving Show configParser :: Parser Config configParser = Config <$> strOption (long "in" <> short 'i')

How to define <*> for applicative parser?

和自甴很熟 提交于 2019-12-11 05:15:22
问题 Suppose we define parser as a function type Parser[A] = String => List(A, String) The parser takes an input string and yields a sequence of pairs. Each pair consists of the parsing result and unconsumed part of the input As I understand, we can define the parser as an applicative and implement a seq parser combinator in terms of <*> . The seq combinator is a function, which takes two parsers p and q as its arguments and returns a new parser, which applies p and q to the input sequentially .

Matrix as Applicative functor, which is not Monad

允我心安 提交于 2019-12-11 02:36:09
问题 I run into examples of Applicatives that are not Monads. I like the multi-dimensional array example but I did not get it completely. Let's take a matrix M[A] . Could you show that M[A] is an Applicative but not a Monad with Scala code ? Do you have any "real-world" examples of using matrices as Applicatives ? 回答1: Something like M[T] <*> M[T => U] is applicative: val A = [[1,2],[1,2]] //let's assume such imaginary syntax for arrays val B = [[*2, *3], [*5, *2]] A <*> B === [[2,6],[5,4]] There

Question about applicative and nested Maybe

孤者浪人 提交于 2019-12-10 18:57:43
问题 I wrote this function: appFunc :: Integer -> Integer -> Bool -> Maybe (Integer,Integer) appFunc i1 i2 b = if b then Just (i1,i2) else Nothing And then I use it as such in GHCi: > appFunc <$> Just 3 <*> Nothing <*> Just True Nothing Which is great because if at least one of the parameters is Nothing then the whole expression evaluates to Nothing . However, when all parameters are Just then I get a nested Maybe : > appFunc <$> Just 3 <*> Just 1 <*> Just False Just Nothing Ideally, I would like

How to compose functions that return Validation?

☆樱花仙子☆ 提交于 2019-12-10 18:15:52
问题 This is a follow-up to my previous question Suppose I have two validating functions that return either the input if it is valid or the error messages if it is not. type Status[A] = ValidationNel[String, A] val isPositive: Int => Status[Int] = x => if (x > 0) x.success else s"$x not positive".failureNel val isEven: Int => Status[Int] = x => if (x % 2 == 0) x.success else s"$x not even".failureNel Suppose also that I need to validate an instance of case class X : case class X(x1: Int, // should

Combining the elements of 2 lists

混江龙づ霸主 提交于 2019-12-10 17:38:12
问题 Assume we have two lists : val l1=List("a","b","c") val l2 = List("1","2","3") What I want is : List("a1", "b2", "c3") that is, adding the nth element of l1 with the nth element of l2 A way to achieve it is : (l1 zip l2).map (c => {c._1+c._2}) I just wonder if one could achieve it with an Applicative. I tried : (l1 |@| l2) { _+ _ } but it gives all the combinations : List(a1, a2, a3, b1, b2, b3, c1, c2, c3) Any idea? Thank you Benoit 回答1: You cannot do that with strict lists, so instead use

In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,…`?

丶灬走出姿态 提交于 2019-12-10 17:28:39
问题 Class Applicative is declared as: class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b We can represent fmapi, i=0,1,2,... in terms of pure and (<*>) : fmap0 :: a -> f a fmap0 = pure fmap1 :: (a -> b) -> f a -> f b fmap1 g x = pure g <*> x fmap2 :: (a -> b -> c) -> f a -> f b -> f c fmap2 g x y = pure g <*> x <*> y fmap3 :: (a -> b -> c -> d) -> f a -> f b -> f c -> f d fmap3 g x y z = pure g <*> x <*> y <*> z In applicative, how can <*> be represented in