百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

monads

> 编程语言
开源

一些常见 Monad 的简单 Ruby 实现。

614 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一些常见 Monad 的简单 Ruby 实现。

Monads

This library provides simple Ruby implementations of some common monads.

The most important method of each implementation is #and_then (a.k.a. bind or >>=), which is used to connect together a sequence of operations involving the value(s) inside the monad. Each monad also has a .from_value class method (a.k.a. return or unit) for constructing an instance of that monad from an arbitrary value.

Optional

(a.k.a. the maybe monad)

An Optional object contains a value that might be nil.

irb
>> require 'monads/optional'
=> true

>> include Monads
=> Object

>> optional_string = Optional.new('hello world')
=> #

>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #

>> optional_result.value
=> "HELLO WORLD"

>> optional_string = Optional.new(nil)
=> #

>> optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }
=> #

>> optional_result.value
=> nil

Many

(a.k.a. the list monad)

A Many object contains multiple values.

irb
>> require 'monads/many'
=> true

>> include Monads
=> Object

>> many_strings = Many.new(['hello world', 'goodbye world'])
=> #

>> many_results = many_strings.and_then { |string| Many.new(string.split(/ /)) }
=> #

>> many_results.values
=> ["hello", "world", "goodbye", "world"]

Eventually

(a.k.a. the continuation monad)

An Eventually object contains a value that will eventually be available, perhaps as the result of an asynchronous process (e.g. a network request).

irb
>> require 'monads/eventually'
=> true

>> include Monads
=> Object

>> eventually_string = Eventually.new do |success|
     Thread.new do
       sleep 5
       success.call('hello world')
     end
   end
=> #

>> eventually_result = eventually_string.and_then do |string|
     Eventually.new do |success|
       Thread.new do
         sleep 5
         success.call(string.upcase)
       end
     end
   end
=> #

>> eventually_result.run { |string| puts string }
=> #
HELLO WORLD

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言