I'm storing some decision-making data in arrays that look like: [condition, result, ..., condition, result, default], so basically ternary expressions (c ? r : ... c ? r : d) and I'm evaluating them as such with this piece of code:
class Array
def ternary &block
# the block checks if a condition is true
i = 0
while true
if i % 2 == 1 or i == length - 1
return self[i]
else
i += yield(self[i]) ? 1 : 2
end
end
end
end
['false', 0, 'true', 1, 'true', 2, 3].ternary {|i| i == 'true'}
# => 1
[1, 2, 3, 4, 5].ternary {|i| i > 6}
# => 5 (defaults to last value because all conditions failed)
I'm wondering if there's a faster built-in way of doing this or how I can improve this code.
Note: there are arbitrarily many conditions and [only_possible_answer] should also work
EDIT: The answers so far (tested over 1 000 000 iterations of the same array):
Setup
flat = ['false', 0, 'false', 1, 'false', 2, 'false', 3, 'false', 4, 'true', 5, 6]
nested = [['false', 0], ['false', 1], ['false', 2], ['false', 3], ['false', 4], ['true', 5], [6]]
Option = Struct.new :condition, :result
Default = Struct.new :result
class Default
def call; self; end
# otherwise:
# undefined method ‘call’ for #<struct Default result=whatever>
end
options = [Option.new('false', 0), Option.new('false', 1), Option.new('false', 2), Option.new('false', 3), Option.new('false', 4), Option.new('true', 5)]
class Array
def ternary_flat_slow
i = 0
while true
if i % 2 == 1 or i == length - 1
return self[i]
else
i += yield(self[i]) ? 1 : 2
end
end
end
def ternary_flat_fast # by @illusionist
index = 0
index += 2 until (index >= self.length - 1) || yield(self[index])
return self[index+1] unless index == self.length - 1
self.last
end
def ternary_nested
find {|i| i.length == 1 or yield i[0]} .last
end
def ternary_options default # by @ReinHenrichs
find(default) {|i| yield i} .result
end
def case_when_then_else(&block) # by @Amadan
each_slice(2).find { |x|
x.size == 1 || (block_given? ? block[x[0]] : x[0])
}&.last
end
end
require 'benchmark'
Benchmark.bmbm(9) do |i|
i.report('flat slow') { 1000000.times { flat.ternary_flat_slow {|con| con == 'true' }}}
i.report('flat fast') { 1000000.times { flat.ternary_flat_fast {|con| con == 'true' }}}
i.report(' nested') { 1000000.times { nested.ternary_nested {|con| con == 'true' }}}
i.report(' options') { 1000000.times { options.ternary_options(Default.new(6)) {|con| con == 'true' }}}
i.report(' c_w_t_e') { 1000000.times { flat.case_when_then_else {|con| con == 'true' }}}
end
Results
Rehearsal ---------------------------------------------
flat slow 4.510000 0.030000 4.540000 ( 4.549424) # original
flat fast 3.600000 0.030000 3.630000 ( 3.656058) # @illusionist
nested 6.920000 0.080000 7.000000 ( 7.252300) # me (as suggested)
options 7.030000 0.050000 7.080000 ( 7.130508) # @ReinHenrichs
c_w_t_e 19.320000 0.140000 19.460000 ( 19.593633) # @Amadan
----------------------------------- total: 41.710000sec
user system total real
flat slow 4.290000 0.030000 4.320000 ( 4.339875) # original
flat fast 3.360000 0.020000 3.380000 ( 3.401809) # @illusionist
nested 6.180000 0.040000 6.220000 ( 6.258939) # me (as suggested)
options 6.640000 0.040000 6.680000 ( 6.704991) # @ReinHenrichs
c_w_t_e 18.340000 0.120000 18.460000 ( 18.548176) # @Amadan
However "unrubyish" it is, @illusionist's answer is the fastest and speed is a primary concern
Aucun commentaire:
Enregistrer un commentaire