require 'facets/more/functor' module Enumerable # Returns an elementwise Functor. This allows # you to map a method on to every element. # # [1,2,3].every + 3 #=> [4,5,6] # # ['a','b','c'].every.upcase #=> ['A','B','C'] # def every Functor.new do |op,*args| self.collect{ |a| a.send(op,*args) } end end end class Array # In place version of #every. def every! Functor.new do |op,*args| self.replace( self.collect{ |a| a.send(op,*args) } ) end end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCEnumerable < Test::Unit::TestCase def test_every_1 a = [1,2,3] assert_equal( [4,5,6], a.every + 3 ) assert_equal( [0,1,2], a.every - 1 ) assert_equal( ['1','2','3'], a.every.to_s ) end def test_every_2 a = [1,2,3] a.every! + 3 assert_equal( [4,5,6], a ) end end =end