This repository has been archived on 2021-12-03. You can view files and clone it, but cannot push or open issues/pull-requests.
2010-04-19 21:08:49 +02:00
|
|
|
Install
|
|
|
|
=======
|
|
|
|
|
|
|
|
gem install functional
|
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
require 'functional'
|
|
|
|
|
2010-06-23 17:01:01 +02:00
|
|
|
# To demonstrate Functional, we create a Class with a infinite loop:
|
|
|
|
class Sequence
|
|
|
|
include Enumerable
|
|
|
|
def initialize first = 0, step = 1
|
|
|
|
@i, @step = first, step
|
|
|
|
end
|
|
|
|
def each
|
|
|
|
# Our infinite loop:
|
|
|
|
loop do
|
|
|
|
yield @i
|
|
|
|
@i += @step
|
|
|
|
end
|
|
|
|
end
|
2010-06-23 17:01:43 +02:00
|
|
|
end
|
2010-06-23 17:01:01 +02:00
|
|
|
|
|
|
|
Functional.new( Sequence.new).
|
|
|
|
select {|i| i.even? }.
|
|
|
|
collect {|i| i/3 }.
|
|
|
|
select {|i| i.even?}.
|
|
|
|
collect {|i| [[[[[[i.even?, i.odd?]]], i, [[[[[[i.class]]]]]]]]] }.
|
|
|
|
flatten. # It flattens everything! Not like: collect {|i| i.flatten }.
|
|
|
|
p
|
|
|
|
|
|
|
|
# Without Functional... Bye bye.
|
|
|
|
Sequence.new.
|
|
|
|
select {|i| i.even? }.
|
|
|
|
collect {|i| i/3 }.
|
|
|
|
select {|i| i.even?}.
|
|
|
|
collect {|i| [[[[[[i.even?, i.odd?]]], i, [[[[[[i.class]]]]]]]]] }.
|
|
|
|
flatten. # It flattens everything! Not like: collect {|i| i.flatten }.
|
|
|
|
p
|
|
|
|
|
|
|
|
It will never realize, that #p doesn't exists, because the first select runs endless.
|
|
|
|
Functional#p prints everything to stdout.
|
|
|
|
|
|
|
|
(0..100000).to_fun.
|
|
|
|
collect {|i| i*3 }.
|
|
|
|
select {|i| i%5 == 2 }.
|
|
|
|
to_a
|
2010-04-19 21:22:30 +02:00
|
|
|
|
|
|
|
What's with _#map_?
|
|
|
|
=================
|
|
|
|
|
|
|
|
Do you know MapReduce? In future #map will be used for MapReduce. Use #collect.
|