diff options
Diffstat (limited to 'stdlib/source/lux/control/function.lux')
-rw-r--r-- | stdlib/source/lux/control/function.lux | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/stdlib/source/lux/control/function.lux b/stdlib/source/lux/control/function.lux new file mode 100644 index 000000000..5a33a2aae --- /dev/null +++ b/stdlib/source/lux/control/function.lux @@ -0,0 +1,40 @@ +(.module: + [lux #* + [abstract + [monoid (#+ Monoid)]]]) + +(def: #export identity + {#.doc (doc "Identity function." + "Does nothing to it's argument and just returns it." + (let [value "foo"] + (is? (identity value) + value)))} + (All [a] (-> a a)) + (|>>)) + +(def: #export (compose f g) + {#.doc (doc "Function composition." + (= ((compose f g) "foo") + (f (g "foo"))))} + (All [a b c] + (-> (-> b c) (-> a b) (-> a c))) + (|>> g f)) + +(def: #export (constant value) + {#.doc (doc "Create constant functions." + (= ((constant "foo") "bar") + "foo"))} + (All [o] (-> o (All [i] (-> i o)))) + (function (_ _) value)) + +(def: #export (flip f) + {#.doc (doc "Flips the order of the arguments of a function." + (= ((flip f) "foo" "bar") + (f "bar" "foo")))} + (All [a b c] + (-> (-> a b c) (-> b a c))) + (function (_ x y) (f y x))) + +(structure: #export monoid (All [a] (Monoid (-> a a))) + (def: identity ..identity) + (def: compose ..compose)) |