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

functional-ruby

> 编程语言
开源

这是一个用于为 Ruby 添加函数式编程工具的宝石。它受到 Erlang、Clojure、Haskell 和函数式 Java 的启发。

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

工具介绍

这是一个用于为 Ruby 添加函数式编程工具的宝石。它受到 Erlang、Clojure、Haskell 和函数式 Java 的启发。

Functional Ruby

A gem for adding functional programming tools to Ruby. Inspired by Erlang, Clojure, and Functional Java.

Introduction

Two things I love are Ruby and functional programming. If you combine Ruby's ability to create functions sans-classes with the power of blocks, proc, and lambda, Ruby code can follow just about every modern functional programming design paradigm. Add to this Ruby's vast metaprogramming capabilities and Ruby is easily one of the most powerful languages in common use today.

Goals

Our goal is to implement various functional programming patterns in Ruby. Specifically:

  • Be an 'unopinionated' toolbox that provides useful utilities without debating which is better or why
  • Remain free of external gem dependencies
  • Stay true to the spirit of the languages providing inspiration
  • But implement in a way that makes sense for Ruby
  • Keep the semantics as idiomatic Ruby as possible
  • Support features that make sense in Ruby
  • Exclude features that don't make sense in Ruby
  • Keep everything small
  • Be as fast as reasonably possible

Features

The primary site for documentation is the automatically generated API documentation.

  • Protocol specifications inspired by Clojure protocol, Erlang behavior, and Objective-C protocol.
  • Function overloading with Erlang-style function pattern matching.
  • Simple, thread safe, immutable data structures, such as Record, Union, and Tuple, inspired by Clojure, Erlang, and other functional languages.
  • Thread safe, immutable Either and Option classes based on Functional Java and Haskell.
  • Memoization of class methods based on Clojure memoize.
  • Lazy execution with a Delay class based on Clojure delay.
  • ValueStruct, a simple, thread safe, immutable variation of Ruby's OpenStruct class.
  • Thread safe data structures, such as FinalStruct and FinalVar, which can be written to at most once before becoming immutable. Based on Java's final keyword.

Supported Ruby Versions

MRI 2.0 and higher, JRuby (1.9 mode), and Rubinius 2.x. This library is pure Ruby and has no gem dependencies. It should be fully compatible with any interpreter that is compliant with Ruby 2.0 or newer.

Install

bash
gem install functional-ruby

or add the following line to Gemfile:

ruby
gem 'functional-ruby'

and run bundle install from your shell.

Once you've installed the gem you must require it in your project:

ruby
require 'functional'

Examples

Specifying a protocol:

ruby
Functional::SpecifyProtocol(:Name) do
  attr_accessor :first
  attr_accessor :middle
  attr_accessor :last
  attr_accessor :suffix
end

Defining immutable data structures including Either, Option, Union and Record

ruby
Name = Functional::Record.new(:first, :middle, :last, :suffix) do
  mandatory :first, :last
  default :first, 'J.'
  default :last, 'Doe'
end

anon = Name.new #=> #"J.", :middle=>nil, :last=>"Doe", :suffix=>nil>
matz = Name.new(first: 'Yukihiro', last: 'Matsumoto') #=> #"Yukihiro", :middle=>nil, :last=>"Matsumoto", :suffix=>nil>

Pattern matching using protocols, type checking, and other options:

…

Performance improvement of idempotent functions through memoization:

ruby
class Factors
  include Functional::Memo

  def self.sum_of(number)
    of(number).reduce(:+)
  end

  def self.of(number)
    (1..number).select {|i| factor?(number, i)}
  end

  def self.factor?(number, potential)
    number % potential == 0
  end

  memoize(:sum_of)
  memoize(:of)
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License and Copyright

Functional Ruby is free software released under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

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

> 相关工具

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