exercism/elixir/grade-school/school.exs

38 lines
713 B
Elixir

defmodule School do
@moduledoc """
Simulate students in a school.
Each student is in a grade.
"""
@doc """
Add a student to a particular grade in school.
"""
@spec add(map, String.t(), integer) :: map
def add(db, name, grade) do
update_in(db, [grade], fn i ->
case i do
nil -> [name]
_ -> [name | i]
end
|> Enum.sort()
end)
end
@doc """
Return the names of the students in a particular grade.
"""
@spec grade(map, integer) :: [String.t()]
def grade(db, grade) do
db[grade] || []
end
@doc """
Sorts the school by grade and name.
"""
@spec sort(map) :: [{integer, [String.t()]}]
def sort(db) do
Enum.sort(db)
end
end