{{ message }}

# tomstuart / nothing

Extract INJECT from SUM and PRODUCT

```def inject(l, x, &block)
if l.empty?
x
else
inject(l[1..-1], block.call(x, l.first), &block)
end
end```
tomstuart committed Oct 28, 2011
1 parent 0908d77 commit 5e10048b4256085f554cafc7e379959cfbe5a71b
Showing with 4 additions and 2 deletions.
1. +4 −2 lib/nothing.rb
 @@ -56,9 +56,11 @@ module Nothing FIRST = -> l { LEFT[RIGHT[l]] } REST = -> l { RIGHT[RIGHT[l]] } INJECT = Z[-> f { -> l { -> x { -> g { IF[IS_EMPTY[l]][x][-> _ { f[REST[l]][g[x][FIRST[l]]][g][_] }] } } } }] RANGE = Z[-> f { -> m { -> n { IF[IS_LESS_OR_EQUAL[m][n]][-> _ { UNSHIFT[f[INCREMENT[m]][n]][m][_] }][EMPTY] } } }] SUM = Z[-> f { -> l { IF[IS_EMPTY[l]][ZERO][-> _ { ADD[FIRST[l]][f[REST[l]]][_] }] } }] PRODUCT = Z[-> f { -> l { IF[IS_EMPTY[l]][ONE][-> _ { MULTIPLY[FIRST[l]][f[REST[l]]][_] }] } }] SUM = -> l { INJECT[l][ZERO][ADD] } PRODUCT = -> l { INJECT[l][ONE][MULTIPLY] } # CONCAT = # PUSH = # REVERSE =

### cstrahan replied Apr 1, 2012

 Sweet. Are there any convenient combinators that could be used to write `SUM` and `PRODUCT` point-free?

### tomstuart replied Apr 2, 2012

 Actually, `SUM` and `PRODUCT` are already point-free when their arguments are taken in the conventional (vs the Ruby-friendly) order; on the `pedant` branch you can see how reordering `INJECT`'s arguments allows you to eta-contract `SUM` and `PRODUCT` into point-free form.