What are the exact uses of Functor, PointedFunctor, ApplicativeFunctor and Monad?

I have been studying Scala (and Haskell by extension) for some time, and I am completely captivated by their type system and functional paradigm. Most recently, I stumbled upon “Level-Level Programming” and was dragged onto things like “Funters” and other things that I had not heard about (except for the Monad, which I knew was something of a mystical nature, but no idea had what to use It!). I studied concepts in Haskell (and by the way, bewitched their system type and output type), and I, as I understand it, firmly know what this means for the Functor, PointedFunctor, ApplicativeFunctor or Monoid type at a purely technical level (I still I don’t know what Monad is even on a technical level), but I feel like an idiot because I don’t see any applications for all this, except that, perhaps, a good classification of some concepts (?) is obtained. What is this good for? Why is life so complicated? Why study these things and classify them into different classes?

+4
source share
5 answers

Why is life so complicated?

They are all there to make life easier!

First, they make the code we write more clean and understandable.

Secondly, they increase our expressiveness without adding really new language features. For example, Monads allows you to use standard syntax to express complex computational contexts. Functors allow you to think and program in standard ways about data structures, and Applicative Functors allow you to view efficient or complex computational contexts as easily as you process simple data, allowing you to use the functional paradigm purely behind pure data.

They all help reuse the code and help us understand each other's code, because they give us a standard way to think about things.

As soon as you get used to them, you will not want to do without them!

+9
source

These are all abstractions. For instance. monoid - this is what supports the zero element and the addition (should be associative). Examples are integers, lists, strings, etc. I think it's pretty good to have one common “interface” for all of these different types.

So why are they helpful? You can write a common sum function for all monoids, for example. Instead of writing one for a string, integers, etc., you write only one common function. I think this is very helpful.

+5
source

We must first ask: What are functors, monads, .... If we do not know what this concept is (in the sense), it is difficult to talk about their use.

These concepts are taken from . They arise due to the fact that many objects in mathematics (and, therefore, in functional programming) have common, abstract properties. Once we know and understand these properties, we can use them to write very general code that is reused for a very large number of tasks.


To give an example: everyone knows the function

map :: (a -> b) -> ([a] -> [b]) 

Having a function from a to b , we can create a function that works in lists [a] . Functors are a generalization of this concept. Everything that can be compared in this way (and preserves the laws of the functor ) is called a functor. So Functor Announces

 fmap :: Functor f => (a -> b) -> (fa -> fb) 

In the case of lists, f becomes [] . With fmap we can map lists (there it is equal to map ), but also over Maybe s, various collections, trees, and even over functions.

see also

+4
source

In addition, I want to note that monads are not "mystical". Especially container-like monads (list, Maybe, Identity) are pretty clear. They are similar to functors, but with a twist: using fmap , the "form" (for example, the number of elements in the list) of the original functor is saved, for example. you cannot use fmap to implement something like filter . Therefore, monads have a "bind" function (in Haskell it (>>=) ) that allows for such things, but they are also not magical (for example, for lists, this is the same as the good old concatMap ). In addition, monads have a return function to wrap a single value.

Now, many other things, not "container-like" things, are monads. There are monads that can work with "stored computing" ( Cont to continue the monad). They can provide ( Reader ), collect ( Writer ) or hold ( State ) some kind of "additional context". A very useful “context” is the “state of the rest of the world”, better known as IO . In this case, the type system (especially the restrictions imposed by polymorphism and class types) can protect against unwanted interactions and force a certain order of calculations (which is not trivial in a lazy language), so we do not need dirty hacks or a language to do IO in a pure language . Some people think that this is a kind of magic, but this is just a clever use of the type system, and monads are not the only solution to this problem (for example, the Clean language uses types of uniqueness for this).

+3
source

"What are these tigers for?" Oh! you can use them to write FizzBuzz: http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html

+1
source

Source: https://habr.com/ru/post/1438615/


All Articles