Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
Last active February 27, 2018 12:56

Revisions

  1. vasily-kirichenko revised this gist Feb 27, 2018. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions CraftingCode.scala
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,17 @@ def sortedKeyList(xs: List[List[Person]]) = {
    }
    }

    def `sorted key list`() = {
    def people(xs: List[String]) = xs.map(Person(_, flag = true))
    class Tests extends FunSuite with Matchers {
    test("sorted key list") {
    def people(xs: List[String]) = xs.map(Person(_, flag = true))

    val data = List(
    (Nil, Nil, "<none>"),
    (List("fred"), List("barney", "wilma"), "barney, fred, wilma"),
    (List("fred"), Nil, "fred"),
    (List("fred"), List("fred", "barney"), "barney, fred"))
    val data = List(
    (Nil, Nil, "<none>"),
    (List("fred"), List("barney", "wilma"), "barney, fred, wilma"),
    (List("fred"), Nil, "fred"),
    (List("fred"), List("fred", "barney"), "barney, fred"))

    data.foreach { case (a, b, res) =>
    sortedKeyList(List(people(a), people(b))).equals(res)
    for ((a, b, res) <- data)
    sortedKeyList(List(people(a), people(b))) should equal(res)
    }
    }
  2. vasily-kirichenko created this gist Feb 27, 2018.
    26 changes: 26 additions & 0 deletions CraftingCode.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    case class Person(name: String, flag: Boolean)

    def sortedKeyList(xs: List[List[Person]]) = {
    xs.flatten
    .map(_.name)
    .distinct
    .sortBy(identity)
    .mkString(", ") match {
    case "" => "<none>"
    case s => s
    }
    }

    def `sorted key list`() = {
    def people(xs: List[String]) = xs.map(Person(_, flag = true))

    val data = List(
    (Nil, Nil, "<none>"),
    (List("fred"), List("barney", "wilma"), "barney, fred, wilma"),
    (List("fred"), Nil, "fred"),
    (List("fred"), List("fred", "barney"), "barney, fred"))

    data.foreach { case (a, b, res) =>
    sortedKeyList(List(people(a), people(b))).equals(res)
    }
    }