🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 ## to Accumulate in the TypeScript Track

Published at Jun 18 2020 · 0 comments
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.

## Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xit` to `it`.

## 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.ts

``````import accumulate from './accumulate'

describe('accumulate()', () => {
it('accumulation empty', () => {
const accumulator = (e: number): number => e * e
expect(accumulate([], accumulator)).toEqual([])
})

it('accumulate squares', () => {
const accumulator = (n: number): number => n * n
const result = accumulate([1, 2, 3], accumulator)
expect(result).toEqual([1, 4, 9])
})

it('accumulate upcases', () => {
const accumulator = (word: string): string => word.toUpperCase()
const result = accumulate('hello world'.split(/\s/), accumulator)
expect(result).toEqual(['HELLO', 'WORLD'])
})

it('accumulate reversed strings', () => {
const accumulator = (word: string): string => word.split('').reverse().join('')
const result = accumulate('the quick brown fox etc'.split(/\s/), accumulator)
expect(result).toEqual(['eht', 'kciuq', 'nworb', 'xof', 'cte'])
})

it('accumulate recursively', () => {
const result = accumulate('a b c'.split(/\s/), (char: string) => accumulate('1 2 3'.split(/\s/), (digit: string) => char + digit))

expect(result).toEqual([['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']])
})
})``````
``````export default function accumulate<T, U>(
inputData: T[],
func: (x: T) => U
): U[] {
return inputData.map(func);
}``````