shlogg · Early preview
Augusts Bautra @augustsbautra

Transforming AREL Expressions With Reduce And Inject

AREL expression chaining simplified with reduce/inject: `combo = clean_value.map { expression_proc.call(_1) }.reduce(:and)

I was working on AREL expression chaining and stumbled on an opportunity to use reduce/inject.
This is easy to understand, but verbose, and suffers from treating the first value in a special way:

combo = expression_proc.call(values.first)
values[1..].each { combo = combo.and(expression_proc.call(_1)) }
combo

    
    

    
    




This starts using reduce, but trades clarity for terseness, a downgrade:

combo = expression_proc.call(values.first)
values[1..].reduce(combo) do |mem, clause|
  mem.and(expression_proc.call(clause))
end

    
    

    
    




Let's transform all values to exp...