src/main/scala/Garden.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
object Plant extends Enumeration {
  val Grass, Clover, Violets, Radishes = Value

  val Abbreviations = Map(
    'G' -> Grass,
    'C' -> Clover,
    'V' -> Violets,
    'R' -> Radishes
  )
}

object Children {
  type Name = String
  type Names = List[Name]

  val Default = List(
    "Alice", "Bob", "Charlie", "David", "Eve", "Fred",
    "Ginny", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry"
  )
}

object Garden {
  type Layout = String

  def defaultGarden(layout: Layout) = new Garden(Children.Default, layout)
  def apply(children: Children.Names, layout: Layout) = new Garden(children.sorted, layout)
}

class Garden(children: Children.Names, layout: Garden.Layout) {
  val rows = layout.split("\n")

  private def childOffset(name: Children.Name) = children.indexOf(name) * 2
  private def plantsAt(offset: Int) =
    rows.flatMap { row => row.slice(offset, offset+2) }

  def getPlants(name: Children.Name) =
    plantsAt(childOffset(name)).map(Plant.Abbreviations)
}

@abo64 thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment