From a5b273033b849e37e87899a374101d3562098d39 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 23 Jun 2010 17:01:01 +0200 Subject: [PATCH] more example in README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d625a86..c8db4a8 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,45 @@ Usage require 'functional' - obj = 0 .. 10**12 - Functional.new( obj).select {|i| i.even? }.collect {|i| i/3 }.select {|i| i.even? }.each &method( :puts) + # 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 + 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 What's with _#map_? =================