Compare commits

..

No commits in common. "master" and "v0.0.1" have entirely different histories.

5 changed files with 23 additions and 558 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
functional.gemspec
pkg

View file

@ -1,8 +1,3 @@
Obsolete
========
Use `#lazy`.
Install Install
======= =======
@ -13,88 +8,8 @@ Usage
require 'functional' require 'functional'
# To demonstrate Functional, we create a Class with a infinite loop: obj = 0 .. 10**12
class Sequence Functional.new( obj).select {|i| i.even? }.collect {|i| i/3 }.select {|i| i.even? }.each &method( :puts)
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
end
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
Thanks to `Symbol#to_proc`:
Sequence.new.to_fun.
select( &:even?).
collect {|i| i/3 }.
select( &:even?).
collect {|i| [[[[[[i.even?, i.odd?]]], i, [[[[[[i.class]]]]]]]]] }.
flatten. # It flattens everything! Not like: collect {|i| i.flatten }.
p
If you know methodphitamine, combine it:
require 'methodphitamine'
Sequence.new.to_fun.
select( &it.even?).
collect( &it/3).
select( &it.even?).
collect {|i| [[[[[[i.even?, i.odd?]]], i, [[[[[[i.class]]]]]]]]] }.
flatten.
p
(0..100000).to_fun.
collect( &it*3).
select( &it%5 == 2).
to_a
Makros
======
seq = Sequence.new.to_fun
seq = seq.select &it.even? if must_be_even
seq = seq.
collect( &it/3).
select( &it.even?).
collect {|i| [[[[[[i.even?, i.odd?]]], i, [[[[[[i.class]]]]]]]]] }
seq = seq.flatten if please_flatten
if print_it
seq.p
else
seq_to_a
end
What's with _#map_? What's with _#map_?
================= =================

View file

@ -1 +1 @@
0.1.5 0.0.1

View file

@ -1,465 +1,46 @@
class ::Regexp
class NegRegexp
def initialize r
@rx = r
end
def match l
! @rx.match( l)
end
def =~ l
! @rx =~ l
end
def -@
@rx
end
end
def -@
NegRegexp.new self
end
end
class ::Object
def to_fun meth = nil
Functional.new self, meth
end
end
class Counter
include Enumerable
attr_reader :c
def initialize first = nil, step = nil
@c, @step = first || 0, step || 1
end
def next; @c += @step end
def to_i; c.to_i end
def to_f; c.to_f end
def + i
@c += @step*i
end
def each &e
loop { e.call self; self.next }
end
end
class Functional class Functional
def self.__version__() '0.1.5' end
include Enumerable include Enumerable
class DEFAULT def self.method_missing meth, *args, &exe
self.new.send meth, *args, &exe
end end
class Base def push_method code, *args, &exe
attr_reader :exe name = "__meth_#{exe.object_id}"
attr_accessor :next define_singleton_method name, &exe
attr_reader :caller @stack.push [code % name]+args
def initialize &e
@caller = Kernel.caller.first
@exe = e
end
def base_fun *a
@next.call *a
end
alias call base_fun
def end
@next.end
end
def clean
@next.clean
end
def to_proc
method( :call).to_proc
end
end
class Collect <Base
def collect_fun *a
@next.call *@exe.call( *a)
end
alias call collect_fun
end
class Tap <Base
def tap_fun *a
@exe.call *a
@next.call *a
end
alias call tap_fun
end
class Select <Base
def select_fun *a
@next.call *a if @exe.call *a
end
alias call select_fun
end
class Filter <Base
def filter_fun *a
@next.call *a unless @exe.call *a
end
alias call filter_fun
end
class Compact <Base
def compact_fun *a
@next.call *a unless a.empty? || [nil] == a
end
alias call compact_fun
end
class BottomUp <Base
def initialize start, *a, &e
@next.call *a, &e
@buffer, @start = nil, start
end
def bottom_up_fun a
if @exe.call a
@next.call @buffer+a
@buffer = @start.dup
else
@buffer += a
end
end
alias call bottom_up_fun
def end
@next.call @buffer
@next.end
end
end
class TopDown <Base
def initialize start, *a, &e
@next.call *a, &e
@buffer, @start = nil, start
end
def top_down_fun a
if @exe.call a
@next.call @buffer
@buffer = @start.dup+a
else
@buffer += a
end
end
alias call top_down_fun
def end
@next.call @buffer
@next.end
end
end
class Each <Base
def each_fun *a
@exe.call *a
end
alias call each_fun
def end
nil
end
alias :clean :end
end
class P <Each
def initialize *a
super *a, &Kernel.method( :p)
end
end
class Inject <Base
attr_reader :it
alias :end :it
def initialize start, *a, &e
super *a, &e
@it = start
end
def inject_fun *a
@it = @exe.call @it, *a
end
alias call inject_fun
end
class To_a <Inject
def initialize *a, &e
super [], *a, &e
end
end
class Map <Collect
def map_fun *a
@exe.call *a, &@next
end
alias call map_fun
end
class Flatten <Base
def flatten_fun *a
a.each &@next.method( :call)
end
alias call flatten_fun
end
class Reduce <Base
def initialize iv = ::Functional::DEFAULT, *a, &exe
super *a, &exe
iv = Array.method :new if ::Functional::DEFAULT == iv
@buf = if iv.kind_of?( ::Proc) || iv.kind_of?( ::Method)
Hash.new {|h,k| h[k] = iv.call }
else
{}.tap {|h| h.default = iv }
end
end
def reduce_fun k, *a
@buf[ k] = @exe.call k, @buf[ k], *a
end
alias call reduce_fun
def end
@buf.each &@next.method( :call)
@next.end
end
end
class ToHash <Base
def initialize
super
@buf = {}
end
def to_hash_fun k, *a
@buf[k] = a
end
alias call to_hash_fun
def end
@buf
end
def clean
end
end
class Slice <Base
def initialize n
@buf, @n = [], n
end
def slice_fun *a
@buf.push a
unless @n > @buf.size
@next.call @buf
@buf.clear
end
end
alias call slice_fun
def end
@next.call @buf
@next.end
end
end
class Cons <Base
def initialize n
@buf, @n = [], n
end
def cons_fun *a
@buf.push a
unless @n > @buf.size
class <<self
def call *a
@buf.push a
@next.call @buf
@buf.shift
end
end
@next.call @buf
@buf.shift
end
end
alias call cons_fun
def end
@next.call @buf unless @n > @buf.size
@next.end
end
end
class Pager <Base
def initialize *opts
@pager = IO.popen ENV['PAGER'] || 'less', 'w'
opts.each do |opt|
case opt.to_s
when *%w[inspect i] then alias call call_inspect
else raise ArgumentError, "Unknown opt: #{opt}"
end
end
end
def call_inspect *a
@pager.puts a.inspect
end
def pager_fun *a
@pager.puts a
end
alias call pager_fun
def clean
@pager.close
end
def end
clean
nil
end
end
attr_accessor :next, :stack, :obj, :func, :args
def initialize obj = nil, func = nil, *args
@next, @stack, @obj, @func, @args = self, self, obj, func, args
end
def push a
@stack = @stack.next = a
self self
end end
def initialize obj = nil, func = nil, *args
@stack, @obj, @func, @args = [], obj, func, args
end
def collect &exe def collect &exe
push Collect.new( &exe) push_method "value=%s(value)", &exe
end end
# map/reduce?
def map &exe def map &exe
push Map.new( &exe) raise "Reserved for MapReduce."
end end
def reduce iv = ::Functional::DEFAULT, &exe # map/reduce?
push Reduce.new( iv, &exe) def reduce &exe
raise "Reserved for MapReduce."
end end
def select &exe def select &exe
push Select.new( &exe) push_method "%s(value)||next", &exe
end end
def grep re def delete_if &exe
push Select.new( &re.method( :match)) push_method "%s(value)&&next", &exe
end
def filter &exe
push Filter.new( &exe)
end
alias reject filter
def compact
push Compact.new
end
def updown init, &exe
push UpDown.new( init, &exe)
end
def topdown init, &exe
push TopDown.new( init, &exe)
end
def flatten &exe
push Flatten.new
end end
def each &exe def each &exe
return self unless exe return self unless exe
push Each.new( &exe) @obj.send @func || :each, *@args, &eval( "lambda{|value|#{@stack.join( ";")};exe.call(value)}")
run
end
def to_hash
push ToHash.new
run
end
def join deli
push Inject.new('') {|i,j|i+deli+j}
run
end
def run
@obj.send @func||:each, *@args, &@next #.method(:call)
@next.end
rescue Object
@next.clean
raise $!
end
def p
each &Kernel.method( :p)
end
def pager *opts
push Pager.new( *opts)
run
end
def sort &exe
to_a.sort &exe
end
def sort_by &exe
to_a.sort_by &exe
end
# [ _A, _B, ..., _C, ..., _D ] ==> [ [0, _A], [1, _B], ..., [_I, _C], ..., [_N, _D]]
# [ [_A|_As], [_B|_Bs], ..., [_C|_Cs], ..., [_D|_Ds] ] ==> [ [0,_A|_As], [1,_B|_Bs], ..., [_I,_C|_Cs], ..., [_N,_D|_Ds] ]
def with_index &exe
i = 0
exe ||= Array.method :[]
push Collect.new {|*a| exe.call i, *a; i += 1 }
end
def slice n, &exe
push Slice.new( n)
block_given? ? self.collect( &exe) : self
end
def cons n, &exe
push Cons.new( n)
block_given? ? self.collect( &exe) : self
end
def tap &exe
push Tap.new( &exe)
end
class Save < Base
attr_reader :db
def initialize db
@db = db
end
def call k, *v
@db[ k] = v.length == 1 ? v.first : v
end
end
def save db
push Save.new( db)
end end
end end

View file

@ -1,29 +0,0 @@
require 'test/unit'
require 'functional'
class FunTest < Test::Unit::TestCase
M = 0..100
def doit_fun m, &exe
f = m.to_fun
yield f
f.to_a
end
def test_to_fun_exists
assert_respond_to Object, :to_fun
end
def test_to_a
assert_equal M.to_a, doit_fun( M) {|x| x }
end
def test_collect
l = lambda {|x| x*2}
assert_equal M.collect( &l), doit_fun( M) {|x| x.collect( &l) }
end
def test_inject
assert_equal M.inject( 0) {|i,j| i+j }, M.to_fun.inject( 0) {|i,j| i+j }
end
end