Skip to content

Commit

Permalink
New implementation of variadic functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoejp committed Dec 3, 2024
1 parent 1b0f830 commit 9dace60
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 123 deletions.
6 changes: 3 additions & 3 deletions stdlib/source/library/lux/control/filter.lux
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
["//" effect]]
["[0]" monad (.only Monad)
["/" free]]]
["[0]" function (.only)
[function
[predicate (.only Predicate)]]
[data
[collection
Expand Down Expand Up @@ -48,13 +48,13 @@
(these (the .public read
(for_any (_ read yield)
(Filter read yield read))
{/.#Impure {#Read [[] (|>> {/.#Pure})]}})
{/.#Impure {#Read [[] (by ..monad pure)]}})

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

(the .public (stream filter it)
(for_any (_ read yield it)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/source/library/lux/control/stream.lux
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
(when it
{/.#Impure [item after]}
(if (? item)
{/.#Impure [item (|>> after (only ?))]}
(only ? (after [])))
{/.#Impure [item (|>> after (only ?))]}
(only ? (after [])))

{/.#Pure it}
{/.#Pure it}))
Expand All @@ -88,7 +88,7 @@
(for_any (_ it)
(-> it
(Stream it Any)))
{/.#Impure [[it] (function.constant {/.#Pure []})]})
{/.#Impure [[it] (by ..monad pure)]})

(the .public many
(for_any (_ it)
Expand Down
169 changes: 107 additions & 62 deletions stdlib/source/library/lux/function/variadic.lux
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,115 @@
... https://en.wikipedia.org/wiki/Variadic_function
(.using
[library
[lux (.except the)
[lux (.except argument ->)
[abstract
["?" projection]
["[0]" monad]]
[error
["[0]" exception (.only Exception)]]
[functor
["/" effect]]]
[data
["[0]" text (.only)
["%" \\injection]]
[collection
["[0]" list (.use "[1]#[0]" monad)
["?[1]" \\projection]]
["[0]" set]]]
[math
[number
[/64
["n" natural]]]]
[macro (.only with_names)
["[0]" syntax (.only)
["[0]" export]]]
["[0]" meta (.only)
["[0]" module]
["[0]" name]
["[0]" code (.only)
["?[1]" \\projection]]]]])

(exception.the .public (duplicate_parameters [definition parameters])
(Exception [Name (List Text)])
(exception.report
(list ["Definition" (name.as_text definition)]
["Parameters" (list.as_text %.text parameters)])))

(exception.the .public (must_have_rest_parameter [definition])
(Exception Name)
(exception.report
(list ["Definition" (name.as_text definition)])))

(syntax.the .public (the [[exported? [name parameters] type body]
(export.with (all ?.and
(?code.form (?.and ?code.local (?.some ?code.local)))
?list.any
?list.any))])
(monad.let meta.monad
[[here _] module.current]
(if (n.= (list.size parameters)
(set.size (set.of_list text.hash parameters)))
(with_names ['function]
(when (list.split 1 (list.in_reverse parameters))
[(list &rest) mandatory]
(let [mandatory (list#each code.local (list.in_reverse mandatory))
&rest (code.local &rest)]
(pure (list (` (.the (, exported?) ((, 'function) (,* mandatory) (, &rest))
(, type)
(, body)))
(` (syntax.the (, exported?) ((, (code.local name)) [(,* (|> mandatory
(list#each (function (_ parameter)
(list parameter (` ?list.any))))
list#conjoint))
(, &rest) (?.some ?list.any)])
(by meta.monad (,' pure)
(list (` ((, 'function)
(,* (list#each (|>> , ((,' .,)) `) mandatory))
(list ((,' .,*) (, &rest))))))))))))

_
(meta.failure (exception.error ..must_have_rest_parameter [[here name]]))))
(meta.failure (exception.error ..duplicate_parameters [[here name] parameters])))))
["?[1]" \\projection]]]]
[macro
["[0]" syntax]
["[0]" expansion]]]]
["[0]" // (.only)
[poly
["[0]" type]]])

(every (Return return)
(/.Effect [] return))

(every (Call' Argument
argument return)
(all /.Or
(Return return)
(Argument argument return)
))

(every (->' Call' Argument
argument return
it)
(.-> (Call' Argument argument return it)
it))

(every (Argument argument return)
(/.Effect argument (->' Call' Argument argument return)))

(every Call
(Call' ..Argument))

(every .public ->
(->' ..Call' ..Argument))

(the (application function argument)
(for_any (_ argument return it)
(.-> (-> argument return it)
(Call argument return it)
it))
(function argument))

(expansion.let [#Return (these 0 0b)
#Argument (these 0 1b)]
(these (the return
(for_any (_ argument return)
(Call argument return
return))
{#Return [[] //.identity]})

(the (argument it)
(for_any (_ argument return)
(.-> argument
(Call argument return
(-> argument
return))))
{#Argument [[it] //.identity]})

(the .public (new' return argument)
(for_any (_ argument return return')
(.-> (.-> return' return)
(.-> argument (Change return'))
return'
(-> argument
return)))
(function (new total it)
(when it
{#Argument [it of]}
(of (new (argument it total)))

{#Return [_ of]}
(of (return total)))))

(the .public new
(for_any (_ argument return)
(.-> (.-> argument (Change return))
return
(-> argument
return)))
(..new' //.identity))
))

(the (partial' argument function)
(for_any (_ argument return)
(.-> argument
(Change (-> argument
return))))
(per ..application
function
(..argument argument)))

(syntax.the .public (partial ['function ?list.any
'*argument (?.some ?list.any)])
(pure (list (` (|> (, 'function)
(,* (list#each (function (_ 'argument)
(` (..partial' (, 'argument))))
'*argument)))))))

(syntax.the .public (of ['function ?list.any
'*argument (?.some ?list.any)])
(pure (list (` (per ..application
(, 'function)
(,* (list#each (function (_ 'argument)
(` (..argument (, 'argument))))
'*argument))
..return)))))
80 changes: 25 additions & 55 deletions stdlib/source/test/lux/function/variadic.lux
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,46 @@
[lux (.except)
[abstract
["[0]" monad]]
[error
["[0]" try]
["[0]" exception]]
[data
["[0]" text]
[collection
["[0]" list (.use "[1]#[0]" mix)
["?[1]" \\projection]]]]
[math
["[0]" random]
[number
[/64
["n" natural]]]]
[macro
["[0]" syntax]
["[0]" expansion]]
[meta
["[0]" code (.only)
["<[1]>" \\projection]]]
[test
["_" property (.only Test)]]]]
[\\library
["[0]" /]])

(syntax.the (macro_error [macro ?list.any])
(function (_ compiler)
(when ((expansion.complete macro) compiler)
{try.#Failure error}
{try.#Success [compiler (list (code.text error))]}

{try.#Success _}
{try.#Failure "OOPS!"})))
(the +
(/.-> Natural
Natural)
(/.new n.+ 0))

(/.the (+ left right extra)
(-> Natural Natural (List Natural)
Natural)
(list#mix n.+ (n.+ left right) extra))
(the average
(/.-> Natural
Natural)
(/.new' (function (_ [count sum])
(n./ count sum))
(function (_ summand [count sum])
[(++ count) (n.+ summand sum)])
[0 0]))

(the .public test
Test
(monad.let [! random.monad]
[p0 random.natural
p1 random.natural
p2 random.natural
p3 random.natural
p4 random.natural
p5 random.natural]
[parameter_0 random.natural
parameter_1 random.natural
parameter_2 random.natural]
(<| (_.covering /._)
(_.for [/.-> /.new])
(all _.and
(_.coverage [/.the]
(and (n.= (all n.+ p0 p1)
(+ p0 p1))
(n.= (all n.+ p0 p1 p2)
(+ p0 p1 p2))
(n.= (all n.+ p0 p1 p2 p3)
(+ p0 p1 p2 p3))
(n.= (all n.+ p0 p1 p2 p3 p4)
(+ p0 p1 p2 p3 p4))
(n.= (all n.+ p0 p1 p2 p3 p4 p5)
(+ p0 p1 p2 p3 p4 p5))))
(_.coverage [/.duplicate_parameters]
(text.contains? (its exception.#label /.duplicate_parameters)
(macro_error
(/.the .public (- _ _)
(-> Natural (List Natural) Natural)
(undefined)))))
(_.coverage [/.must_have_rest_parameter]
(text.contains? (its exception.#label /.must_have_rest_parameter)
(macro_error
(/.the .public (-)
(-> Natural (List Natural) Natural)
(undefined)))))
(_.coverage [/.of]
(n.= (all n.+ parameter_0 parameter_1 parameter_2)
(/.of ..+ parameter_0 parameter_1 parameter_2)))
(_.coverage [/.partial]
(n.= (/.of ..+ parameter_0 parameter_1 parameter_2)
(/.of (/.partial ..+ parameter_0 parameter_1) parameter_2)))
(_.coverage [/.new']
(n.= (n./ 3 (all n.+ parameter_0 parameter_1 parameter_2))
(/.of ..average parameter_0 parameter_1 parameter_2)))
))))

0 comments on commit 9dace60

Please sign in to comment.