ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# SergiiVlasiuk's solution

## to Strain in the Scala Track

Published at Aug 23 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

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

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

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

### StrainTest.scala

``````import org.scalatest.{Matchers, FlatSpec}

/** @version created manually **/
class StrainTest extends FlatSpec with Matchers {

it should "handle empty keep" in {
val result = Strain.keep[Int](List(), x => x < 10)
result should be(List.empty)
}

it should "keep everything" in {
pending
val expectedList = List("a", "b", "c")
val result = Strain.keep[String](expectedList, _ => true)
result should be(expectedList)
}

it should "keep first and last" in {
pending
val result = Strain.keep[Int](List(1, 2, 3), x => x % 2 != 0)
result should be(List(1, 3))
}

it should "keep neither first or last" in {
pending
val result = Strain.keep[Int](List(1, 2, 3, 4, 5), x => x % 2 == 0)
result should be(List(2, 4))
}

it should "keep strings" in {
pending
val words = List("apple",  "zebra", "banana", "zombies", "cherimoya",  "zelot")
val result = Strain.keep[String](words, s => s.startsWith("z"))
result should be(List("zebra", "zombies", "zelot"))
}

it should "keep sequences" in {
pending
val sequences = Seq(Seq(1, 2, 3), Seq(5, 5, 5), Seq(5, 1, 2), Seq(2, 1, 2),
Seq(1, 5, 2), Seq(2, 2, 1), Seq(1, 2, 5))
val expected = Seq(Seq(5, 5, 5), Seq(5, 1, 2), Seq(1, 5, 2), Seq(1, 2, 5))
val result = Strain.keep[Seq[Int]](sequences, seq => seq.contains(5))
result should be (expected)
}

it should "handle empty discard" in {
pending
val result = Strain.discard[Int](List(), x => x < 10)
result should be (List.empty)
}

it should "discard first and last" in {
pending
val result = Strain.discard[Int](List(1, 2, 3), x => x % 2 != 0)
result should be (List(2))
}

it should "discard neither first or last" in {
pending
val result = Strain.discard[Int](List(1, 2, 3, 4, 5), x => x % 2 == 0)
result should be (List(1, 3, 5))
}

it should "discard strings" in {
pending
val words = List("apple",  "zebra", "banana", "zombies", "cherimoya",  "zelot")
val result = Strain.discard[String](words, s => s.startsWith("z"))
result should be (List("apple", "banana", "cherimoya"))
}

it should "discard sequences" in {
pending
val sequences = Seq(Seq(1, 2, 3), Seq(5, 5, 5), Seq(5, 1, 2), Seq(2, 1, 2),
Seq(1, 5, 2), Seq(2, 2, 1), Seq(1, 2, 5))
val expected = Seq(Seq(1, 2, 3), Seq(2, 1, 2), Seq(2, 2, 1))
val result = Strain.discard[Seq[Int]](sequences, seq => seq.contains(5))
result should be (expected)
}
}``````
``````object Strain {
def keep[T](input: Seq[T], pred: T => Boolean): List[T] = input.filter(pred).toList

def discard[T](input: Seq[T], pred: T => Boolean): List[T] = keep(input, (x: T) => !pred(x))
}``````