Skip to content

_.combine #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ <h3>Iterator Functions</h3>
<li>
<a href='#map'>map</a>
</li>
<li>
<a href='#combine'>combine</a>
</li>
<li>
<a href='#each'>each</a>
</li>
Expand Down Expand Up @@ -222,6 +225,14 @@ <h3>Iterator Functions</h3>
<p class='description'>Produces a new array by mapping each value in iter through a transformation function.</p>
<pre class='example'>_.map({1,2,3}, function(i) return i*2 end)&#x000A;=> { 2,4,6 }</pre>
</div>
<div class='function' id='combine'>
<p>
<strong class='name'>combine</strong>
<code>_.combine(iters, func)</code>
</p>
<p class='description'>Produces a new array by mapping each value in each array in iters through a transformation function.</p>
<pre class='example'>_.combine({ {1,2,3}, {3,2,1}, {1,1,1} }, function(a,b,c) a+b+c end)&#x000A;=> { 5,5,5 }&#x000A;_.combine({ {3,2,1}, {1,1,1,1,1} }, function(a,b) a+b end)&#x000A;=> { 4,3,2 }</pre>
</div>
<div class='function' id='each'>
<p>
<strong class='name'>each</strong>
Expand Down
12 changes: 12 additions & 0 deletions lib/underscore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ function Underscore.funcs.map(list, func)
return mapped
end

function Underscore.funcs.combine(list, func)
local mapped = {}
local remaining_lists = _.map(_.rest(list), Underscore.iter)
for v1 in Underscore.iter(list[1]) do
local remaining_vals = _.map(remaining_lists, function (x)
return x()
end)
mapped[#mapped+1] = func(v1, unpack(remaining_vals))
end
return mapped
end

function Underscore.funcs.reduce(list, memo, func)
for i in Underscore.iter(list) do
memo = func(memo, i)
Expand Down
15 changes: 15 additions & 0 deletions spec/combine_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe["_.combine"] = function()
before = function()
input = { {1,2,3}, {3,2,1,0}, {1,1,1,1,1} }
result = _.combine(input, function(a,b,c) return a+b+c end)
end

it["should return an array of size of the first array with the elements of all arrays transformed by the function"] = function()
expect(result).should_equal {5,5,5}
end
end


spec:report(true)
12 changes: 12 additions & 0 deletions template/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ iterator_functions:
_.map({1,2,3}, function(i) return i*2 end)
=> { 2,4,6 }

- name:
combine
params:
iters, func
description:
Produces a new array by mapping each value in each array in iters through a transformation function.
example: |
_.combine({ {1,2,3}, {3,2,1}, {1,1,1} }, function(a,b,c) a+b+c end)
=> { 5,5,5 }
_.combine({ {3,2,1}, {1,1,1,1,1} }, function(a,b) a+b end)
=> { 4,3,2 }

- name:
each
aliases:
Expand Down