Created
January 20, 2010 00:05
-
-
Save Maciek416/281455 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| irb(main):001:0> # Builds a function which returns true | |
| irb(main):002:0* # whenever _every_ function in 'predicates' | |
| irb(main):003:0* # returns true. | |
| irb(main):004:0* def conjoin *predicates | |
| irb(main):005:1> base = lambda {|*args| true } | |
| irb(main):006:1> predicates.inject(base) do |built, pred| | |
| irb(main):007:2* lambda do |*args| | |
| irb(main):008:3* built.call(*args) && pred.call(*args) | |
| irb(main):009:3> end | |
| irb(main):010:2> end | |
| irb(main):011:1> end | |
| => nil | |
| irb(main):012:0> is_awesome_number = conjoin(lambda {|x| x > 10}, lambda {|x| x % 2 == 0}) | |
| => #<Proc:0x001690cc@(irb):7> | |
| irb(main):013:0> is_awesome_number.call(22) | |
| => true | |
| irb(main):014:0> is_awesome_number.call(23) | |
| => false | |
| irb(main):015:0> is_awesome_number.call(8) | |
| => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment