Avatar of vjba

vjba's solution

to Strain in the TypeScript Track

Published at Oct 12 2019 · 2 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:

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.

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>(array: T[], callback: Function): T[] {
    let ret: T[] = []
    array.forEach(el => {
        if (callback(el)) { ret.push(el) }
    })
    return ret
}

export function discard<T>(array: T[], callback: Function): T[] {
    let ret: T[] = []
    array.forEach(el => {
        if (!callback(el)) { ret.push(el) }
    })
    return array.filter(el => !callback(el))
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of vjba
Solution Author
commented 61 days ago

Woops... I didn't see this.

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library!

Avatar of vjba
Solution Author
commented 61 days ago

Resolved in iteration 2

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?