Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Accumulate in the CoffeeScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Refer to the Exercism CoffeScript page for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:

jasmine-node --coffee .

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_test.spec.coffee

require './accumulate'

describe '[].accumulate()', ->

  it 'empty accumulation', ->
    accumulator = (e) -> e * e
    expect([].accumulate(accumulator)).toEqual []

  xit 'accumulate squares', ->
    accumulator = (number) -> number * number
    expect([1, 2, 3].accumulate accumulator).toEqual [1, 4, 9]

  xit 'accumulate upcases', ->
    accumulator = (word) -> word.toUpperCase()
    result      = 'hello world'.split(/\s/).accumulate accumulator

    expect(result).toEqual ['HELLO', 'WORLD']

  xit 'accumulate reversed strings', ->
    accumulator = (word) -> word.split('').reverse().join('')
    result      = 'the quick brown fox etc'.split(/\s/).accumulate accumulator

    expect(result).toEqual ["eht", "kciuq", "nworb", "xof", "cte"]

  xit 'accumulate recursively', ->
    result = 'a b c'.split(/\s+/).accumulate (char)  ->
      '1 2 3'.split(/\s+/).accumulate (digit) -> "#{char}#{digit}"

    expect(result).toEqual([["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]])
Array::accumulate = (accumulator) ->
  for i in this
    i = accumulator i

Community comments

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

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?