aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/function/mixin.lux
blob: 57405979dd42a8040889b55869c2f5893a00013f (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
... Inspired by;
... "The Different Aspects of Monads and Mixins" by Bruno C. d. S. Oliveira

(.require
 [library
  [lux (.except)
   [abstract
    [monoid (.only Monoid)]
    [monad (.only Monad do)]]
   [control
    [function
     [predicate (.only Predicate)]]]]])

(type .public (Mixin input output)
  (-> (-> input output) (-> input output)
      (-> input output)))

(def .public (fixed f)
  (All (_ input output)
    (-> (Mixin input output)
        (-> input output)))
  (function (mix input)
    ((f mix mix) input)))

(def .public nothing
  Mixin
  (function (_ next again)
    next))

(def .public (mixed parent child)
  (All (_ input output)
    (-> (Mixin input output) (Mixin input output)
        (Mixin input output)))
  (function (_ next again)
    (parent (child next again) again)))

(def .public monoid
  (All (_ input output)
    (Monoid (Mixin input output)))
  (implementation
   (def identity ..nothing)
   (def composite ..mixed)))

(def .public (advice when then)
  (All (_ input output)
    (-> (Predicate input) (Mixin input output)
        (Mixin input output)))
  (function (_ next again input)
    (if (when input)
      ((then next again) input)
      (next input))))

(def .public (before ! action)
  (All (_ ! input output)
    (-> (Monad !) (-> input (! Any))
        (Mixin input (! output))))
  (function (_ next again input)
    (do !
      [_ (action input)]
      (next input))))

(def .public (after ! action)
  (All (_ ! input output)
    (-> (Monad !) (-> input output (! Any))
        (Mixin input (! output))))
  (function (_ next again input)
    (do !
      [output (next input)
       _ (action input output)]
      (in output))))

(type .public (Recursive input output)
  (-> (-> input output)
      (-> input output)))

(def .public (of_recursive recursive)
  (All (_ input output)
    (-> (Recursive input output)
        (Mixin input output)))
  (function (_ next again)
    (recursive again)))