 # rootulp's solution

## to Strain in the TypeScript 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 `keep` and `discard` operation on collections. Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

• 1, 2, 3, 4, 5

And the predicate:

• is the number even?

Then your keep operation should produce:

• 2, 4

• 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language.

## Restrictions

Keep your hands off that filter/reject/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:

http://exercism.io/languages/typescript

## Requirements

Install assignment dependencies:

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

## Making the test suite pass

Execute the tests with:

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

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

### strain.test.ts

``````import {keep, discard} from './strain'

describe('strain', () => {
it('keeps on empty array returns empty array', () => {
expect(keep<number>([], (e: number) => e < 10)).toEqual([])
})

xit('keeps everything ', () => {
expect(keep<number>([1, 2, 3], (e: number) => e < 10)).toEqual([1, 2, 3])
})

xit('keeps first and last', () => {
expect(keep<number>([1, 2, 3], (e: number) => e % 2 === 1)).toEqual([1, 3])
})

xit('keeps neither first nor last', () => {
expect(keep<number>([1, 2, 3, 4, 5], (e: number) => e % 2 === 0)).toEqual([2, 4])
})

xit('keeps strings', () => {
const words = 'apple zebra banana zombies cherimoya zelot'.split(' ')
const result = keep<string>(words, (word: string) => word.indexOf('z') === 0)
expect(result).toEqual('zebra zombies zelot'.split(' '))
})

xit('keeps arrays', () => {
const rows = [
[1, 2, 3],
[5, 5, 5],
[5, 1, 2],
[2, 1, 2],
[1, 5, 2],
[2, 2, 1],
[1, 2, 5],
]
const result = keep<number[]>(rows, (row: number[]) => row.indexOf(5) > -1)
expect(result).toEqual([[5, 5, 5], [5, 1, 2], [1, 5, 2], [1, 2, 5]])
})

expect(discard<number>([], (e: number) => e < 10)).toEqual([])
})

expect(discard<number>([1, 2, 3], (e: number) => e > 10)).toEqual([1, 2, 3])
})

xit('discards first and last', () => {
expect(discard<number>([1, 2, 3], (e: number) => e % 2 === 1)).toEqual()
})

xit('discards neither first nor last', () => {
const result = discard<number>([1, 2, 3, 4, 5], (e: number) => e % 2 === 0)
expect(result).toEqual([1, 3, 5])
})

const words = 'apple zebra banana zombies cherimoya zelot'.split(' ')
const result = discard<string>(words, (word: string) => word.indexOf('z') === 0)
expect(result).toEqual('apple banana cherimoya'.split(' '))
})

const rows = [
[1, 2, 3],
[5, 5, 5],
[5, 1, 2],
[2, 1, 2],
[1, 5, 2],
[2, 2, 1],
[1, 2, 5],
]
const result = discard<number[]>(rows, (row: number[]) => row.indexOf(5) > -1)
expect(result).toEqual([[1, 2, 3], [2, 1, 2], [2, 2, 1]])
})
})``````
``````export function keep<T>(collection: T[], predicate: (element: T)  => boolean ): T[] {
const kept: T[] = []
collection.forEach((element) => {
if (predicate(element)) {
kept.push(element)
}
})
return kept
}

export function discard<T>(collection: T[], predicate: (element: T)  => boolean ): T[] {
collection.forEach((element) => {
if (!predicate(element)) {
}
})
}``````