aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/enum.lux
blob: d1fec7f7f528c77ff5161a13b8884a25e3f98201 (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
(.module:
  [library
   [lux #*]]
  [//
   ["." order (#+ Order)]])

(interface: .public (Enum e)
  {#.doc "Enumerable types, with a notion of moving forward and backwards through a type's instances."}
  (: (Order e) &order)
  (: (-> e e) succ)
  (: (-> e e) pred))

(def: .public (range enum from to)
  {#.doc "An inclusive [from, to] range of values."}
  (All [a] (-> (Enum a) a a (List a)))
  (let [(^open "/\.") enum]
    (loop [end to
           output #.End]
      (cond (/\< end from)
            (recur (/\pred end) (#.Item end output))

            (/\< from end)
            (recur (/\succ end) (#.Item end output))

            ## (/\= end from)
            (#.Item end output)))))