Avatar of McEileen

McEileen's solution

to Strain in the TypeScript Track

Published at Sep 30 2019 · 0 comments
Instructions
Test suite
Solution

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

While your discard operation should produce:

  • 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]])
  })

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

  it('discards nothing', () => {
    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([2])
  })

  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])
  })

  xit('discards strings', () => {
    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(' '))
  })

  xit('discards 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 = 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>(input: T[], fn: (x: T) => boolean): T[] {
    let output: T[] = []
    input.forEach(elem =>{
        if (fn(elem)) {
            output.push(elem)
        }
    })
    return output
}

export function discard<T>(input: T[], fn: (x: T) => boolean): T[] {
    let output: T[] = []
    input.forEach(elem =>{
        if (!fn(elem)) {
            output.push(elem)
        }
    })
    return output
}

Community comments

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

McEileen's Reflection

Feedback on style would be appreciated. I'm getting back into TypeScript after not using it for about a year.