diff options
author | Eduardo Julian | 2019-02-03 11:53:21 -0400 |
---|---|---|
committer | Eduardo Julian | 2019-02-03 11:53:21 -0400 |
commit | cf9e3fa6fef24cc0828ab882661fa96dc6f2570d (patch) | |
tree | 082f91e8c84a72ace38a35b30a4acb61b8d27ab6 | |
parent | 700f82c940794684cbce9535274f6d7ea3f9c692 (diff) |
Some refactoring around the identity function.
-rw-r--r-- | stdlib/source/lux.lux | 9 | ||||
-rw-r--r-- | stdlib/source/lux/data/format/css/selector.lux | 2 | ||||
-rw-r--r-- | stdlib/source/lux/data/format/json.lux | 17 | ||||
-rw-r--r-- | stdlib/source/lux/data/number.lux | 64 | ||||
-rw-r--r-- | stdlib/source/lux/data/number/ratio.lux | 7 | ||||
-rw-r--r-- | stdlib/source/lux/function.lux | 25 | ||||
-rw-r--r-- | stdlib/source/lux/host.jvm.lux | 34 | ||||
-rw-r--r-- | stdlib/source/lux/macro/syntax/common/writer.lux | 3 | ||||
-rw-r--r-- | stdlib/test/test/lux.lux | 5 |
9 files changed, 85 insertions, 81 deletions
diff --git a/stdlib/source/lux.lux b/stdlib/source/lux.lux index d3a0f1825..b7de70c5d 100644 --- a/stdlib/source/lux.lux +++ b/stdlib/source/lux.lux @@ -3985,10 +3985,7 @@ #None (fail "Wrong syntax for structure:")))) -(def: #export (id x) - {#.doc "Identity function. Does nothing to it's argument and just returns it."} - (All [a] (-> a a)) - x) +(def: (identity x) (All [a] (-> a a)) x) (macro: #export (type: tokens) {#.doc (text$ ($_ "lux text concat" @@ -5302,8 +5299,8 @@ <open> parts-text <close>)])) - ([#Form "(" ")" id] - [#Tuple "[" "]" id] + ([#Form "(" ")" ..identity] + [#Tuple "[" "]" ..identity] [#Record "{" "}" rejoin-all-pairs]) [new-cursor (#Rev value)] diff --git a/stdlib/source/lux/data/format/css/selector.lux b/stdlib/source/lux/data/format/css/selector.lux index 905012aeb..7b80e4973 100644 --- a/stdlib/source/lux/data/format/css/selector.lux +++ b/stdlib/source/lux/data/format/css/selector.lux @@ -1,5 +1,5 @@ (.module: - [lux (#- or and for id is? not) + [lux (#- or and for is? not) [data ["." text format]] diff --git a/stdlib/source/lux/data/format/json.lux b/stdlib/source/lux/data/format/json.lux index 7d2e90270..edafe3178 100644 --- a/stdlib/source/lux/data/format/json.lux +++ b/stdlib/source/lux/data/format/json.lux @@ -296,7 +296,7 @@ [string Text #String "string"] ) -(do-template [<test> <check> <type> <eq> <encoder> <tag> <desc> <pre>] +(do-template [<test> <check> <type> <eq> <encoder> <tag> <desc>] [(def: #export (<test> test) {#.doc (code.text ($_ text/compose "Asks whether a JSON value is a " <desc> "."))} (-> <type> (Reader Bit)) @@ -304,7 +304,7 @@ [head any] (case head (<tag> value) - (wrap (:: <eq> = test (<pre> value))) + (wrap (:: <eq> = test value)) _ (fail ($_ text/compose "JSON value is not " <desc> "."))))) @@ -316,17 +316,16 @@ [head any] (case head (<tag> value) - (let [value (<pre> value)] - (if (:: <eq> = test value) - (wrap []) - (fail ($_ text/compose "Value mismatch: " (<encoder> test) " =/= " (<encoder> value))))) + (if (:: <eq> = test value) + (wrap []) + (fail ($_ text/compose "Value mismatch: " (<encoder> test) " =/= " (<encoder> value)))) _ (fail ($_ text/compose "JSON value is not a " <desc> ".")))))] - [boolean? boolean! Bit bit.Equivalence<Bit> encode-boolean #Boolean "boolean" id] - [number? number! Frac number.Equivalence<Frac> (:: number.Codec<Text,Frac> encode) #Number "number" id] - [string? string! Text text.Equivalence<Text> text.encode #String "string" id] + [boolean? boolean! Bit bit.Equivalence<Bit> encode-boolean #Boolean "boolean"] + [number? number! Frac number.Equivalence<Frac> (:: number.Codec<Text,Frac> encode) #Number "number"] + [string? string! Text text.Equivalence<Text> text.encode #String "string"] ) (def: #export (nullable parser) diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux index e745d542a..f297f2788 100644 --- a/stdlib/source/lux/data/number.lux +++ b/stdlib/source/lux/data/number.lux @@ -12,14 +12,14 @@ [data ["." error (#+ Error)] ["." maybe] - ["." text] + ["." text (#+ Char)] [collection ["." array (#+ Array)]]] + ["." function] ["." math]] [/ ["." i64]]) -## [Structures] (do-template [<type> <test>] [(structure: #export _ (Equivalence <type>) (def: = <test>))] @@ -44,14 +44,38 @@ [Frac Equivalence<Frac> f/< f/<= f/> f/>=] ) +(do-template [<type> <order> <succ> <pred>] + [(structure: #export _ (Enum <type>) + (def: order <order>) + (def: succ <succ>) + (def: pred <pred>))] + + [Nat Order<Nat> inc dec] + [Int Order<Int> inc dec] + [Frac Order<Frac> (f/+ ("lux frac smallest")) (f/- ("lux frac smallest"))] + [Rev Order<Rev> inc dec] + ) + +(do-template [<type> <enum> <top> <bottom>] + [(structure: #export _ (Interval <type>) + (def: enum <enum>) + (def: top <top>) + (def: bottom <bottom>))] + + [ Nat Enum<Nat> (:coerce Nat -1) 0] + [ Int Enum<Int> +9_223_372_036_854_775_807 -9_223_372_036_854_775_808] + [Frac Enum<Frac> ("lux frac max") ("lux frac min")] + [ Rev Enum<Rev> (:coerce Rev -1) (:coerce Rev 0)] + ) + (structure: #export _ (Number Nat) (def: + n/+) (def: - n/-) (def: * n/*) (def: / n//) (def: % n/%) - (def: negate id) - (def: abs id) + (def: (negate value) (n/- (:: Interval<Nat> top) value)) + (def: abs function.identity) (def: (signum x) (case x 0 0 @@ -88,34 +112,10 @@ (def: / r//) (def: % r/%) (def: (negate x) (r/- x (:coerce Rev -1))) - (def: abs id) + (def: abs function.identity) (def: (signum x) (:coerce Rev -1))) -(do-template [<type> <order> <succ> <pred>] - [(structure: #export _ (Enum <type>) - (def: order <order>) - (def: succ <succ>) - (def: pred <pred>))] - - [Nat Order<Nat> inc dec] - [Int Order<Int> inc dec] - [Frac Order<Frac> (f/+ ("lux frac smallest")) (f/- ("lux frac smallest"))] - [Rev Order<Rev> inc dec] - ) - -(do-template [<type> <enum> <top> <bottom>] - [(structure: #export _ (Interval <type>) - (def: enum <enum>) - (def: top <top>) - (def: bottom <bottom>))] - - [ Nat Enum<Nat> (:coerce Nat -1) 0] - [ Int Enum<Int> +9_223_372_036_854_775_807 -9_223_372_036_854_775_808] - [Frac Enum<Frac> ("lux frac max") ("lux frac min")] - [ Rev Enum<Rev> (:coerce Rev -1) (:coerce Rev 0)] - ) - (do-template [<name> <type> <identity> <compose>] [(structure: #export <name> (Monoid <type>) (def: identity <identity>) @@ -177,9 +177,6 @@ [Frac "lux frac encode" "lux frac decode" "Could not decode Frac"] ) -## [Values & Syntax] -(type: Char Nat) - (def: (get-char! full idx) (-> Text Nat Char) ("lux text char" full idx)) @@ -1011,10 +1008,9 @@ shifted (f/* -1.0 shifted)))))) -## [Hash] (structure: #export _ (Hash Nat) (def: eq Equivalence<Nat>) - (def: hash id)) + (def: hash function.identity)) (structure: #export _ (Hash Int) (def: eq Equivalence<Int>) diff --git a/stdlib/source/lux/data/number/ratio.lux b/stdlib/source/lux/data/number/ratio.lux index 647eaf4b5..1447040e6 100644 --- a/stdlib/source/lux/data/number/ratio.lux +++ b/stdlib/source/lux/data/number/ratio.lux @@ -8,12 +8,13 @@ monad ["p" parser]] [data - ["E" error] + ["." error] ["." product] ["." maybe] [number ("nat/." Codec<Text,Nat>)] ["." text ("text/." Monoid<Text>) format]] + ["." function] ["." math] ["." macro ["." code] @@ -121,7 +122,7 @@ (def: (negate (^slots [#numerator #denominator])) {#numerator denominator #denominator numerator}) - (def: abs id) + (def: abs function.identity) (def: (signum x) {#numerator 1 #denominator 1})) @@ -139,7 +140,7 @@ (def: (decode input) (case (text.split-with separator input) (#.Some [num denom]) - (do E.Monad<Error> + (do error.Monad<Error> [numerator (nat/decode num) denominator (nat/decode denom)] (wrap (normalize {#numerator numerator diff --git a/stdlib/source/lux/function.lux b/stdlib/source/lux/function.lux index 4d4af846a..ca6ebb73e 100644 --- a/stdlib/source/lux/function.lux +++ b/stdlib/source/lux/function.lux @@ -1,24 +1,39 @@ (.module: [lux #* - [control [monoid (#+ Monoid)]]]) + [control + [monoid (#+ Monoid)]]]) + +(def: #export identity + {#.doc (doc "Identity function." + "Does nothing to it's argument and just returns it." + (= (identity "foo") + "foo"))} + (All [a] (-> a a)) + (|>>)) (def: #export (compose f g) - {#.doc "Function composition."} + {#.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 "Create constant functions."} + {#.doc (doc "Create constant functions." + (= ((constant "foo") "bar") + "foo"))} (All [o] (-> o (All [i] (-> i o)))) (function (_ _) value)) (def: #export (flip f) - {#.doc "Flips the order of the arguments of a function."} + {#.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<Function> (All [a] (Monoid (-> a a))) - (def: identity id) + (def: identity ..identity) (def: compose ..compose)) diff --git a/stdlib/source/lux/host.jvm.lux b/stdlib/source/lux/host.jvm.lux index 8a6a9f43c..6da77945f 100644 --- a/stdlib/source/lux/host.jvm.lux +++ b/stdlib/source/lux/host.jvm.lux @@ -15,7 +15,7 @@ [collection ["." array (#+ Array)] ["." list ("list/." Monad<List> Fold<List> Monoid<List>)]]] - function + ["." function] ["." type ("type/." Equivalence<Type>)] ["." macro (#+ with-gensyms Functor<Meta> Monad<Meta>) ["." code] @@ -1388,7 +1388,7 @@ (spaced (list (super-class-decl$ super) (with-brackets (spaced (list/map super-class-decl$ interfaces))) (with-brackets (spaced (list/map constructor-arg$ constructor-args))) - (with-brackets (spaced (list/map (method-def$ id super) methods))))))] + (with-brackets (spaced (list/map (method-def$ function.identity super) methods))))))] (wrap (list (` ((~ (code.text def-code)))))))) (syntax: #export (null) @@ -1398,23 +1398,19 @@ (def: #export (null? obj) {#.doc (doc "Test for null object reference." - (null? (null)) - "=>" - #1 - (null? "YOLO") - "=>" - #0)} + (= (null? (null)) + true) + (= (null? "YOLO") + false))} (-> (primitive "java.lang.Object") Bit) ("jvm object null?" obj)) (syntax: #export (??? expr) {#.doc (doc "Takes a (potentially null) ObjectType reference and creates a (Maybe ObjectType) for it." - (??? (: java/lang/String (null))) - "=>" - #.None - (??? "YOLO") - "=>" - (#.Some "YOLO"))} + (= (??? (: java/lang/String (null))) + #.None) + (= (??? "YOLO") + (#.Some "YOLO")))} (with-gensyms [g!temp] (wrap (list (` (let [(~ g!temp) (~ expr)] (if ("jvm object null?" (~ g!temp)) @@ -1424,12 +1420,10 @@ (syntax: #export (!!! expr) {#.doc (doc "Takes a (Maybe ObjectType) and returns a ObjectType." "A #.None would get translated into a (null)." - (!!! (??? (: java/lang/Thread (null)))) - "=>" - (null) - (!!! (??? "YOLO")) - "=>" - "YOLO")} + (= (null) + (!!! (??? (: java/lang/Thread (null))))) + (= "foo" + (!!! (??? "foo"))))} (with-gensyms [g!value] (wrap (list (` ({(#.Some (~ g!value)) (~ g!value) diff --git a/stdlib/source/lux/macro/syntax/common/writer.lux b/stdlib/source/lux/macro/syntax/common/writer.lux index fe4c961e2..3affd97f7 100644 --- a/stdlib/source/lux/macro/syntax/common/writer.lux +++ b/stdlib/source/lux/macro/syntax/common/writer.lux @@ -5,6 +5,7 @@ [collection [list ("list/." Functor<List>)]] ["." product]] + ["." function] [macro ["." code]]] ["." //]) @@ -23,7 +24,7 @@ (def: #export annotations (-> //.Annotations Code) - (|>> (list/map (product.both code.tag id)) + (|>> (list/map (product.both code.tag function.identity)) code.record)) (def: #export type-variables diff --git a/stdlib/test/test/lux.lux b/stdlib/test/test/lux.lux index 72b9f45ef..0e41a961c 100644 --- a/stdlib/test/test/lux.lux +++ b/stdlib/test/test/lux.lux @@ -6,6 +6,7 @@ [data [number ["." i64]]] + ["." function] ["." math ["r" random (#+ Random) ("r/." Functor<Random>)]] ["_" test (#+ Test)]] @@ -21,8 +22,8 @@ ($_ _.and (_.test "Every value is identical to itself." (is? self self)) - (_.test "The 'id' function doesn't change values in any way." - (is? self (id self))) + (_.test "The identity function doesn't change values in any way." + (is? self (function.identity self))) (do @ [other (r.unicode 1)] (_.test "Values created separately can't be identical." |