From ecda0d219cf7dc25bd9b0b76815a8880e20232c2 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 18 Aug 2022 19:11:40 -0400 Subject: Added variadic functions. --- stdlib/source/library/lux/control/exception.lux | 8 +-- .../library/lux/control/function/variadic.lux | 68 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 stdlib/source/library/lux/control/function/variadic.lux (limited to 'stdlib/source/library') diff --git a/stdlib/source/library/lux/control/exception.lux b/stdlib/source/library/lux/control/exception.lux index 8d39a9daf..474ca07d3 100644 --- a/stdlib/source/library/lux/control/exception.lux +++ b/stdlib/source/library/lux/control/exception.lux @@ -37,9 +37,6 @@ (-> (Exception e) (-> Text a) (Try a) (Try a))) (.when try - {//.#Success output} - {//.#Success output} - {//.#Failure error} (let [reference (the #label exception)] (if (text.starts_with? reference error) @@ -47,7 +44,10 @@ (text.clip_since (text.size reference)) maybe.trusted then)} - {//.#Failure error})))) + {//.#Failure error})) + + success + success)) (.def .public (otherwise else try) (All (_ a) diff --git a/stdlib/source/library/lux/control/function/variadic.lux b/stdlib/source/library/lux/control/function/variadic.lux new file mode 100644 index 000000000..5b53bbe83 --- /dev/null +++ b/stdlib/source/library/lux/control/function/variadic.lux @@ -0,0 +1,68 @@ +... https://en.wikipedia.org/wiki/Variadic_function +(.require + [library + [lux (.except def) + [abstract + [monad (.only do)]] + [control + ["?" parser] + ["[0]" exception (.only Exception)]] + [data + ["[0]" text (.only) + ["%" \\format]] + [collection + ["[0]" list (.use "[1]#[0]" monad)] + ["[0]" set]]] + [math + [number + ["n" nat]]] + ["[0]" meta (.only) + ["[0]" code (.only) + ["?[1]" \\parser]] + [macro (.only with_symbols) + [syntax (.only syntax) + ["[0]" export]]]]]]) + +(exception.def .public (duplicate_parameters [definition parameters]) + (Exception [Symbol (List Text)]) + (exception.report + (list ["Definition" (%.symbol definition)] + ["Parameters" (%.list %.text parameters)]))) + +(exception.def .public (must_have_rest_parameter definition) + (Exception Symbol) + (exception.report + (list ["Definition" (%.symbol definition)]))) + +(.def .public def + (syntax (_ [[exported? [name parameters] type body] + (export.parser (all ?.and + (?code.form (?.and ?code.local (?.some ?code.local))) + ?code.any + ?code.any))]) + (do meta.monad + [here meta.current_module_name] + (if (n.= (list.size parameters) + (set.size (set.of_list text.hash parameters))) + (with_symbols [g!function] + (when (list.reversed parameters) + (list.partial &rest mandatory) + (let [mandatory (list#each code.local (list.reversed mandatory)) + &rest (code.local &rest)] + (in (list (` (.def (, exported?) ((, g!function) (,* mandatory) (, &rest)) + (, type) + (, body))) + (` (.def (, exported?) (, (code.local name)) + (syntax ((, (code.local name)) [(,* (|> mandatory + (list#each (function (_ parameter) + (list parameter (` ?code.any)))) + list#conjoint)) + (, &rest) (?.some ?code.any)]) + (at meta.monad (,' in) + (list (` ((, g!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])))))) -- cgit v1.2.3