What is the difference between Functor and Monad?

There are similar questions here, but they are tied to a specific programming language, and I'm looking for an answer at a conceptual level.

As I understand it, Functors are essentially immutable containers that expose the map () API that another functor gets. What addition allows you to call a specific functor a monad?

As I understand it, every monad is a functor, but not every functor is a monad.

+9
source share
2 answers

(Note that this will be a simplified explanation of the concepts of category theory)

Functor

- a : a -> b. , String -> Integer:

function fn(text: string) : integer

- : fa(fb(x)). :

hash(lowercase(text))

, , , , , .

  • String -> (String, Integer)

  • Monad, ,

Functor T, , , :

  • input -> T(input)
  • T(T(input)) -> T(input)

, . .

, , Functor, Functor .

+4

, ,

, .

= + ()

, unit = , .

map = , , .

: , //doubleMe :: Int a → Int b const doubleMe = a => 2 * a; (2).map(doubleMe)// (4)

Monad = unit + flatMap ( )

flatMap = , . .

: , , , .

, ,
append :: ( a, b) → Maybe ( c)

( Functor),

Maybe ("a"). Map (append ("b"))//Maybe (Maybe ("ab"))

?

, , , , .

,

step1: . - append ("b"), - "a", Maybe ("ab")

step2: , Maybe (Maybe ("ab")).

. FlatMap, .
Maybe ("a"). FlatMap (append ("b"))//Maybe ("ab")

, , , , .

0

All Articles