applicative

How to define <*> for Option[List[_]] n Scala

[亡魂溺海] 提交于 2019-12-10 16:06:57
问题 This is a followup to my previous question with an example found on the Internet. Suppose I define a typeclass Applicative as follows: trait Functor[T[_]]{ def map[A,B](f:A=>B, ta:T[A]):T[B] } trait Applicative[T[_]] extends Functor[T] { def unit[A](a:A):T[A] def ap[A,B](tf:T[A=>B], ta:T[A]):T[B] } I can define an instance of Applicative for List object AppList extends Applicative[List] { def map[A,B](f:A=>B, as:List[A]) = as.map(f) def unit[A](a: A) = List(a) def ap[A,B](fs:List[A=>B], as

Scalaz: how does `scalaz.syntax.applicative._` works its magic

人走茶凉 提交于 2019-12-10 15:20:37
问题 This question is related to this one, where I was trying to understand how to use the reader monad in Scala. In the answer the autor uses the following code for getting an instance of ReaderInt[String] : import scalaz.syntax.applicative._ val alwaysHello2: ReaderInt[String] = "hello".point[ReaderInt] Which mechanisms does Scala use to resolve the type of the expression "hello".point[ReaderInt] so that it uses the right point function? 回答1: A good first step any time you're trying to figure

Must I implement Applicative and Functor to implement a Monad

三世轮回 提交于 2019-12-09 17:21:34
问题 I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng This should be the standard implementation of Maybe as far as I know. However, this doesn't compile, because the compiler complains: No instance for (Applicative Maybee) and similarly he wants a Functor instance once the Applicative is given. So: Simple question:

Is there a standard name or implementation of the “purely applicative Either”?

南楼画角 提交于 2019-12-09 05:54:39
问题 I frequently find use for what I call the "purely applicative Either ", i.e. Either with the Applicative instance available so long as we don't implement a Monad instance as well. newtype AEither e a = AEither { unAEither :: Either e a } deriving Functor -- technically we only need Semigroup instance Monoid e => Applicative (AEither e) where pure a = AEither (pure a) AEither e1 <*> AEither e2 = AEither (combine e1 e2) where combine (Right f) (Right a) = Right (f a) combine (Left m1) (Left m2)

Proving equivalence of sequence definitions from Applicative and Monad

六眼飞鱼酱① 提交于 2019-12-08 17:43:30
问题 How can I properly prove that sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) sequenceA [] = pure [] sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs is essentially the same to monad inputs as sequenceA' :: Monad m => [m a] -> m [a] sequenceA' [] = return [] sequenceA' (x:xs) = do x' <- x xs' <- sequenceA' xs return (x':xs') In spite of the constraint Applicative and Monad of course. 回答1: Here's a proof sketch: Show that do x' <- x xs' <- sequenceA' xs return (x' : xs') is

What are some better ways to write [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] in Haskell?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 14:45:55
问题 I've run in to a few situations where I need the list: [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)] -- no (0,0) Note that there is no (0,0) in the list. I use the (dx,dy) tuples to search up, down, left, right and diagonally from a coordinate. Every time I write it I feel like there should be a more concise, and/or easier to read way to define it. I'm relatively new to Haskell and I figure somewhere in the bag of Applicative/Functor/Monad tricks there should be a neat way to do

ApplicativeDo in Haskell

两盒软妹~` 提交于 2019-12-07 07:23:45
问题 AFAIK one of the new additions to GHC8 is the ApplicativeDo language extension, which desugars the do-notation to the corresponding Applicative methods ( <$> , <*> ) if possible. I have the following questions. How does it decide whether the desugaring to Applicative methods is possible? From what I know, it does a dependency check (if the later depends on the result of former) to decide the eligibility. Are there any other criteria? Although this addition makes applicative code easier to

Computational cost of applicative style

自古美人都是妖i 提交于 2019-12-06 19:50:31
问题 I am using a small database pool in my web app. And this particular function: withPool pool = bracket (takeConn pool) (putConn pool) can be rewritten in applicative style: withPool = bracket <$> takeConn <*> putConn Arguably it is just as readable and much more elegant. So naturally, I want to write it like that. But database connection pool supposed to be fast, and I am afraid that this style introduces unnecessary overhead. So my question is, how much overhead (if any) does use of

I can't understand Wikipedia's definition of “applicative functor”

旧时模样 提交于 2019-12-06 19:05:09
问题 Studying functors, applicative functors and monads in Haskell, I found this definition on Wikipedia: In functional programming, specifically Haskell, an applicative functor is a structure that is like a monad ( return , fmap , join ) without join , or like a functor with return . I can't understand: it seems to me that providing return (i.e. pure ) to a functor is not sufficient to obtain an applicative functor, because you need to provide ap (i.e. <*> ) too, which cannot be defined in terms

How `sequenceA` works

浪子不回头ぞ 提交于 2019-12-06 06:39:37
问题 I'm new to Haskell and trying to understand how does this work? sequenceA [(+3),(+2),(+1)] 3 I have started from the definition sequenceA :: (Applicative f) => [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) <$> x <*> sequenceA xs And then unfolded recursion into this (:) <$> (+3) <*> $ (:) <$> (+2) <*> $ (:) <$> (+1) <*> pure [] (:) <$> (+3) <*> $ (:) <$> (+2) <*> $ (:) <$> (+1) <*> [] But here i don't understand for which applicative functor operator <*> will be called, for ((-