Welcome! Got a question? Do you have -Ypartial-unification turned on? Other FAQs: http://typelevel.org/cats/faq.html
List
behaves weirdly?
*>
means "and then"
State
, checkIO
, check
Ok the intuition holds...
List(1,2,3) *> List(4, 5, 6)
<--- intuition breaks
fs2.Stream
behaves like List
in many respects so this is a good exercise regardless
StateT
-type solution
Most literature focuses on implementing them on one hand, or in "real world examples" without much explanations on the other, whereas I think developing the sort of thinking that lets you write code using algebras only is very important, and not necessarily always explained well
@felher this really matters to me so I want to expand a bit. Let's take State
as an example. In most cases books/tutorial fall in two categories:
modify
function that we can use in a for comprehension. Don't worry about how it works now...[proceeds with concrete example]
"a => (a, s)
where each function gets a new copy of the State. If we write down a couple of example we can see that it fits the Monad abstraction. Let's write the instance for Monad"Obviously they are both useful in different respects, however there are drawbacks:
IO
.I like to use a third approach. After having explained what the main algebras bring to the table (Functor lifts 1-arg functions, Applicative lifts n-ary functions, Monad gives you context sensitivity), I present State like this:
State is a datatype representing computations that describe modifications to a value, with the following algebra:
trait State[S, A] // S state, A output
object State {
def get[S]: State[S, S]
def set[S](s: S): State[S, Unit]
implicit def m: Monad[State[S, ?]] // stress that this implies Functor and Applicative
}
and then you have them write programs in this "language", without knowing anything about the internal representation, but only the State algebra (get, set, what the type means, any laws, the monad instance). After they manage to use it like that, you can explain how it's implemented (typically you'll go back and forth a few times in real life). The advantage is that this is aimed to instill the sort of algebraic thinking that is not only one of the biggest advantages of FP, but also necessary when you deal with opaque/complex abstractions like IO
or Stream
local
, I'm been waiting for the talk for a while. That's still ReaderT
though I think?
Reader
, but it's advanced so I didn't mention it here
ask
, I think this is a good place to start.
Reader
alone though
State
only you can have them write quite a few interesting programs, with Reader
less so (unless you have ReaderT
). Btw my favourite non-obvious use case is using Reader
to write shapeless typeclass instances that depend on a runtime value
Hi,
Playing with introduction code http://47deg.github.io/fetch/docs.html#introduction-0
// type Fetch[A] = cats.free.Free[fetch.FetchOp, A]
def getUser(id: UserId): Fetch[User] = Fetch(id)
for {
x: User <- getUser(1)
} yield s"${x.id} ${x.username}”
Produces following error:Error:(105, 23) value filter is not a member of fetch.Fetch[User]
Thoughts on how to get around this?
: Type
or if foo
, it calls withFilter
withFilter
call
withFilter :: MonadError f e => (a -> Bool) -> f a -> f a
function yourself, but honestly you should probably just have @oleg-py’s better for
on all the time anyway (along with kind projectors or pascal)