aboutsummaryrefslogtreecommitdiff
path: root/src/Order.purs
blob: 33c2be9c82fef7f420c9d4f9ffa82f401a6c46f2 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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