Skip to content

Instantly share code, notes, and snippets.

@zzak
Forked from headius/proc_composition.rb
Created September 17, 2010 16:03
Show Gist options
  • Select an option

  • Save zzak/584438 to your computer and use it in GitHub Desktop.

Select an option

Save zzak/584438 to your computer and use it in GitHub Desktop.

Revisions

  1. @headius headius revised this gist Sep 17, 2010. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions proc_composition.rb
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,8 @@ def >>(other)
    end
    end

    times6 = ->(i){ i * 2 } << ->(i){ i * 3}
    times10 = ->(i){ i * 2} >> ->(i){ i + 1}
    times6 = ->(i){ i * 2 } << ->(i){ i * 3 }
    times2plus1 = ->(i){ i * 2} >> ->(i){ i + 1 }

    puts times6.call(3)
    puts times10.call(3)
    puts times2plus1.call(3)
  2. @headius headius created this gist Sep 17, 2010.
    25 changes: 25 additions & 0 deletions proc_composition.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    class Proc
    def <<(other)
    case other
    when Proc
    Proc.new do |*args|
    call(other.call(*args))
    end
    else
    call(other)
    end
    end

    def >>(other)
    raise TypeError unless Proc === other
    Proc.new do |*args|
    other.call(call(*args))
    end
    end
    end

    times6 = ->(i){ i * 2 } << ->(i){ i * 3}
    times10 = ->(i){ i * 2} >> ->(i){ i + 1}

    puts times6.call(3)
    puts times10.call(3)