Request:
I would like to be able to do
array.inject(:or) and array.inject(:and)
Reason:
If I have an array of integers:
a=[2,3,4,5,6,7]
inject/reduce works with all of these operators:
a.inject( :+ | :- | :* | : / | :** | :^ | :& | :| )
Currently, for an array b of booleans (true, false/nil) you CANNOT do:
b.inject(:or | :and).
Use Cases:
In alot of my mathematical algorithmic programming things like this
occur:
residues = [2, 3, 4, 5, 6, 7]
{do something} if ( s=false; residues.each {|r| s ||= n%(s) == 0}; s)
or
{do something} if residues.map {|r| n%r }.include? 0
or
{do something} if residues].any? {|r| n%r == 0 }
These perform faster depending on the the Ruby VM (MRI,JRuby,or
Rubinius).
It would be nicer (and hopefully consistently faster) to be able to do:
{do something} if residues].map {|r| n%r == 0 }.inject(:or)
or
{do something} if residues].inject(:or) {|r| n%r == 0 }
so that in this example the conditional returns 'true' when the first
instance of {|r| n%r == 0 } becomes true just as if I wrote it out
explicitly like:
{do something} if (n%r1 == 0 or n%r2 == 0 or ... n%rn == 0)
which provides the best performance on all VMs.
But as array sizes grows this isn't feasible to code for the general
case, so I do the above techniques with variable sized arrays, which are
slower than if written out explicitly.
on 2012-12-03 23:18
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.