码迷,mamicode.com
首页 > 其他好文 > 详细

Monad的基本运算

时间:2018-03-27 14:27:22      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:into   and   pass   mbed   creating   class   puts   ted   span   

A monad is created by defining a type constructor M and two operations, bind and return (where return is often also called unit):

  • The unary return operation takes a value from a plain type (a) and puts it into a container using the constructor, creating a monadic value (with type M a).
  • The binary bind operation ">>=" takes as its arguments a monadic value with type M a and a function (a → M b) that can transform the value.
    • The bind operator unwraps the plain value with type a embedded in its input monadic value with type M a, and feeds it to the function.
    • The function then creates a new monadic value, with type M b, that can be fed to the next bind operators composed in the pipeline.

With these elements, the programmer composes a sequence of function calls (the "pipeline") with several bind operators chained together in an expression. Each function call transforms its input plain type value, and the bind operator handles the returned monadic value, which is fed into the next step in the sequence. Between each pair of composed function calls, the bind operator can inject into the monadic value some additional information that is not accessible within the function, and pass it along. It can also exert finer control of the flow of execution, for example by calling the function only under some conditions, or executing the function calls in a particular order.

 

https://en.wikipedia.org/wiki/Monad_(functional_programming)

Monad的基本运算

标签:into   and   pass   mbed   creating   class   puts   ted   span   

原文地址:https://www.cnblogs.com/feng9exe/p/8656843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!