这是一个用于为 Ruby 添加函数式编程工具的宝石。它受到 Erlang、Clojure、Haskell 和函数式 Java 的启发。
这是一个用于为 Ruby 添加函数式编程工具的宝石。它受到 Erlang、Clojure、Haskell 和函数式 Java 的启发。
A gem for adding functional programming tools to Ruby. Inspired by Erlang, Clojure, and Functional Java.
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.
Our goal is to implement various functional programming patterns in Ruby. Specifically:
The primary site for documentation is the automatically generated API documentation.
Record, Union, and Tuple, inspired by
Clojure, Erlang,
and other functional languages.Either and Option classes based on Functional Java and Haskell.Delay class based on Clojure delay.ValueStruct, a simple, thread safe, immutable variation of Ruby's OpenStruct class.FinalStruct and FinalVar, which can be written to at most once
before becoming immutable. Based on Java's final keyword.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.
gem install functional-rubyor add the following line to Gemfile:
gem 'functional-ruby'and run bundle install from your shell.
Once you've installed the gem you must require it in your project:
require 'functional'Specifying a protocol:
Functional::SpecifyProtocol(:Name) do
attr_accessor :first
attr_accessor :middle
attr_accessor :last
attr_accessor :suffix
endDefining immutable data structures including Either, Option, Union and Record
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:
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)
endgit checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Functional Ruby is free software released under the MIT License.
暂无开放 Issues,或尚未同步最近议题。