aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/function/mixin.lux
blob: 357fdb7c1567aabb47136a5a27858e446ab2514a (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
55
56
57
58
59
60
61
62
63
64
... Inspired by;
... "The Different Aspects of Monads and Mixins" by Bruno C. d. S. Oliveira

(.module:
  [library
   [lux #*
    [abstract
     [monoid (#+ Monoid)]
     [predicate (#+ Predicate)]
     [monad (#+ Monad do)]]]])

(type: .public (Mixin i o)
  (-> (-> i o) (-> i o) (-> i o)))

(def: .public (fixed f)
  (All (_ i o) (-> (Mixin i o) (-> i o)))
  (function (mix input)
    ((f mix mix) input)))

(def: .public nothing
  Mixin
  (function (_ delegate recur)
    delegate))

(def: .public (mixed parent child)
  (All (_ i o) (-> (Mixin i o) (Mixin i o) (Mixin i o)))
  (function (_ delegate recur)
    (parent (child delegate recur) recur)))

(implementation: .public monoid
  (All (_ i o) (Monoid (Mixin i o)))

  (def: identity ..nothing)
  (def: composite ..mixed))

(def: .public (advice when then)
  (All (_ i o) (-> (Predicate i) (Mixin i o) (Mixin i o)))
  (function (_ delegate recur input)
    (if (when input)
      ((then delegate recur) input)
      (delegate input))))

(def: .public (before monad action)
  (All (_ ! i o) (-> (Monad !) (-> i (! Any)) (Mixin i (! o))))
  (function (_ delegate recur input)
    (do monad
      [_ (action input)]
      (delegate input))))

(def: .public (after monad action)
  (All (_ ! i o) (-> (Monad !) (-> i o (! Any)) (Mixin i (! o))))
  (function (_ delegate recur input)
    (do monad
      [output (delegate input)
       _ (action input output)]
      (in output))))

(type: .public (Recursive i o)
  (-> (-> i o) (-> i o)))

(def: .public (of_recursive recursive)
  (All (_ i o) (-> (Recursive i o) (Mixin i o)))
  (function (_ delegate recur)
    (recursive recur)))