Is there any intuition to understand join two functions in Monad?

前端 未结 3 887
醉话见心
醉话见心 2021-02-13 17:58

join is defined along with bind to flatten the combined data structure into single structure.

From type system view, (+) 7 :: Num a =>

3条回答
  •  走了就别回头了
    2021-02-13 18:17

    An intuition about join is that is squashes 2 containers into one. .e.g

    join [[1]] => [1]
    join (Just (Just 1)) => 1
    join (a christmas tree decorated with small cristmas tree) => a cristmas tree
    

    etc ...

    Now, how can you join functions ? In fact functions, can be seen as a container. If you look at a Hash table for example. You give a key and you get a value (or not). It's a function key -> value (or if you prefer key -> Maybe value). So how would you join 2 HashMap ?

    Let's say I have (in python style) h={"a": {"a": 1, "b": 2}, "b" : {"a" : 10, "b" : 20 }} how can I join it, or if you prefer flatten it ? Given "a" which value should I get ? h["a"] gives me {"a":1, "b":2}. The only thing I can do with it is to find "a" again in this new value, which gives me 1. Therefore join h equals to {"a":1, "b":20}.

    It's the same for a function.

提交回复
热议问题