module Order ( Order , OrderKind(..) , OrderDirection(..) , init , select ) where import Prelude (class Eq, (==)) import Data.Generic (class Generic, gEq) type Order = { kind :: OrderKind , direction :: OrderDirection } data OrderKind = Name | GlycemicIndex | Carbohydrates | GlycemicLoad derive instance genericOrderKind :: Generic OrderKind instance eqOrderKind :: Eq OrderKind where eq = gEq data OrderDirection = Ascending | Descending derive instance genericOrderDirection :: Generic OrderDirection instance eqOrderDirection :: Eq OrderDirection where eq = gEq init :: Order init = { kind: Name , direction: initDirection } select :: OrderKind -> Order -> Order select kind order = if order.kind == kind then order { direction = otherDirection order.direction } else order { kind = kind, direction = initDirection } otherDirection :: OrderDirection -> OrderDirection otherDirection Ascending = Descending otherDirection Descending = Ascending initDirection :: OrderDirection initDirection = Ascending