diff options
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/source/lux/data/number/complex.lux | 6 | ||||
-rw-r--r-- | stdlib/source/lux/data/text/format.lux | 2 | ||||
-rw-r--r-- | stdlib/source/lux/macro/poly/json.lux | 7 | ||||
-rw-r--r-- | stdlib/source/lux/time/instant.lux | 17 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/format/json.lux | 75 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/number/complex.lux | 23 | ||||
-rw-r--r-- | stdlib/test/test/lux/lang/syntax.lux | 7 | ||||
-rw-r--r-- | stdlib/test/test/lux/math.lux | 13 | ||||
-rw-r--r-- | stdlib/test/test/lux/time/date.lux | 2 | ||||
-rw-r--r-- | stdlib/test/test/lux/time/instant.lux | 30 |
10 files changed, 95 insertions, 87 deletions
diff --git a/stdlib/source/lux/data/number/complex.lux b/stdlib/source/lux/data/number/complex.lux index d17180530..876ab66d1 100644 --- a/stdlib/source/lux/data/number/complex.lux +++ b/stdlib/source/lux/data/number/complex.lux @@ -8,8 +8,6 @@ ["p" parser]) (data [number "frac/" Number<Frac> "f/" Codec<Text,Frac>] [text "text/" Monoid<Text>] - text/format - ["E" error] [maybe] (coll [list "L/" Monad<List>])) [macro] @@ -127,8 +125,8 @@ (let [(^slots [#real #imaginary]) subject] {#real (f/* (math.cosh imaginary) (math.cos real)) - #imaginary (f/* (math.sinh imaginary) - (frac/negate (math.sin real)))})) + #imaginary (frac/negate (f/* (math.sinh imaginary) + (math.sin real)))})) (def: #export (cosh subject) (-> Complex Complex) diff --git a/stdlib/source/lux/data/text/format.lux b/stdlib/source/lux/data/text/format.lux index 80d672d67..c511d194d 100644 --- a/stdlib/source/lux/data/text/format.lux +++ b/stdlib/source/lux/data/text/format.lux @@ -51,7 +51,7 @@ [%hex Nat (:: number.Hex@Codec<Text,Nat> encode)] [%xml xml.XML (:: xml.Codec<Text,XML> encode)] [%json json.JSON (:: json.Codec<Text,JSON> encode)] - [%instant instant.Instant (:: instant.Codec<Text,Instant> encode)] + [%instant instant.Instant instant.to-text] [%duration duration.Duration (:: duration.Codec<Text,Duration> encode)] [%date date.Date (:: date.Codec<Text,Date> encode)] ) diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux index 9a3096dff..cbe3877d7 100644 --- a/stdlib/source/lux/macro/poly/json.lux +++ b/stdlib/source/lux/macro/poly/json.lux @@ -17,7 +17,7 @@ [sequence #+ Sequence sequence "sequence/" Monad<Sequence>] ["d" dict]) (format ["//" json #+ JSON])) - (time ["i" instant] + (time ## ["i" instant] ["du" duration] ["da" date]) [macro #+ with-gensyms] @@ -50,6 +50,7 @@ (|> low nat-to-int int-to-frac #//.Number))))) (def: (decode input) (<| (//.run input) + //.array (do p.Monad<Parser> [high //.number low //.number]) @@ -97,7 +98,7 @@ (|>> (:: <codec> (~' encode)) #//.String)))))] [du.Duration du.Codec<Text,Duration>] - [i.Instant i.Codec<Text,Instant>] + ## [i.Instant i.Codec<Text,Instant>] [da.Date da.Codec<Text,Date>] [da.Day da.Codec<Text,Day>] [da.Month da.Codec<Text,Month>])] @@ -209,7 +210,7 @@ (p.codec <codec> //.string)))))] [du.Duration du.Codec<Text,Duration>] - [i.Instant i.Codec<Text,Instant>] + ## [i.Instant i.Codec<Text,Instant>] [da.Date da.Codec<Text,Date>] [da.Day da.Codec<Text,Day>] [da.Month da.Codec<Text,Month>])] diff --git a/stdlib/source/lux/time/instant.lux b/stdlib/source/lux/time/instant.lux index 334b05115..22d57a06e 100644 --- a/stdlib/source/lux/time/instant.lux +++ b/stdlib/source/lux/time/instant.lux @@ -192,7 +192,7 @@ day-time])) ## Based on this: https://stackoverflow.com/a/42936293/6823464 -(def: (encode instant) +(def: #export (to-text instant) (-> Instant Text) (let [[[year month day] day-time] (extract-date instant) day-time (if (duration/>= duration.empty day-time) @@ -200,8 +200,7 @@ (duration.merge duration.day day-time)) [hours day-time] [(duration.query duration.hour day-time) (duration.frame duration.hour day-time)] [minutes day-time] [(duration.query duration.minute day-time) (duration.frame duration.minute day-time)] - [seconds millis] [(duration.query duration.second day-time) (duration.frame duration.second day-time)] - ] + [seconds millis] [(duration.query duration.second day-time) (duration.frame duration.second day-time)]] ($_ text/compose (int/encode year) "-" (pad month) "-" (pad day) "T" (pad hours) ":" (pad minutes) ":" (pad seconds) (|> millis @@ -299,13 +298,13 @@ (-> Text (e.Error Instant)) (l.run input lex-instant)) -(struct: #export _ - {#.doc "Based on ISO 8601. +## (struct: #export _ +## {#.doc "Based on ISO 8601. - For example: 2017-01-15T21:14:51.827Z"} - (Codec Text Instant) - (def: encode encode) - (def: decode decode)) +## For example: 2017-01-15T21:14:51.827Z"} +## (Codec Text Instant) +## (def: encode encode) +## (def: decode decode)) (def: #export now (IO Instant) diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index e08478993..d5d863514 100644 --- a/stdlib/test/test/lux/data/format/json.lux +++ b/stdlib/test/test/lux/data/format/json.lux @@ -76,17 +76,17 @@ (#Addition Frac Recursive)) (type: Record - {## #unit Unit - ## #bool Bool - ## #frac Frac - ## #text Text - ## #maybe (Maybe Frac) - ## #list (List Frac) + {#unit Unit + #bool Bool + #frac Frac + #text Text + #maybe (Maybe Frac) + #list (List Frac) + #dict (d.Dict Text Frac) ## #variant Variant ## #tuple [Bool Frac Text] - ## #dict (d.Dict Text Frac) - ## #recursive Recursive - #instant ti.Instant + #recursive Recursive + ## #instant ti.Instant #duration tdu.Duration #date tda.Date #grams (unit.Qty unit.Gram) @@ -109,17 +109,17 @@ (do r.Monad<Random> [size (:: @ map (n/% +2) r.nat)] ($_ r.seq - ## (:: @ wrap []) - ## r.bool - ## r.frac - ## (r.text size) - ## (r.maybe r.frac) - ## (r.list size r.frac) + (:: @ wrap []) + r.bool + r.frac + (r.text size) + (r.maybe r.frac) + (r.list size r.frac) + (r.dict text.Hash<Text> size (r.text size) r.frac) ## ($_ r.alt r.bool (r.text size) r.frac) ## ($_ r.seq r.bool r.frac (r.text size)) - ## (r.dict text.Hash<Text> size (r.text size) r.frac) - ## gen-recursive - _instant.instant + gen-recursive + ## _instant.instant _duration.duration _date.date qty @@ -142,28 +142,27 @@ _ false))] - (and ## (:: bool.Eq<Bool> = (get@ #bool recL) (get@ #bool recR)) - ## (f/= (get@ #frac recL) (get@ #frac recR)) - ## (:: text.Eq<Text> = (get@ #text recL) (get@ #text recR)) - ## (:: (maybe.Eq<Maybe> number.Eq<Frac>) = (get@ #maybe recL) (get@ #maybe recR)) - ## (:: (list.Eq<List> number.Eq<Frac>) = (get@ #list recL) (get@ #list recR)) - ## (variant/= (get@ #variant recL) (get@ #variant recR)) - ## (let [[tL0 tL1 tL2] (get@ #tuple recL) - ## [tR0 tR1 tR2] (get@ #tuple recR)] - ## (and (:: bool.Eq<Bool> = tL0 tR0) - ## (f/= tL1 tR1) - ## (:: text.Eq<Text> = tL2 tR2))) - ## (:: (d.Eq<Dict> number.Eq<Frac>) = (get@ #dict recL) (get@ #dict recR)) - ## (:: Eq<Recursive> = (get@ #recursive recL) (get@ #recursive recR)) - (:: ti.Eq<Instant> = (get@ #instant recL) (get@ #instant recR)) - (:: tdu.Eq<Duration> = (get@ #duration recL) (get@ #duration recR)) - (:: tda.Eq<Date> = (get@ #date recL) (get@ #date recR)) - (:: unit.Eq<Unit> = (get@ #grams recL) (get@ #grams recR)) - )))) + (and (:: bool.Eq<Bool> = (get@ #bool recL) (get@ #bool recR)) + (f/= (get@ #frac recL) (get@ #frac recR)) + (:: text.Eq<Text> = (get@ #text recL) (get@ #text recR)) + (:: (maybe.Eq<Maybe> number.Eq<Frac>) = (get@ #maybe recL) (get@ #maybe recR)) + (:: (list.Eq<List> number.Eq<Frac>) = (get@ #list recL) (get@ #list recR)) + (:: (d.Eq<Dict> number.Eq<Frac>) = (get@ #dict recL) (get@ #dict recR)) + ## (variant/= (get@ #variant recL) (get@ #variant recR)) + ## (let [[tL0 tL1 tL2] (get@ #tuple recL) + ## [tR0 tR1 tR2] (get@ #tuple recR)] + ## (and (:: bool.Eq<Bool> = tL0 tR0) + ## (f/= tL1 tR1) + ## (:: text.Eq<Text> = tL2 tR2))) + (:: Eq<Recursive> = (get@ #recursive recL) (get@ #recursive recR)) + ## (:: ti.Eq<Instant> = (get@ #instant recL) (get@ #instant recR)) + (:: tdu.Eq<Duration> = (get@ #duration recL) (get@ #duration recR)) + (:: tda.Eq<Date> = (get@ #date recL) (get@ #date recR)) + (:: unit.Eq<Unit> = (get@ #grams recL) (get@ #grams recR)) + )))) (context: "Polytypism" - (<| (seed +10473881104006775240) - ## (times +100) + (<| (times +100) (do @ [sample gen-record #let [(^open "@/") Eq<Record> diff --git a/stdlib/test/test/lux/data/number/complex.lux b/stdlib/test/test/lux/data/number/complex.lux index 3bc0d6e6d..98138d9fe 100644 --- a/stdlib/test/test/lux/data/number/complex.lux +++ b/stdlib/test/test/lux/data/number/complex.lux @@ -3,15 +3,16 @@ (lux [io] (control [monad #+ do Monad] pipe) - (data [number "frac/" Number<Frac>] + (data [product] + [number "frac/" Number<Frac>] ["&" number/complex] - (coll [list "list/" Functor<List>]) - [product]) + text/format + (coll [list "list/" Functor<List>])) [math] ["r" math/random]) lux/test) -(def: margin-of-error Frac 1.0e-10) +(def: margin-of-error Frac 1.0e-9) (def: (within? margin standard value) (-> Frac &.Complex &.Complex Bool) @@ -155,19 +156,25 @@ (get@ #&.real (&.c/abs (&.c/negate x))))) )))) +(def: (trigonometric-symmetry forward backward angle) + (-> (-> &.Complex &.Complex) (-> &.Complex &.Complex) &.Complex Bool) + (let [normal (|> angle forward backward)] + (|> normal forward backward (within? margin-of-error normal)))) + (context: "Trigonometry" (<| (times +100) (do @ - [x gen-complex] + [angle (|> gen-complex (:: @ map (|>> (update@ #&.real (f/% 1.0)) + (update@ #&.imaginary (f/% 1.0)))))] ($_ seq (test "Arc-sine is the inverse of sine." - (|> x &.sin &.asin (within? margin-of-error x))) + (trigonometric-symmetry &.sin &.asin angle)) (test "Arc-cosine is the inverse of cosine." - (|> x &.cos &.acos (within? margin-of-error x))) + (trigonometric-symmetry &.cos &.acos angle)) (test "Arc-tangent is the inverse of tangent." - (|> x &.tan &.atan (within? margin-of-error x))))))) + (trigonometric-symmetry &.tan &.atan angle)))))) (context: "Power 2 and exponential/logarithm" (<| (times +100) diff --git a/stdlib/test/test/lux/lang/syntax.lux b/stdlib/test/test/lux/lang/syntax.lux index a12aa5ae9..47d46f0b4 100644 --- a/stdlib/test/test/lux/lang/syntax.lux +++ b/stdlib/test/test/lux/lang/syntax.lux @@ -28,6 +28,7 @@ space "\t\v \n\r\f" invalid-range (format digits delimiters space) char-gen (|> r.nat + (:: @ map (|>> (n/% +256) (n/max +1))) (r.filter (function [sample] (not (text.contains? (text.from-code sample) invalid-range)))))] @@ -75,8 +76,7 @@ composite^)))))) (context: "Lux code syntax." - (<| (seed +7998490996744206936) - ## (times +100) + (<| (times +100) (do @ [sample code^ other code^] @@ -166,8 +166,7 @@ (wrap (format "#( " comment " )#"))))))) (context: "Multi-line text & comments." - (<| (seed +6749851812188286456) - ## (times +100) + (<| (times +100) (do @ [#let [char-gen (|> r.nat (r.filter (function [value] (not (or (text.space? value) diff --git a/stdlib/test/test/lux/math.lux b/stdlib/test/test/lux/math.lux index fcfe6ab47..b95751dbc 100644 --- a/stdlib/test/test/lux/math.lux +++ b/stdlib/test/test/lux/math.lux @@ -15,19 +15,24 @@ (def: margin Frac 0.0000001) +(def: (trigonometric-symmetry forward backward angle) + (-> (-> Frac Frac) (-> Frac Frac) Frac Bool) + (let [normal (|> angle forward backward)] + (|> normal forward backward (within? margin normal)))) + (context: "Trigonometry" - (<| (seed +4611737486687492590) + (<| (times +100) (do @ [angle (|> r.frac (:: @ map (f/* &.tau)))] ($_ seq (test "Sine and arc-sine are inverse functions." - (|> angle &.sin &.asin (within? margin angle))) + (trigonometric-symmetry &.sin &.asin angle)) (test "Cosine and arc-cosine are inverse functions." - (|> angle &.cos &.acos (within? margin angle))) + (trigonometric-symmetry &.cos &.acos angle)) (test "Tangent and arc-tangent are inverse functions." - (|> angle &.tan &.atan (within? margin angle))) + (trigonometric-symmetry &.tan &.atan angle)) )))) (context: "Roots" diff --git a/stdlib/test/test/lux/time/date.lux b/stdlib/test/test/lux/time/date.lux index 7d7ca03fe..8e8c77860 100644 --- a/stdlib/test/test/lux/time/date.lux +++ b/stdlib/test/test/lux/time/date.lux @@ -126,7 +126,7 @@ (@/<= reference sample))))))) (context: "(Date) Codec" - (<| (seed +1501531301120) + (<| (times +100) (do @ [sample date #let [(^open "@/") @.Eq<Date> diff --git a/stdlib/test/test/lux/time/instant.lux b/stdlib/test/test/lux/time/instant.lux index a264e6155..ee328ea3d 100644 --- a/stdlib/test/test/lux/time/instant.lux +++ b/stdlib/test/test/lux/time/instant.lux @@ -77,19 +77,19 @@ (test "All instants are relative to the epoch." (|> @.epoch (@.shift (@.relative sample)) (@/= sample))))))) -(context: "Codec" - (<| (seed +9863552679229274604) - ## (times +100) - (do @ - [sample instant - #let [(^open "@/") @.Eq<Instant> - (^open "@/") @.Codec<Text,Instant>]] - (test "Can encode/decode instants." - (|> sample - @/encode - @/decode - (case> (#E.Success decoded) - (@/= sample decoded) +## (context: "Codec" +## (<| (seed +9863552679229274604) +## ## (times +100) +## (do @ +## [sample instant +## #let [(^open "@/") @.Eq<Instant> +## (^open "@/") @.Codec<Text,Instant>]] +## (test "Can encode/decode instants." +## (|> sample +## @/encode +## @/decode +## (case> (#E.Success decoded) +## (@/= sample decoded) - (#E.Error error) - false)))))) +## (#E.Error error) +## false)))))) |