Skip to content

Commit 9dace60

Browse files
committed
New implementation of variadic functions.
1 parent 1b0f830 commit 9dace60

File tree

4 files changed

+138
-123
lines changed

4 files changed

+138
-123
lines changed

stdlib/source/library/lux/control/filter.lux

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
["//" effect]]
1010
["[0]" monad (.only Monad)
1111
["/" free]]]
12-
["[0]" function (.only)
12+
[function
1313
[predicate (.only Predicate)]]
1414
[data
1515
[collection
@@ -48,13 +48,13 @@
4848
(these (the .public read
4949
(for_any (_ read yield)
5050
(Filter read yield read))
51-
{/.#Impure {#Read [[] (|>> {/.#Pure})]}})
51+
{/.#Impure {#Read [[] (by ..monad pure)]}})
5252

5353
(the .public (yield it)
5454
(for_any (_ read yield)
5555
(-> yield
5656
(Filter read yield Any)))
57-
{/.#Impure {#Yield [[it] (function.constant {/.#Pure []})]}})
57+
{/.#Impure {#Yield [[it] (by ..monad pure)]}})
5858

5959
(the .public (stream filter it)
6060
(for_any (_ read yield it)

stdlib/source/library/lux/control/stream.lux

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
(when it
6161
{/.#Impure [item after]}
6262
(if (? item)
63-
{/.#Impure [item (|>> after (only ?))]}
64-
(only ? (after [])))
63+
{/.#Impure [item (|>> after (only ?))]}
64+
(only ? (after [])))
6565

6666
{/.#Pure it}
6767
{/.#Pure it}))
@@ -88,7 +88,7 @@
8888
(for_any (_ it)
8989
(-> it
9090
(Stream it Any)))
91-
{/.#Impure [[it] (function.constant {/.#Pure []})]})
91+
{/.#Impure [[it] (by ..monad pure)]})
9292

9393
(the .public many
9494
(for_any (_ it)

stdlib/source/library/lux/function/variadic.lux

+107-62
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,115 @@
44
... https://en.wikipedia.org/wiki/Variadic_function
55
(.using
66
[library
7-
[lux (.except the)
7+
[lux (.except argument ->)
88
[abstract
99
["?" projection]
10-
["[0]" monad]]
11-
[error
12-
["[0]" exception (.only Exception)]]
10+
[functor
11+
["/" effect]]]
1312
[data
14-
["[0]" text (.only)
15-
["%" \\injection]]
1613
[collection
1714
["[0]" list (.use "[1]#[0]" monad)
18-
["?[1]" \\projection]]
19-
["[0]" set]]]
20-
[math
21-
[number
22-
[/64
23-
["n" natural]]]]
24-
[macro (.only with_names)
25-
["[0]" syntax (.only)
26-
["[0]" export]]]
27-
["[0]" meta (.only)
28-
["[0]" module]
29-
["[0]" name]
30-
["[0]" code (.only)
31-
["?[1]" \\projection]]]]])
32-
33-
(exception.the .public (duplicate_parameters [definition parameters])
34-
(Exception [Name (List Text)])
35-
(exception.report
36-
(list ["Definition" (name.as_text definition)]
37-
["Parameters" (list.as_text %.text parameters)])))
38-
39-
(exception.the .public (must_have_rest_parameter [definition])
40-
(Exception Name)
41-
(exception.report
42-
(list ["Definition" (name.as_text definition)])))
43-
44-
(syntax.the .public (the [[exported? [name parameters] type body]
45-
(export.with (all ?.and
46-
(?code.form (?.and ?code.local (?.some ?code.local)))
47-
?list.any
48-
?list.any))])
49-
(monad.let meta.monad
50-
[[here _] module.current]
51-
(if (n.= (list.size parameters)
52-
(set.size (set.of_list text.hash parameters)))
53-
(with_names ['function]
54-
(when (list.split 1 (list.in_reverse parameters))
55-
[(list &rest) mandatory]
56-
(let [mandatory (list#each code.local (list.in_reverse mandatory))
57-
&rest (code.local &rest)]
58-
(pure (list (` (.the (, exported?) ((, 'function) (,* mandatory) (, &rest))
59-
(, type)
60-
(, body)))
61-
(` (syntax.the (, exported?) ((, (code.local name)) [(,* (|> mandatory
62-
(list#each (function (_ parameter)
63-
(list parameter (` ?list.any))))
64-
list#conjoint))
65-
(, &rest) (?.some ?list.any)])
66-
(by meta.monad (,' pure)
67-
(list (` ((, 'function)
68-
(,* (list#each (|>> , ((,' .,)) `) mandatory))
69-
(list ((,' .,*) (, &rest))))))))))))
70-
71-
_
72-
(meta.failure (exception.error ..must_have_rest_parameter [[here name]]))))
73-
(meta.failure (exception.error ..duplicate_parameters [[here name] parameters])))))
15+
["?[1]" \\projection]]]]
16+
[macro
17+
["[0]" syntax]
18+
["[0]" expansion]]]]
19+
["[0]" // (.only)
20+
[poly
21+
["[0]" type]]])
22+
23+
(every (Return return)
24+
(/.Effect [] return))
25+
26+
(every (Call' Argument
27+
argument return)
28+
(all /.Or
29+
(Return return)
30+
(Argument argument return)
31+
))
32+
33+
(every (->' Call' Argument
34+
argument return
35+
it)
36+
(.-> (Call' Argument argument return it)
37+
it))
38+
39+
(every (Argument argument return)
40+
(/.Effect argument (->' Call' Argument argument return)))
41+
42+
(every Call
43+
(Call' ..Argument))
44+
45+
(every .public ->
46+
(->' ..Call' ..Argument))
47+
48+
(the (application function argument)
49+
(for_any (_ argument return it)
50+
(.-> (-> argument return it)
51+
(Call argument return it)
52+
it))
53+
(function argument))
54+
55+
(expansion.let [#Return (these 0 0b)
56+
#Argument (these 0 1b)]
57+
(these (the return
58+
(for_any (_ argument return)
59+
(Call argument return
60+
return))
61+
{#Return [[] //.identity]})
62+
63+
(the (argument it)
64+
(for_any (_ argument return)
65+
(.-> argument
66+
(Call argument return
67+
(-> argument
68+
return))))
69+
{#Argument [[it] //.identity]})
70+
71+
(the .public (new' return argument)
72+
(for_any (_ argument return return')
73+
(.-> (.-> return' return)
74+
(.-> argument (Change return'))
75+
return'
76+
(-> argument
77+
return)))
78+
(function (new total it)
79+
(when it
80+
{#Argument [it of]}
81+
(of (new (argument it total)))
82+
83+
{#Return [_ of]}
84+
(of (return total)))))
85+
86+
(the .public new
87+
(for_any (_ argument return)
88+
(.-> (.-> argument (Change return))
89+
return
90+
(-> argument
91+
return)))
92+
(..new' //.identity))
93+
))
94+
95+
(the (partial' argument function)
96+
(for_any (_ argument return)
97+
(.-> argument
98+
(Change (-> argument
99+
return))))
100+
(per ..application
101+
function
102+
(..argument argument)))
103+
104+
(syntax.the .public (partial ['function ?list.any
105+
'*argument (?.some ?list.any)])
106+
(pure (list (` (|> (, 'function)
107+
(,* (list#each (function (_ 'argument)
108+
(` (..partial' (, 'argument))))
109+
'*argument)))))))
110+
111+
(syntax.the .public (of ['function ?list.any
112+
'*argument (?.some ?list.any)])
113+
(pure (list (` (per ..application
114+
(, 'function)
115+
(,* (list#each (function (_ 'argument)
116+
(` (..argument (, 'argument))))
117+
'*argument))
118+
..return)))))

stdlib/source/test/lux/function/variadic.lux

+25-55
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,46 @@
66
[lux (.except)
77
[abstract
88
["[0]" monad]]
9-
[error
10-
["[0]" try]
11-
["[0]" exception]]
12-
[data
13-
["[0]" text]
14-
[collection
15-
["[0]" list (.use "[1]#[0]" mix)
16-
["?[1]" \\projection]]]]
179
[math
1810
["[0]" random]
1911
[number
2012
[/64
2113
["n" natural]]]]
22-
[macro
23-
["[0]" syntax]
24-
["[0]" expansion]]
25-
[meta
26-
["[0]" code (.only)
27-
["<[1]>" \\projection]]]
2814
[test
2915
["_" property (.only Test)]]]]
3016
[\\library
3117
["[0]" /]])
3218

33-
(syntax.the (macro_error [macro ?list.any])
34-
(function (_ compiler)
35-
(when ((expansion.complete macro) compiler)
36-
{try.#Failure error}
37-
{try.#Success [compiler (list (code.text error))]}
38-
39-
{try.#Success _}
40-
{try.#Failure "OOPS!"})))
19+
(the +
20+
(/.-> Natural
21+
Natural)
22+
(/.new n.+ 0))
4123

42-
(/.the (+ left right extra)
43-
(-> Natural Natural (List Natural)
44-
Natural)
45-
(list#mix n.+ (n.+ left right) extra))
24+
(the average
25+
(/.-> Natural
26+
Natural)
27+
(/.new' (function (_ [count sum])
28+
(n./ count sum))
29+
(function (_ summand [count sum])
30+
[(++ count) (n.+ summand sum)])
31+
[0 0]))
4632

4733
(the .public test
4834
Test
4935
(monad.let [! random.monad]
50-
[p0 random.natural
51-
p1 random.natural
52-
p2 random.natural
53-
p3 random.natural
54-
p4 random.natural
55-
p5 random.natural]
36+
[parameter_0 random.natural
37+
parameter_1 random.natural
38+
parameter_2 random.natural]
5639
(<| (_.covering /._)
40+
(_.for [/.-> /.new])
5741
(all _.and
58-
(_.coverage [/.the]
59-
(and (n.= (all n.+ p0 p1)
60-
(+ p0 p1))
61-
(n.= (all n.+ p0 p1 p2)
62-
(+ p0 p1 p2))
63-
(n.= (all n.+ p0 p1 p2 p3)
64-
(+ p0 p1 p2 p3))
65-
(n.= (all n.+ p0 p1 p2 p3 p4)
66-
(+ p0 p1 p2 p3 p4))
67-
(n.= (all n.+ p0 p1 p2 p3 p4 p5)
68-
(+ p0 p1 p2 p3 p4 p5))))
69-
(_.coverage [/.duplicate_parameters]
70-
(text.contains? (its exception.#label /.duplicate_parameters)
71-
(macro_error
72-
(/.the .public (- _ _)
73-
(-> Natural (List Natural) Natural)
74-
(undefined)))))
75-
(_.coverage [/.must_have_rest_parameter]
76-
(text.contains? (its exception.#label /.must_have_rest_parameter)
77-
(macro_error
78-
(/.the .public (-)
79-
(-> Natural (List Natural) Natural)
80-
(undefined)))))
42+
(_.coverage [/.of]
43+
(n.= (all n.+ parameter_0 parameter_1 parameter_2)
44+
(/.of ..+ parameter_0 parameter_1 parameter_2)))
45+
(_.coverage [/.partial]
46+
(n.= (/.of ..+ parameter_0 parameter_1 parameter_2)
47+
(/.of (/.partial ..+ parameter_0 parameter_1) parameter_2)))
48+
(_.coverage [/.new']
49+
(n.= (n./ 3 (all n.+ parameter_0 parameter_1 parameter_2))
50+
(/.of ..average parameter_0 parameter_1 parameter_2)))
8151
))))

0 commit comments

Comments
 (0)