Is there any recommendation on which to go for between `cats.Applicative[Option].apX`

and `cats.Applicative[Option].mapX`

or it boils down to personal preference?

I personally find the `.apX`

variant more intuitive as i think it fits more into the general idea of applicative: applying functions to values, albeit in a context. The `.mapX`

variants feel slightly out of place for me :/

thinking more about it, I am guessing the

`map`

thingies need to be in there since `Applicatives`

extends `Functors`

I never use either directly. The

`(op1, op2).mapN(f)`

syntax is usually fine.
I guess thats how its gonna play out in practice...I am still going through the Scala with Cats book (underscore), and was a little bit trippy by the explanation of Applicative/Semigroupal and similar functions in the companion objects of both typeclasses

@dadepo for me it's usually much easier to work with

`mapX`

instead of `apX`

but YMMV
having an

`F[A => C]`

would require me to do `map`

on my `F[B]`

first, and with `mapX`

I can get it done without additional maps
in haskell a chain of

`map`

and then `ap`

(times arity) works really nice and you get the `f <$> fa <*> fb <*> fc`

out of the box without any magic. `mapN`

is magic but works really nice here, and I recommend that
Following exercise at https://github.com/scala-exercises/exercises-cats/blob/master/src/main/scala/catslib/Semigroup.scala I import cats.implicits.* cats.kernel.Semigroup, and cats.syntax.semigroup.*. But when trying with code one |+| two, compiler complains

`value |+| is not a member of Option[Int]`

. Anything I miss so it can't get compiled?
Hey guys, Can I ask Haskell's do notation related question?

@JsonFn in this case you are missing

`cats.instances.option._`

thanks @SystemFw :)

here https://github.com/heyrutvik/playground/blob/master/haskell/graham-hutton/src/FunctionalParsers.hs#L40

here https://github.com/heyrutvik/playground/blob/master/haskell/graham-hutton/src/FunctionalParsers.hs#L40

`p'`

and `p''`

working as expected.. but `p`

has error. I think all of them has same meaning but it's not working.
he said he imported

`cats.implicits`

though. @JsonFn do you have `import cats.implicits._`

with the underscore?
However I would advise forgetting about specific imports and just going with

`import cats._, implicits._`

(and `data._`

if you need things like Validated), and removing other imports
if you import *not* have

`implicits._`

then you have to `syntax`

or `instances`

, or they will conflict
(also, it might be due to a conflict because you bring the semigroup syntax twice - once in

`implicits._`

and `syntax.semigroup._`

)
yeah this

yes i have the statement

`import cats.implicits._`

at the beginning of scala file.
just go with this *nothing* else (apart from

`import cats._, implicits._`

and `data._`

when you need it)
@heyrutvik let me have a look

Looks like import cats.syntax.semigroup._ causes the conflict. @SystemFw Thanks for help!

yeah, any

`syntax`

or `instances`

import will conflict with `implicits`

@SystemFw I think I found the issue. I can't use

`do notation`

because I haven't defined monad instance for `Parser`

type. :)
so, there are three things you can do

if you want to keep your code exactly as it is

you need to add this on top of your file

`{-# Language RebindableSyntax #-}`

that's option 1

option 2 is to keep your code as is but define a monad instance. That will require

``{-# Language TypeSynonymInstances #-}`

or

option 3, which I recommend

is change

`Parser`

to be a `newtype`

instead, and define a Monad instance for it
no extensions needed

@heyrutvik

Ohh.. that

`{-# xxx #-}`

is new thing for me! :smile: Where can I find more information on that?
there are dozens of those, from obscure, syntax only enhancements to very important, commonplace extensions

honestly, I'd recommend not going into the rabbit hole for now

define a

`newtype`

for `Parser`

(which you should do anyway)
and add a

`Monad/Applicative/Functor`

instance for it
Great..

honestly, I'd recommend not going into the rabbit hole for now

Sure.. :)

Thanks man for the help! Appreciate it :)

this is the starting point:

`newtype Parser a = Parser { parse :: String -> [(a, String)] }`

So here we are defining Parser type with one type parameter, right?

Is

Is

`parse`

a method for the Parser values? :confused:
@SystemFw

then nevermind, do it like this

```
newtype Parser a = Parser (String -> [(a, String)] )
parse :: Parser a -> String -> [(a, String)]
parse (Parser p) x = p x
```

or like this if you don't want `newtype`

```
data Parser a = Parser (String -> [(a, String)] )
parse :: Parser a -> String -> [(a, String)]
parse (Parser p) x = p x
```

Nope.. not records yet! :)

apparently, there is very subtle difference between

apparently, there is very subtle difference between

`newtype`

and `data`

because both snippet are identical other than that!
never mind.. I will keep reading the book, eventually everything will be covered ;) thanks @SystemFw. BTW I liked your username! :thumbsup:

you can ignore the difference between newtype and data for now

in a nutshell,

`newtype`

guarantees zero-cost wrapping (so no boxing) for one argument product types, which implies slight differences with regards to laziness. All irrelevant for now
@SystemFw hey I'm sitting here with @smarter and we got the higher-rank

`fmap`

to work. amazeballs
I’ve got a problem for you folks.

This is what I have so far:

```
trait Algebra[F[_]] {
def foo(x: String): F[String]
}
case class Foo[A](f: Int => IO[A])
def bar(a: Int): Algebra[IO] = …
def transformed: Algebra[Foo] = new Algebra[Foo] {
def foo(x: String): Foo[String] = Foo(n => bar(n).foo(x))
}
```

Now I want to be able to abstract this for any `Alg[_[_]]`

. I’ve tried playing around with `FunctorK`

, but I can’t quite get it to work since I don’t actually have an algebra to transform:

```
type Alg[_[_]]
implicit val algFunctorK: FunctorK[Alg]
def bar(a: Int): Alg[IO]
def transformed: Alg[Foo] = ???
```

Anything obvious I’m missing here? 🤔

what's the best way to get from an

`OptionT`

to an `EitherT`

?
Hi I have been trying to add some scala check tests to verify semigroup laws for a type which is analogous to scala's Either, but I am unable to compile this:

https://gist.github.com/scalolli/7728581fb1ecba56e44897e49320a766

For the life of me I cannot figure out the compilation error

https://gist.github.com/scalolli/7728581fb1ecba56e44897e49320a766

For the life of me I cannot figure out the compilation error

is there a way to monadically compose Seq.headOption and a function lifted into Option

```
:t (<*>)
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
```

cats calls this

`ap`

@smarter @tpolecat just saw the actual gist: first off, awesome!

Second, I do have some question regarding syntax (and corresponding semantics). The type is this:

`val fmap: [A, B] -> (A => B) => [F[_]] -> F[A] => implicit Functor[F] => F[B]`

The first question is whether order of parameters (e.g. the `implicit`

being last) matters or not. I'm assuming yes because of implicit function types?

The second is scoping and precedence of `->`

vs `=>`

. The thing that confuses me is that `F[_]`

and `[A, B]`

are in different "type parameter lists", so to speak. And I'm confused as to where exactly the `forall`

are.

I.e. I'd expect the type of `fmap`

to be:

`fmap :: forall f a b. Functor f => (a -> b) -> (f a -> f b)`

whereas the one in the gist looks like

`map :: (forall a b. a -> b) -> (forall f. Functor f => f a -> f b)`

which shouldn't even compile? (edit: it should compile but it the `a`

and `b`

aren't the same)

Yeah we had to move the functor because the resulting function type was

`(implicit Functor[F]) => F[A] => F[B]`

so `addOne(Option(2))`

wouldn't compile because it wanted the instance first.
So there's still some funny business but it's really promising.

So there's still some funny business but it's really promising.

It definitely is :)

I showed it to Martin and he was laughing because it was so cool.

this doesn't compile

```
{-# Language RankNTypes #-}
ffmap :: (forall a b. a -> b) -> (forall f. Functor f => f a -> f b)
ffmap f fa = fmap f fa
res = ffmap (+2) $ Just 4
```

```
Couldn't match type ‘a’ with ‘b1’
‘a’ is a rigid type variable bound by
a type expected by the context: a -> b1
at /Users/fabio/.stack/global-project/.stack-work/intero/intero284rwd.hs:7:7
‘b1’ is a rigid type variable bound by
a type expected by the context: a -> b1
at /Users/fabio/.stack/global-project/.stack-work/intero/intero284rwd.hs:7:7
Expected type: a -> b1
Actual type: a -> a
In the first argument of ‘ffmap’, namely ‘(+ 2)’
In the expression: ffmap (+ 2)
In the expression: ffmap (+ 2) $ Just 4
```

this does

```
{-# Language RankNTypes #-}
ffmap :: (a -> b) -> (forall f. Functor f => f a -> f b)
ffmap f fa = fmap f fa
res = ffmap (+2) $ Just 4
```

so perhaps I'm reading the associativity and precedence of

`->`

vs `=>`

wrong?
and

`val fmap: [A, B] -> (A => B) => [F[_]] -> F[A] => implicit Functor[F] => F[B]`

translates to the latter?
I'm assuming

`val x = fmap((s: String) => 3)`

would be `val x: [F[_]] -> F[String] => implicit Functor[F] => F[Int]`

I think it translates to `forall a b. (a -> b) -> (forall f.

Functor f => f a -> f b)`

Ugh what the hell gitter

`forall a b. (a -> b) -> (forall f. Functor f => f a -> f b)`

how about putting type parameters on the val like on a def, plus multiple type parameter lists?

`val x[A, B][F[_]]: (A => B) => F[A] => implicit Functor[F] => F[B]`

or sth like that
right, so

`->`

binds all the way to the end, just like `forall`

normally would, cool
@kubukoz you need to able to put type parameters in different position to express

`RankN`

types though
I realized that once I sent the message

I think what I want to write is

`val fmap: [A, B] (A => B) => [F[_]: Functor] (F[A] => F[B]) = f => fa => fa.map(f)`

seems like we're mechanically not too far from it. You'd need some kind of heuristic for shoving the context bounds into the "right" place, which I guess would be as late as possible?