>>= 在纯脚本中是什么意思?
What does >>= mean in purescript?
我正在阅读 purescript wiki,发现以下部分用 >>=
解释了 do
。
>>=
是什么意思?
Do notation
The do keyword introduces simple syntactic sugar for monadic
expressions.
Here is an example, using the monad for the Maybe
type:
maybeSum :: Maybe Number -> Maybe Number -> Maybe Number
maybeSum a b = do
n <- a
m <- b
let result = n + m
return result
maybeSum
takes two
values of type Maybe Number
and returns their sum if neither number is
Nothing
.
When using do notation, there must be a corresponding
instance of the Monad type class for the return type. Statements can
have the following form:
a <- x
which desugars to x >>= \a -> ...
x
which desugars to x >>= \_ -> ...
or just x if this is the last statement.
- A let binding
let a = x
. Note the lack of the in
keyword.
The example maybeSum desugars to
::
maybeSum a b =
a >>= \n ->
b >>= \m ->
let result = n + m
in return result
>>=
是一个函数,仅此而已。它位于 Prelude 模块中,类型为 (>>=) :: forall m a b. (Bind m) => m a -> (a -> m b) -> m b
,是 Bind
类型 class 的 bind
函数的别名。您可以在 this link, found in the Pursuit package index.
中找到 Prelude 模块的定义
这个和Haskell中的Monad
类型class密切相关,比较容易找资源。关于这个概念有一个 famous question on SO,如果您希望提高对绑定函数的了解,这是一个很好的起点(如果您现在开始进行函数式编程,可以暂时跳过它)。
我正在阅读 purescript wiki,发现以下部分用 >>=
解释了 do
。
>>=
是什么意思?
Do notation
The do keyword introduces simple syntactic sugar for monadic expressions.
Here is an example, using the monad for the
Maybe
type:maybeSum :: Maybe Number -> Maybe Number -> Maybe Number maybeSum a b = do n <- a m <- b let result = n + m return result
maybeSum
takes two values of typeMaybe Number
and returns their sum if neither number isNothing
.When using do notation, there must be a corresponding instance of the Monad type class for the return type. Statements can have the following form:
a <- x
which desugars tox >>= \a -> ...
x
which desugars tox >>= \_ -> ...
or just x if this is the last statement.- A let binding
let a = x
. Note the lack of thein
keyword.The example
maybeSum desugars to
::maybeSum a b = a >>= \n -> b >>= \m -> let result = n + m in return result
>>=
是一个函数,仅此而已。它位于 Prelude 模块中,类型为 (>>=) :: forall m a b. (Bind m) => m a -> (a -> m b) -> m b
,是 Bind
类型 class 的 bind
函数的别名。您可以在 this link, found in the Pursuit package index.
这个和Haskell中的Monad
类型class密切相关,比较容易找资源。关于这个概念有一个 famous question on SO,如果您希望提高对绑定函数的了解,这是一个很好的起点(如果您现在开始进行函数式编程,可以暂时跳过它)。