This post is part of a series called functional fundamentals. See the introduction and index. That's also where you can request specific topics
No...
For further actions, you may consider blocking this person and/or reporting abuse
I'm going to log some of my thoughts while reading this post, hopefully this may serve as constructive feedback, as well as notes to other readers-
Category explanation
The explanation on what a category itself is, is spot on. Incredible job there, it unifies both the formal definition and the haskell implementation (
Control.Category
) extremely well.To add to your definition, I'd suggest readers to draw an intuitive analogy to sets. Most people are familiar with sets and set theory, not so much category theory. But really, a set is very similar to a category. In fact, a category can be thought of as a "set" equipped with some morphism on its elements. Don't get scared by the word morphism! It's just a fancy word for a function. Well, a pure function, of course :)
Functors explanation
I think the definition you used for functors is quite vague in an informal way. Don't get me wrong, there's nothing wrong with informality when explaining these topics - but it seems like you tried to combine the functors in fp, and the functors in category theory into one definition. A functor in haskell is really a concrete (and constrained) application of the abstract functor concept from category theory. Meanwhile, an actual functor, is "a mapping between categories".
I personally would suggest preferring the concrete functor definition over the abstract one. To say, "a functor is simply a computational context that can be mapped over" feels both intuitive and correct, in the context of haskell (and therefore in pretty much all fp langs).
Monads "compose"
This wording- "What's special about them is that they are composable.", regarding monads, is....problematic. This isn't really anyone's fault. It just so happens that "compose" is used ambiguously in many places in these contexts. Monads don't compose!. That is their weakness.
I understand what you meant, you're talking about the composition of functions returning monads - the Kleisli arrow. But perhaps it'd be better to say- err, monads chain..? It's a bit of a difficult analogy to be made here. Since skipping applicative functors has been, and still is, a historical mistake. The idea of chaining functions in a higher order world (functor world) - really bears well with applicative functors. Monads are also a pattern for chaining operations - but the monadic operation is binding, whereas an applicative functor operation is just applying. The key distinction is that monad allows dynamically choosing what computation (functor) is executed next, depending on the previous result. Applicatives may only choose statically. See: difference between applicatives and monads.
Monads allow the higher order world and the regular world to seamlessly interface. This is clear in the type signature of
bind
/>>=
. Look at the function it takes as argument-a -> m b
.a
is a regular value,m b
is a higher order value, it's a computational context! A functor! A higher order value! We've just let the user meddle the 2 worlds together.<*>
andfmap
don't let the user do this.This essentially gives you a get-out-of-jail-free card to essentially design a completely imperative API.
Of course, this comes with major drawbacks - hence why many consider the focus on monads before applicatives.... a historical mistake. Monads don't compose. Dynamically choosing effects is expensive. In many cases, an applicative can do everything you need - just much more efficiently. A true unsung hero, it saddens me to not see this article mention applicatives.
|>
in HaskellActually, haskell does have a
|>
in the standard :). It's&
fromData.Function
. It's just a flipped version of$
. Nothing special. Though>>=
does not combineandThen
and&
.>>=
isandThen
(but flipped). The exact haskell alternative is=<<
They're the same thing. In fact, you could write-Which, of course, can be simplified to-
Covariant mapping of functions
The idea you discussed in "Function signatures" is very nice. I think that particular intuition is also a great door opener to contravariance. Contravariant functors, that is. Reverse all the arrows!
Which functor is not a monad?
+1 for the recommendation about "Try to think of a functor that isn't a monad". This is a neat little exercise once you have decent grasp of the covariant functor hierarchy (covariant is the default one in haskell, and therefore other fp langs).
Some hints to aid in your journey, dear readers- Remember, a covariant functor won't be a monad if and only if the functor can proceed through
bind
without actually containing value. You need thea
to actually feed intoa -> m b
! Think of sum types! Notice howMaybe a
is a monad becauseNothing
is not allowed to pass through bind. IfNothing
was allowed, what would you pass to thea -> m b
function? nothing!Good stuff all around. Such a refresher to see something braver than "how to make a landing page with react". Keep it up!
Fair points.
I'm curious how you came upon this article. I posted it almost 3 years ago.
EDIT: just checked out the sources you posted for applicatives. Thanks for those. Real eye-openers. Academic literature made them sound almost like some obscure technicality.
Oh whoa I didn't even notice the date. Whoops, sorry for necroing. A friend of mine posted this and cited it as "an interesting dev.to article". I frankly just assumed it was new :P.
That's pretty hilarious though. My friend must've been searching for articles on FP concepts on dev.to and happened to stumble upon your post.
No problem at all. Hey, I learned something new! I was just curious how that happens.
Still didn't get it, sorry
I think that might be my fault, there were a few places were I was unsure about the explanations I chose.
Can you tell me where you got stuck? Maybe I can improve that section.
Otherwise, Batosz Milewski's series (linked at the bottom of the post) takes a lot more time to explain every concept (in particular the video series) so if this post doesn't work for you, you should definitely check them out.
Sorry for my short first reply I was very frustrated reading the n-th "simple" explanation. My approach would be completely different. What you are doing is describing Category Theorie like a farmer would describe her "functor-monard-object-arrow-a-b-catgeory-unit"-land. What I need is a way from "function, method, class, object"-land to you land.
First of all: About which objects are you talking in when you say "CT deals with objects…". Is this an abstract term for something not related to OOP objects or just what I'm used to?
If you are talking about arrows I will think about functions in the classical programming approach. Use examples to explain how the theory is translated into functional programming.
Haskell and Elm are known for their tight connection to functional programming but Javascript is also very powerful and much more common. Adding examples from JS broadens your target audience.
You introduce "Category", now I realy need an real world example about what you are talking otherwise it's just abstract. And what is the "structure of arrows"? And why could that mean "F a ≠ F b"?
Then you go on with a functor from an object but you just wrote, an functor connects a category to another one. And what is an Unit? Is an Endofunctor the real name of an identitiy functor?
In the next paragraph you talk about the Maybe/Optional and Cmd type. Never heard of it. Also e.g. in PHP, JS, Python an Array is absolutely a real type as it doesn't have to be typed.
Next I assume a typo (or isn't it?): "iff. we" -> "if we". Then the sentence I quitted: "and produces something isomorphic to identity if identity is provided as input". Well what?
Sorry I didn't get it. Maybe I'm just to old and my brain plasticity is below what is needed. But I still don't get the concept.
It looks like we just have very different backgrounds. Your feedback is very useful, I will use it to improve the post.
One of the big problems with learning / teaching FP is that people from IP (imperative programming) have expectations that don't hold for FP. So rather than lacking brain plasticity, you know too much. It is a common problem to which I have probably not given enough attention. To address your specific concerns:
Not the same. Objects in CT are pretty much undefined, nothing more than something for arrows to connect. So you can have a category where CT objects are OOP object, but you can also have a category where objects are apples, or the number 5, or the belief in a higher power.
In programming we are mostly concerned about the cases were objects are types (FP) or states (IP) and arrows are functions (FP) or commands (IP).
You've got it, arrows are isomorphic to functions ("isomorphic" means they have the same 'shape', they act the same).
The isomorphism doesn't work well for javascript because it is neither statically typed nor functional (being functional requires no side-effects, not just passing callbacks around).
This series is about the theory of functional programming, not so much about how to make JS look like Haskell. There's other posts doing that pretty well.
I should have spent more words describing all this. It is all, indeed, very abstract though.
It's a bit much to address in a reply, please give me some time to fix the post.
I mentioned how the isomorphism doesn't really hold up for weakly typed languages. Essentially those languages don't have types, so they also don't have type constructors.
"Monads" in js are more monad-inspired.
Nope. "iff." is short for "if and only if".
Expressed in JS, a function
map(callback)
should return a callbackcbk
that acts just likea => a
, i.e.cbk(foo) === (a => a)(foo)
.Two functions are isomorphic if they produce the same result for the same input.
Thank you for spending so much effort on explaining everything much clear. I can't say I completely understood CT-land but it's a lot clearer.
That has been the clearest explanation I've seen so far. It explains just enough category theory without making pages about it, but it's not just about return, bind and fmap either. So thank you! :D
Good job explaining functors and endofunctors. I am currently working my way through Bartosz Milewski's book on category theory.
That is going to take you far beyond this :-)