Avatar of JaeHyoLee

JaeHyoLee's solution

to Accumulate in the Lua Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Implement the accumulate operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of numbers:

  • 1, 2, 3, 4, 5

And the operation:

  • square a number (x => x * x)

Your code should be able to produce the collection of squares:

  • 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.

Restrictions

Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

accumulate_spec.lua

local accumulate = require('accumulate')

describe('accumulate', function()
  local function square(x) return x * x end

  it('should accumulate over an empty array', function()
    assert.are.same({}, accumulate({}, square))
  end)

  it('should accumulate over an array with a single element', function()
    assert.are.same({ 4 }, accumulate({ 2 }, square))
  end)

  it('should accumulate over an array with several elements', function()
    assert.are.same({ 1, 4, 9 }, accumulate({ 1, 2, 3 }, square))
  end)

  it('should accumulate over an array with a different function', function()
    assert.are.same({ 'HELLO', 'WORLD' }, accumulate({ 'hello', 'world' }, string.upper))
  end)

  it('should not modify the input array', function()
    local input = { 1, 2, 3 }
    accumulate(input, square)
    assert.are.same({ 1, 2, 3 }, input)
  end)
end)
local function accumulate(input, func)
  local output = {}
  for k, v in pairs(input) do
    output[k] = func(input[k])
  end
  return output
end
return accumulate

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

One more tip: you can return the function directly to make things a bit shorter: return function(input, func) local output = {} for k, v in pairs(input) do output[k] = func(input[k]) end return output end

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?