Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the exact applications of Functor, PointedFunctor, ApplicativeFunctor and Monad? [closed]

I have been studying Scala (and Haskell by extension) for some time now and I'm totally captured by their type system and functional paradigm. Quite recently I stumbled upon "Type Level Programming" and got dragged into things like Functors and other things I haven't heard about (except for Monad, which I knew was something of a mystic nature but had no idea of what to make use of it!). I studied the concepts in Haskell (and got bewitched by its type system and type inference capabilities by the way) and I kinda have a firm grasp of what it means for a type to be Functor, PointedFunctor, ApplicativeFunctor or Monoid in a purely technical level (I still don't know what a Monad is even in technical level) but I'm feeling like an idiot since I see no uses for all this except that perhaps a good categorization of some concepts is acquired (?). What are these things useful for ? Why make life so complex? Why study these stuff and categorize them into various classes?

like image 769
Ashkan Kh. Nazary Avatar asked Nov 30 '22 02:11

Ashkan Kh. Nazary


2 Answers

Why make life so complex?

They are all there to make life more simple!

Firstly, they make the code we write cleaner and clearer.

Secondly, they increase our expressiveness without adding genuinely new language features. For example, Monads let you use a standard syntax to express complex computational contexts, Functors let you think and program in standard ways about data structures, and Applicative Functors let you treat effectful or complex computational contexts as simply as you treat straightforward data, letting you use the functional paradigm cleanly outside pure data.

They all help code reuse and help us understand each others' code because they give us a standard way of thinking about things.

Once you're used to them, you'll not want to do without them!

like image 200
AndrewC Avatar answered Dec 05 '22 03:12

AndrewC


They are all abstractions. Eg. a monoid is something that supports a zero element and addition (must be associative). Examples would be integers, lists, strings etc. I think its rather nice to have a single common "interface" for all those different types.

So why are they useful? You can write a generic sum function for all monoids for example. Instead of writing one for string, integers and so on you only write one generic function. I think that's quite useful.

like image 43
Jan Avatar answered Dec 05 '22 04:12

Jan