From e651e1bd6df5a82fd97d3ab06aed38d0427a5dbc Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 9 Sep 2024 14:23:40 -0400 Subject: [PATCH] `Slice` abstraction for `Text`. --- .../lux/compiler/meta/cli/compiler.lux | 6 +- .../source/library/lux/data/format/json.lux | 10 +- stdlib/source/library/lux/data/format/xml.lux | 34 +- stdlib/source/library/lux/data/text/slice.lux | 134 ++++++++ .../source/library/lux/meta/configuration.lux | 6 +- stdlib/source/library/lux/physics.lux | 265 +-------------- stdlib/source/library/lux/physics/1.lux | 52 +++ stdlib/source/library/lux/physics/2.lux | 301 ++++++++++++++++++ .../lux/target/jvm/type/projection.lux | 8 +- stdlib/source/projection/lux/data/text.lux | 259 +++------------ .../source/projection/lux/data/text/slice.lux | 165 ++++++++++ stdlib/source/test/lux/data/text.lux | 209 +++++------- stdlib/source/test/lux/data/text/slice.lux | 300 +++++++++++++++++ stdlib/source/test/lux/math/number/big.lux | 9 +- stdlib/source/test/lux/physics.lux | 89 +----- stdlib/source/test/lux/physics/1.lux | 9 +- stdlib/source/test/lux/physics/2.lux | 107 +++++++ 17 files changed, 1249 insertions(+), 714 deletions(-) create mode 100644 stdlib/source/library/lux/data/text/slice.lux create mode 100644 stdlib/source/library/lux/physics/2.lux create mode 100644 stdlib/source/projection/lux/data/text/slice.lux create mode 100644 stdlib/source/test/lux/data/text/slice.lux create mode 100644 stdlib/source/test/lux/physics/2.lux diff --git a/stdlib/source/library/lux/compiler/meta/cli/compiler.lux b/stdlib/source/library/lux/compiler/meta/cli/compiler.lux index 686277342..c06f5167f 100644 --- a/stdlib/source/library/lux/compiler/meta/cli/compiler.lux +++ b/stdlib/source/library/lux/compiler/meta/cli/compiler.lux @@ -12,7 +12,9 @@ ["[0]" product] ["[0]" text (.only) ["%" \\injection] - ["<[1]>" \\projection (.only Projection)]] + ["<[1]>" \\projection (.only Projection)] + ["[0]" slice + ["?[1]" \\projection]]] [collection ["[0]" list (.use "[1]#[0]" functor)]]] [math @@ -60,6 +62,6 @@ (let [parameter (is (Projection Text) (<| (<>.after (.this ..start)) (<>.before (.this ..end)) - (.slice (.many! (.none_of! ..end)))))] + (?slice.slice (?slice.many (?slice.none_of ..end)))))] (<>.and (<>.and parameter parameter) (<>.some parameter)))) diff --git a/stdlib/source/library/lux/data/format/json.lux b/stdlib/source/library/lux/data/format/json.lux index 15d3305eb..1938ae9f1 100644 --- a/stdlib/source/library/lux/data/format/json.lux +++ b/stdlib/source/library/lux/data/format/json.lux @@ -24,7 +24,9 @@ ["[0]" product] ["[0]" text (.only \n) ["<[1]>" \\projection (.only Projection)] - ["[0]" escape]] + ["[0]" escape] + ["[0]" slice + ["?[1]" \\projection]]] [collection ["[0]" list (.use "[1]#[0]" monoid mix functor) ["?[1]" \\projection]] @@ -409,9 +411,9 @@ [(.text "\" text.double_quote) text.double_quote] ["\\" "\"]])) (<>.after (.this "\u") - (|> .hexadecimal! - (.exactly! 4) - .slice + (|> ?slice.hexadecimal + (?slice.exactly 4) + ?slice.slice (<>.of natural.hex) (<>#each text.of_character))) ))) diff --git a/stdlib/source/library/lux/data/format/xml.lux b/stdlib/source/library/lux/data/format/xml.lux index 07b46061c..b945db5ce 100644 --- a/stdlib/source/library/lux/data/format/xml.lux +++ b/stdlib/source/library/lux/data/format/xml.lux @@ -12,7 +12,9 @@ [data ["[0]" product] ["[0]" text (.only \n) - ["<[1]>" \\projection (.only Projection Slice)]] + ["<[1]>" \\projection (.only Projection)] + ["[0]" slice (.only Slice) + ["?[1]" \\projection]]] [collection ["[0]" list (.use "[1]#[0]" functor)] ["[0]" dictionary (.only Dictionary)]]] @@ -63,14 +65,14 @@ [hex? (<>.maybe (.this "x"))] (<| (by ! each (|>> .natural text.of_character)) (<>.of integer.base_10) - .slice - .many! + ?slice.slice + ?slice.many (when hex? {.#None} - .decimal! + ?slice.decimal {.#Some _} - .hexadecimal!))) + ?slice.hexadecimal))) (<>.before (.this ";")) (<>.after (.this "&#")))) @@ -86,12 +88,12 @@ (the xml_identifier (Projection Text) - (.slice - (all .and! - (<>.either (.one_of! "_") - .alpha!) - (.some! (<>.either (.one_of! "_.-") - .alpha_num!))))) + (?slice.slice + (all ?slice.and + (<>.either (?slice.one_of "_") + ?slice.alpha) + (?slice.some (<>.either (?slice.one_of "_.-") + ?slice.alpha_num))))) (the namespaced_name^ (Projection Name) @@ -145,8 +147,8 @@ (the comment^ (Projection Slice) - (|> (.not! (.this "--")) - .some! + (|> (?slice.not (.this "--")) + ?slice.some (.enclosed [""]) ..spaced^)) @@ -160,7 +162,7 @@ (the cdata^ (Projection Slice) (let [end (.this "]]>")] - (|> (.some! (.not! end)) + (|> (?slice.some (?slice.not end)) (<>.after end) (<>.after (.this " (..spaced^ (.many xml_character^)) - (<>.either (.slice cdata^)) + (<>.either (?slice.slice cdata^)) (<>#each (|>> {#Text})))) (the null^ @@ -189,7 +191,7 @@ (pure {#Node tag attrs (list)})) (do <>.monad [_ (.this ">") - _ (<>.some (<>.either .space! + _ (<>.some (<>.either ?slice.space ..comment^)) _ (..close_tag^ tag)] (pure {#Node tag attrs (list)}))) diff --git a/stdlib/source/library/lux/data/text/slice.lux b/stdlib/source/library/lux/data/text/slice.lux new file mode 100644 index 000000000..63c7b2711 --- /dev/null +++ b/stdlib/source/library/lux/data/text/slice.lux @@ -0,0 +1,134 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.using + [library + [lux (.except text macro) + [type + ["[0]" nominal]] + [aspect + ["[0]" view (.only View)]] + [math + ["[0]" random (.only Random) (.use "[1]#[0]" functor)] + [number + [/64 + ["n" natural]]]] + [function + [predicate (.only Predicate)]] + [error + ["[0]" try (.only Try)] + ["[0]" exception (.only Exception)]] + [abstract + [equivalence (.only Equivalence)]]]] + ["[0]" //]) + +(nominal.every .public Slice + (Record + [#space Text + #origin Natural + #size Natural]) + + (the .public (whole it) + (-> Text + Slice) + (nominal.of [#space it + #origin 0 + #size (//.size it)])) + + (exception.the .public (cannot_slice [space origin size]) + (Exception [Text Natural Natural]) + (exception.report + (list ["Origin" (by n.base_10 as origin)] + ["Size" (by n.base_10 as size)] + ["Space" (//.as_text space)]))) + + (the .public (partial origin size space) + (-> Natural Natural Text + (Try Slice)) + (if (n.<= (//.size space) (n.+ origin size)) + {try.#Success (nominal.of [#space space + #origin origin + #size size])} + (exception.except ..cannot_slice [space origin size]))) + + (the (as_text it) + (-> Slice + Text) + (let [it (nominal.as it)] + (.text_clip# (its #origin it) + (its #size it) + (its #space it)))) + + (the .public text + (View Slice + Text) + (view.new whole as_text)) + + (the .public size + (-> Slice + Natural) + (|>> nominal.as + (its #size))) + + (the .public empty + Slice + (whole //.empty)) + + (the .public empty? + (Predicate Slice) + (|>> size + (n.= 0))) + + (these (the macro (.in_module# .prelude template#macro)) + (the with_template (.in_module# .prelude with_template)) + (with_template [,name ,slot] + [(the ,name + (macro (_ ,it) + [(its ,slot (nominal.as ,it))]))] + + [[space ..#space] + [origin ..#origin]])) + + (the .public (+ origin it) + (-> Slice + (Change Slice)) + (if (empty? origin) + it + + (empty? it) + origin + + (let [same_space! + (same? (..space origin) + (..space it)) + + contiguity! + (n.= (n.+ (..origin origin) + (..size origin)) + (..origin it))] + (and same_space! + contiguity!)) + (nominal.of [#space (..space origin) + #origin (..origin origin) + #size (n.+ (..size origin) (..size it))]) + + ... else + (whole (.text (as_text origin) (as_text it))))) + + (the .public (random size) + (-> Natural + (Random Slice)) + (|> (random.upper_cased size) + (random#each whole))) + + (the .public (= reference it) + (-> Slice + (Predicate Slice)) + (//.= (as_text reference) + (as_text it))) + + (the .public equivalence + (Equivalence Slice) + (implementation + (the = ..=))) + ) diff --git a/stdlib/source/library/lux/meta/configuration.lux b/stdlib/source/library/lux/meta/configuration.lux index c9ac44ce4..f5114ec21 100644 --- a/stdlib/source/library/lux/meta/configuration.lux +++ b/stdlib/source/library/lux/meta/configuration.lux @@ -19,7 +19,9 @@ [data ["[0]" text (.only) ["%" \\injection] - ["<[1]>" \\projection (.only Projection)]] + ["<[1]>" \\projection (.only Projection)] + ["[0]" slice + ["?[1]" \\projection]]] [collection ["[0]" list (.use "[1]#[0]" functor) ["/" property] @@ -74,7 +76,7 @@ (let [of_text (is (Projection Text) (<| (<>.after (.this ..start)) (<>.before (.this ..end)) - (.slice (.some! (.none_of! ..end)))))] + (?slice.slice (?slice.some (?slice.none_of ..end)))))] (<>.some (<>.and of_text of_text)))) (exception.the .public invalid) diff --git a/stdlib/source/library/lux/physics.lux b/stdlib/source/library/lux/physics.lux index b58f99022..79ee27034 100644 --- a/stdlib/source/library/lux/physics.lux +++ b/stdlib/source/library/lux/physics.lux @@ -4,269 +4,8 @@ ... https://en.wikipedia.org/wiki/Physics (.using [library - [lux (.except) - [math - [number - [/64 - ["/" decimal]]] - [geometry - ["[0]" circle]]] - [macro - ["[0]" template]] - ["[0]" function (.only) - [predicate (.only Predicate)]] - [abstract - [equivalence (.only Equivalence)]]]] - ["[0]" /1]) + [lux (.except)]]) ... https://en.wikipedia.org/wiki/Scalar_(physics) (every .public Scalar - /1.Dimension) - -(every .public (Constant dimension) - dimension) - -(every .public (Variable dimension) - (-> /1.Time - (Constant dimension))) - -... https://en.wikipedia.org/wiki/Velocity -(every .public Velocity - Scalar) - -(the .public (average_velocity interval displacement) - (-> /1.Interval /1.Displacement - Velocity) - (/./ (/1.value interval) - (/1.value displacement))) - -... https://en.wikipedia.org/wiki/Acceleration -(every .public Acceleration - Scalar) - -(the .public (average_acceleration interval difference_in_velocity) - (-> /1.Interval (/1.Difference Velocity) - Acceleration) - (/./ (/1.value interval) - (/1.value difference_in_velocity))) - -(the .public (velocity acceleration initial) - (-> Acceleration Velocity - (Variable Velocity)) - (|>> (/.x acceleration) - (/.+ initial))) - -(the (mean left right) - (-> Scalar Scalar - Scalar) - (/./ +2.0 - (/.+ left right))) - -(the .public (position acceleration initial_velocity initial) - (-> Acceleration Velocity /1.Position - (Variable /1.Position)) - (function (_ time) - (let [velocity (mean initial_velocity - (..velocity acceleration initial_velocity time))] - (|> time - (/.x velocity) - (/.+ initial))))) - -... https://en.wikipedia.org/wiki/Euclidean_vector -(every .public Vector - (Record - [#forward/backward /1.Dimension - #left/right /1.Dimension])) - -(the .public equivalence - (Equivalence Vector) - (implementation - (the (= reference it) - (and (/.= (its #forward/backward reference) - (its #forward/backward it)) - (/.= (its #left/right reference) - (its #left/right it)))))) - -(alias [=] - ..equivalence) - -(the .public origin - Vector - [#forward/backward /1.origin - #left/right /1.origin]) - -(template.with [,vector ,scalar] - [(the .public (,vector origin it) - (-> Vector - (.Change Vector)) - [#forward/backward (,scalar (its #forward/backward origin) - (its #forward/backward it)) - #left/right (,scalar (its #left/right origin) - (its #left/right it))])] - - [[+ /.+] - [- /.-]]) - -(the .public opposite - (.Change Vector) - (|>> (revised #forward/backward /.opposite) - (revised #left/right /.opposite))) - -(template.with [,vector ,scalar] - [(the .public (,vector scale) - (-> Scalar - (.Change Vector)) - (|>> (revised #forward/backward (,scalar scale)) - (revised #left/right (,scalar scale))))] - - [[x /.x] - [/ /./]]) - -(every .public Orientation - Scalar) - -(the (square it) - (.Change /1.Dimension) - (/.x it it)) - -... https://en.wikipedia.org/wiki/Magnitude_(mathematics) -(the .public (magnitude it) - (-> Vector - Scalar) - (/.root_2 (/.+ (square (its #forward/backward it)) - (square (its #left/right it))))) - -... https://en.wikipedia.org/wiki/Orientation_(geometry) -(the .public (orientation it) - (-> Vector - Orientation) - (circle.arc_tangent_2 (its #forward/backward it) - (its #left/right it))) - -(the .public (polar magnitude orientation) - (-> Scalar Orientation - Vector) - (|> [#forward/backward (circle.co_sine orientation) - #left/right (circle.sine orientation)] - (x magnitude))) - -(the .public (approximately? margin_of_error standard value) - (-> Scalar Vector - (Predicate Vector)) - (|> value - (- standard) - magnitude - (/.<= margin_of_error))) - -(the .public (distance from to) - (-> Vector Vector - Scalar) - (magnitude (- from to))) - -(every .public (Difference it) - (.Change it)) - -(the .public value - (-> (Difference Vector) - Vector) - (function.of ..origin)) - -... https://en.wikipedia.org/wiki/Displacement_(geometry) -(every .public Displacement - (Difference Vector)) - -(the .public (difference initial final) - (-> Vector Vector - Displacement) - (+ [#forward/backward (/.- (its #forward/backward initial) - (its #forward/backward final)) - #left/right (/.- (its #left/right initial) - (its #left/right final))])) - -(the .public displacement - difference) - -(the .public (average_velocity' interval displacement) - (-> /1.Interval Displacement - Vector) - (/ (/1.value interval) - (..value displacement))) - -(the .public (average_acceleration' interval difference_in_velocity) - (-> /1.Interval (/1.Difference Vector) - Vector) - (/ (/1.value interval) - (..value difference_in_velocity))) - -(the .public (velocity' acceleration initial) - (-> Vector Vector - (Variable Vector)) - (function (_ time) - (|> acceleration - (x time) - (+ initial)))) - -... https://en.wikipedia.org/wiki/Center_of_mass#Barycentric_coordinates -(the (center_of_mass left right) - (-> Vector Vector - Vector) - (/ +2.0 - (+ left right))) - -(the .public (position' acceleration initial_velocity initial) - (-> Vector Vector Vector - (Variable Vector)) - (function (_ time) - (let [velocity (center_of_mass initial_velocity - (..velocity' acceleration initial_velocity time))] - (|> velocity - (x time) - (+ initial))))) - -(every .public Radius - Scalar) - -... https://en.wikipedia.org/wiki/Centripetal_force -(every .public Centripetal_Acceleration - Scalar) - -(the .public (centripetal_acceleration radius velocity) - (-> Radius Vector - Centripetal_Acceleration) - (/./ radius - (square (magnitude velocity)))) - -... https://en.wikipedia.org/wiki/Frequency#Period_versus_frequency -(every .public Period - Scalar) - -(the .public (period radius velocity) - (-> Radius Vector - Period) - (|> (magnitude velocity) - (/./ radius) - (/.x circle.tau))) - -... https://en.wikipedia.org/wiki/Angular_frequency -(every .public Angular_Speed - Scalar) - -(the .public (angular_speed period) - (-> Period - Angular_Speed) - (/./ period - circle.tau)) - -(the .public (speed radius angular_speed) - (-> Radius Angular_Speed - Scalar) - (/.x radius angular_speed)) - -... https://en.wikipedia.org/wiki/Acceleration#Circular_motion -(every .public Radial_Acceleration - Scalar) - -(the .public (radial_acceleration radius velocity) - (-> Radius Vector - Radial_Acceleration) - (/.opposite (centripetal_acceleration radius velocity))) + Decimal) diff --git a/stdlib/source/library/lux/physics/1.lux b/stdlib/source/library/lux/physics/1.lux index a935f77d6..2540f357d 100644 --- a/stdlib/source/library/lux/physics/1.lux +++ b/stdlib/source/library/lux/physics/1.lux @@ -24,6 +24,15 @@ (every .public Time Dimension) +... https://en.wikipedia.org/wiki/Constant_(mathematics) +(every .public (Constant dimension) + dimension) + +... https://en.wikipedia.org/wiki/Variable_(mathematics) +(every .public (Variable dimension) + (-> Time + (Constant dimension))) + ... https://en.wikipedia.org/wiki/Origin_(mathematics) (the .public origin Dimension @@ -68,3 +77,46 @@ (|> random.integer (each (|>> (integer.% (.integer range)) integer.decimal))))) + +... https://en.wikipedia.org/wiki/Velocity +(every .public Velocity + Dimension) + +(the .public (average_velocity interval displacement) + (-> Interval Displacement + Velocity) + (/./ (value interval) + (value displacement))) + +... https://en.wikipedia.org/wiki/Acceleration +(every .public Acceleration + Dimension) + +(the .public (average_acceleration interval difference_in_velocity) + (-> Interval (Difference Velocity) + Acceleration) + (/./ (value interval) + (value difference_in_velocity))) + +(the .public (velocity acceleration initial) + (-> Acceleration Velocity + (Variable Velocity)) + (|>> (/.x acceleration) + (/.+ initial))) + +... https://en.wikipedia.org/wiki/Center_of_mass#Barycentric_coordinates +(the (center_of_mass left right) + (-> Dimension Dimension + Dimension) + (/./ +2.0 + (/.+ left right))) + +(the .public (position acceleration initial_velocity initial) + (-> Acceleration Velocity Position + (Variable Position)) + (function (_ time) + (let [velocity (center_of_mass initial_velocity + (..velocity acceleration initial_velocity time))] + (|> time + (/.x velocity) + (/.+ initial))))) diff --git a/stdlib/source/library/lux/physics/2.lux b/stdlib/source/library/lux/physics/2.lux new file mode 100644 index 000000000..ce6a9a3cf --- /dev/null +++ b/stdlib/source/library/lux/physics/2.lux @@ -0,0 +1,301 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.using + [library + [lux (.except) + [math + [number + [/64 + ["/" decimal]]] + [geometry + ["[0]" circle]]] + [macro + ["[0]" template]] + ["[0]" function (.only) + [predicate (.only Predicate)]] + [abstract + [equivalence (.only Equivalence)]] + [math + ["[0]" random (.only Random)]]]] + [// + ["[0]" /1]]) + +... https://en.wikipedia.org/wiki/Euclidean_vector +(every .public Vector + (Record + [#forward/backward /1.Dimension + #left/right /1.Dimension])) + +(the .public (random range) + (-> Natural + (Random Vector)) + (random.and (/1.random range) + (/1.random range))) + +(the .public (= reference it) + (-> Vector + (Predicate Vector)) + (and (/.= (its #forward/backward reference) + (its #forward/backward it)) + (/.= (its #left/right reference) + (its #left/right it)))) + +(the .public equivalence + (Equivalence Vector) + (implementation + (the = ..=))) + +(every .public Position + Vector) + +(the .public origin + Position + [#forward/backward /1.origin + #left/right /1.origin]) + +(template.with [,vector ,scalar] + [(the .public (,vector origin it) + (-> Vector + (.Change Vector)) + [#forward/backward (,scalar (its #forward/backward origin) + (its #forward/backward it)) + #left/right (,scalar (its #left/right origin) + (its #left/right it))])] + + [[+ /.+] + [- /.-]]) + +(the .public opposite + (.Change Vector) + (|>> (revised #forward/backward /.opposite) + (revised #left/right /.opposite))) + +(template.with [,vector ,scalar] + [(the .public (,vector scale) + (-> /1.Dimension + (.Change Vector)) + (|>> (revised #forward/backward (,scalar scale)) + (revised #left/right (,scalar scale))))] + + [[x /.x] + [/ /./]]) + +(every .public Orientation + /1.Dimension) + +(the (square it) + (.Change /1.Dimension) + (/.x it it)) + +... https://en.wikipedia.org/wiki/Magnitude_(mathematics) +(the .public (magnitude it) + (-> Vector + /1.Dimension) + (/.root_2 (/.+ (square (its #forward/backward it)) + (square (its #left/right it))))) + +... https://en.wikipedia.org/wiki/Orientation_(geometry) +(the .public (orientation it) + (-> Vector + Orientation) + (circle.arc_tangent_2 (its #forward/backward it) + (its #left/right it))) + +(the .public (polar magnitude orientation) + (-> /1.Dimension Orientation + Vector) + (|> [#forward/backward (circle.co_sine orientation) + #left/right (circle.sine orientation)] + (x magnitude))) + +(the .public (approximately? margin_of_error standard value) + (-> /1.Dimension Vector + (Predicate Vector)) + (|> value + (- standard) + magnitude + (/.<= margin_of_error))) + +(the .public (distance from to) + (-> Position Position + /1.Dimension) + (magnitude (- from to))) + +(every .public (Difference it) + (.Change it)) + +(the .public value + (-> (Difference Vector) + Vector) + (function.of ..origin)) + +(the .public (difference initial final) + (-> Vector Vector + (Difference Vector)) + (+ [#forward/backward (/.- (its #forward/backward initial) + (its #forward/backward final)) + #left/right (/.- (its #left/right initial) + (its #left/right final))])) + +... https://en.wikipedia.org/wiki/Displacement_(geometry) +(every .public Displacement + (Difference Position)) + +(the .public displacement + difference) + +(every .public Velocity + Vector) + +(the .public (average_velocity interval displacement) + (-> /1.Interval Displacement + Velocity) + (/ (/1.value interval) + (..value displacement))) + +(every .public Acceleration + Vector) + +(the .public (average_acceleration interval difference_in_velocity) + (-> /1.Interval (/1.Difference Velocity) + Acceleration) + (/ (/1.value interval) + (..value difference_in_velocity))) + +(the .public (velocity acceleration initial) + (-> Acceleration Velocity + (/1.Variable Velocity)) + (function (_ time) + (|> acceleration + (x time) + (+ initial)))) + +... https://en.wikipedia.org/wiki/Center_of_mass#Barycentric_coordinates +(the (center_of_mass left right) + (-> Vector Vector + Vector) + (/ +2.0 + (+ left right))) + +(the .public (position acceleration initial_velocity initial) + (-> Acceleration Velocity Position + (/1.Variable Position)) + (function (_ time) + (let [velocity (center_of_mass initial_velocity + (..velocity acceleration initial_velocity time))] + (|> velocity + (x time) + (+ initial))))) + +(every .public Radius + /1.Dimension) + +... https://en.wikipedia.org/wiki/Centripetal_force +(every .public Centripetal_Acceleration + /1.Dimension) + +(the .public (centripetal_acceleration radius velocity) + (-> Radius Velocity + Centripetal_Acceleration) + (/./ radius + (square (magnitude velocity)))) + +... https://en.wikipedia.org/wiki/Frequency#Period_versus_frequency +(every .public Period + /1.Dimension) + +(the .public (period radius velocity) + (-> Radius Velocity + Period) + (|> (magnitude velocity) + (/./ radius) + (/.x circle.tau))) + +... https://en.wikipedia.org/wiki/Angular_frequency +(every .public Angular_Speed + /1.Dimension) + +(the .public (angular_speed period) + (-> Period + Angular_Speed) + (/./ period + circle.tau)) + +(every .public Speed + /1.Dimension) + +(the .public (speed radius angular_speed) + (-> Radius Angular_Speed + Speed) + (/.x radius angular_speed)) + +... https://en.wikipedia.org/wiki/Acceleration#Circular_motion +(every .public Radial_Acceleration + /1.Dimension) + +(the .public (radial_acceleration radius velocity) + (-> Radius Velocity + Radial_Acceleration) + (/.opposite (centripetal_acceleration radius velocity))) + +(every .public Mass + /1.Dimension) + +(every .public Force + Vector) + +(the .public (net_force acceleration mass) + (-> Acceleration Mass + Force) + (x mass acceleration)) + +... (the law_2_of_newton +... Algebra +... (with_variables [net_force mass acceleration] +... (= net_force +... (x mass acceleration)))) + +(the .public gravitational_acceleration + Acceleration + [#forward/backward -9.8 + #left/right /1.origin]) + +(the .public gravitational_force + (-> Mass + Force) + (net_force gravitational_acceleration)) + +... https://en.wikipedia.org/wiki/Friction +(every .public Static_Friction + /1.Dimension) + +(the .public (static_friction coefficient normal_force) + (-> Static_Friction Force + Force) + (x coefficient normal_force)) + +(every .public Kinetic_Friction + /1.Dimension) + +(the .public (kinetic_friction coefficient normal_force) + (-> Kinetic_Friction Force + Force) + (x coefficient normal_force)) + +(template.with [,static ,kinetic ,surface ,material] + [(`` (the .public (,, (template.name ["static_friction_of_" ,material "_on_" ,surface])) + Static_Friction + ,static)) + + (`` (the .public (,, (template.name ["kinetic_friction_of_" ,material "_on_" ,surface])) + Kinetic_Friction + ,static))] + + [[+1.00 +0.80 concrete rubber] + [+0.78 +0.57 steel steel] + [+0.61 +0.47 steel aluminium] + [+0.53 +0.36 steel copper] + [+0.94 +0.40 glass glass] + [+0.14 +0.10 wet_snow waxed_wood] + [+0.10 +0.03 ice ice]]) diff --git a/stdlib/source/library/lux/target/jvm/type/projection.lux b/stdlib/source/library/lux/target/jvm/type/projection.lux index ea9ed5fd2..5db934b2d 100644 --- a/stdlib/source/library/lux/target/jvm/type/projection.lux +++ b/stdlib/source/library/lux/target/jvm/type/projection.lux @@ -14,7 +14,9 @@ ["[0]" product] ["[0]" text ["%" \\injection] - ["<[1]>" \\projection (.only Projection)]] + ["<[1]>" \\projection (.only Projection)] + ["[0]" slice + ["?[1]" \\projection]]] [collection ["[0]" list]]] [macro @@ -76,8 +78,8 @@ [(the .public (Projection ) (by <>.functor each - (.slice (.and! (.one_of! ) - (.some! (.one_of! ))))))] + (?slice.slice (?slice.and (?slice.one_of ) + (?slice.some (?slice.one_of ))))))] [[external.Name class_name class/set class/set (|>> internal.name internal.external)] [Text var_name var/head var/tail function.identity]]) diff --git a/stdlib/source/projection/lux/data/text.lux b/stdlib/source/projection/lux/data/text.lux index 68dac0961..f3f449f67 100644 --- a/stdlib/source/projection/lux/data/text.lux +++ b/stdlib/source/projection/lux/data/text.lux @@ -7,15 +7,13 @@ [abstract [monad (.only Monad do)] - ["//" projection]] + ["?" projection]] [control ["[0]" maybe]] [error ["[0]" try (.only Try)] ["[0]" exception (.only Exception)]] [data - ["/" text (.use "[1]#[0]" monoid) - [character (.only Character)]] ["[0]" product] [collection ["[0]" list (.use "[1]#[0]" mix)]]] @@ -26,8 +24,10 @@ [macro ["^" pattern] ["[0]" template]] - [meta - ["[0]" code]]]]) + [function + [predicate (.only Predicate)]]]] + ["/" \\library (.use "[1]#[0]" monoid) + [character (.only Character)]]) (every .public Offset Natural) @@ -37,33 +37,16 @@ 0) (exception.the .public cannot_parse) -(exception.the .public cannot_slice) (every .public Projection - (//.Projection [Offset Text])) - -(every .public Slice - (Record - [#basis Offset - #distance Offset])) - -(the .public (slice projection) - (-> (Projection Slice) - (Projection Text)) - (do //.monad - [[basis distance] projection] - (function (_ (^.let input [offset tape])) - (when (/.clip basis distance tape) - {.#Some output} - {try.#Success [input output]} - - {.#None} - (exception.except ..cannot_slice []))))) + (?.Projection [Offset Text])) (the (left_over offset tape) (-> Offset Text Text) - (|> tape (/.clip_since offset) maybe.trusted)) + (|> tape + (/.clip_since offset) + maybe.trusted)) (exception.the .public (unconsumed_input [offset tape]) (Exception [Offset Text]) @@ -96,19 +79,6 @@ (function (_ (^.let input [offset tape])) {try.#Success [input offset]})) -(the (with_slices projection) - (-> (Projection (List Slice)) - (Projection Slice)) - (do //.monad - [offset ..offset - slices projection] - (pure (list#mix (function (_ [slice::basis slice::distance] - [total::basis total::distance]) - [total::basis (.i64_+# slice::distance total::distance)]) - [#basis offset - #distance 0] - slices)))) - (the .public any (Projection Text) (function (_ [offset tape]) @@ -119,34 +89,17 @@ _ (exception.except ..cannot_parse [])))) -(the .public any! - (Projection Slice) - (function (_ [offset tape]) - (when (/.character offset tape) - {.#Some _} - {try.#Success [[(.i64_+# 1 offset) tape] - [#basis offset - #distance 1]]} - +(the .public (not projection) + (for_any (_ it) + (-> (Projection it) + (Projection Text))) + (function (_ input) + (when (projection input) + {try.#Failure msg} + (..any input) + _ - (exception.except ..cannot_slice [])))) - -(template.with [ ] - [(`` (the .public ( projection) - (for_any (_ it) - (-> (Projection it) - (Projection ))) - (function (_ input) - (when (projection input) - {try.#Failure msg} - ( input) - - _ - (exception.except ..expected_to_fail input)))))] - - [[not Text ..any] - [not! Slice ..any!]] - ) + (exception.except ..expected_to_fail [input])))) (exception.the .public (cannot_match reference) (Exception Text) @@ -192,61 +145,34 @@ (the .public (range minimum maximum) (-> Natural Natural (Projection Text)) - (do //.monad - [character any - .let [character' (maybe.trusted (/.character 0 character))] - _ (//.assertion (all /#composite "Character is not within range: " (/.of_character minimum) "-" (/.of_character maximum)) - (.and (n.>= minimum character') - (n.<= maximum character')))] - (pure character))) - -(the .public (range! minimum maximum) - (-> Natural Natural - (Projection Slice)) - (do //.monad - [it ..any! - character (..slice (pure it)) - .let [character' (maybe.trusted (/.character 0 character))] - _ (//.assertion (all /#composite "Character is not within range: " (/.of_character minimum) "-" (/.of_character maximum)) - (.and (n.>= minimum character') - (n.<= maximum character')))] + (do ?.monad + [it ..any + .let [character (maybe.trusted (/.character 0 it))] + _ (?.assertion (all /#composite "Character is not within range: " (/.of_character minimum) "-" (/.of_character maximum)) + (.and (n.>= minimum character) + (n.<= maximum character)))] (pure it))) -(template.with [ ] +(template.with [ ] [(the .public (Projection Text) - (..range (character ) (character ))) - - (the .public - (Projection Slice) - (..range! (character ) (character )))] - - [["A" "Z" upper upper!] - ["a" "z" lower lower!] - ["0" "9" decimal decimal!] - ["0" "7" octal octal!]] - ) + (..range (character ) (character )))] -(the .public alpha (Projection Text) (//.either ..lower ..upper)) -(the .public alpha! (Projection Slice) (//.either ..lower! ..upper!)) + [["A" "Z" upper] + ["a" "z" lower] + ["0" "9" decimal] + ["0" "7" octal]]) -(the .public alpha_num (Projection Text) (//.either ..alpha ..decimal)) -(the .public alpha_num! (Projection Slice) (//.either ..alpha! ..decimal!)) +(the .public alpha (Projection Text) (?.either ..lower ..upper)) +(the .public alpha_num (Projection Text) (?.either ..alpha ..decimal)) (the .public hexadecimal (Projection Text) - (all //.either + (all ?.either ..decimal (..range (character "a") (character "f")) (..range (character "A") (character "F")))) -(the .public hexadecimal! - (Projection Slice) - (all //.either - ..decimal! - (..range! (character "a") (character "f")) - (..range! (character "A") (character "F")))) - (template.with [] [(exception.the .public ( [options character]) (Exception [Text Character]) @@ -255,8 +181,7 @@ ["Character" (/.as_text (/.of_character character))])))] [[character_should_be] - [character_should_not_be]] - ) + [character_should_not_be]]) (template.with [ ] [(the .public ( options) @@ -274,29 +199,7 @@ (exception.except ..cannot_parse []))))] [[one_of |> ..character_should_be] - [none_of .not ..character_should_not_be]] - ) - -(template.with [ ] - [(the .public ( options) - (-> Text - (Projection Slice)) - (function (_ [offset tape]) - (when (/.character offset tape) - {.#Some output} - (let [output' (/.of_character output)] - (if ( (/.contains? output' options)) - {try.#Success [[(.i64_+# 1 offset) tape] - [#basis offset - #distance 1]]} - (exception.except [options output]))) - - _ - (exception.except ..cannot_slice []))))] - - [[one_of! |> ..character_should_be] - [none_of! .not ..character_should_not_be]] - ) + [none_of .not ..character_should_not_be]]) (exception.the .public (character_does_not_satisfy_predicate character) (Exception Character) @@ -304,7 +207,7 @@ (list ["Character" (/.as_text (/.of_character character))]))) (the .public (satisfies projection) - (-> (-> Character Bit) + (-> (Predicate Character) (Projection Text)) (function (_ [offset tape]) (when (/.character offset tape) @@ -316,65 +219,28 @@ _ (exception.except ..cannot_parse [])))) -(the .public (satisfies! projection) - (-> (-> Character Bit) - (Projection Slice)) - (function (_ [offset tape]) - (when (/.character offset tape) - {.#Some output} - (if (projection output) - {try.#Success [[(.i64_+# 1 offset) tape] - [#basis offset #distance 1]]} - (exception.except ..character_does_not_satisfy_predicate [output])) - - _ - (exception.except ..cannot_parse [])))) - (the .public space (Projection Text) (..satisfies /.space?)) -(the .public space! - (Projection Slice) - (..satisfies! /.space?)) - (the .public (and left right) (-> (Projection Text) (Projection Text) (Projection Text)) - (do //.monad + (do ?.monad [=left left =right right] (pure (all /#composite =left =right)))) -(the .public (and! left right) - (-> (Projection Slice) (Projection Slice) - (Projection Slice)) - (do //.monad - [(open "left[0]") left - (open "right[0]") right] - (pure [left#basis (.i64_+# left#distance right#distance)]))) - (template.with [ ] [(the .public ( projection) (-> (Projection Text) (Projection Text)) (|> projection - (by //.monad each /.together)))] - - [[some //.some "some"] - [many //.many "many"]] - ) - -(template.with [ ] - [(the .public ( projection) - (-> (Projection Slice) - (Projection Slice)) - (with_slices ( projection)))] + (by ?.monad each /.together)))] - [[some! //.some "some"] - [many! //.many "many"]] - ) + [[some ?.some "some"] + [many ?.many "many"]]) (template.with [ ] [(the .public ( amount projection) @@ -382,45 +248,26 @@ (Projection Text)) (|> projection ( amount) - (by //.monad each /.together)))] + (by ?.monad each /.together)))] - [[exactly //.exactly "exactly"] - [at_most //.at_most "at most"] - [at_least //.at_least "at least"]] - ) - -(template.with [ ] - [(the .public ( amount projection) - (-> Natural (Projection Slice) - (Projection Slice)) - (with_slices - ( amount projection)))] - - [[exactly! //.exactly "exactly"] - [at_most! //.at_most "at most"] - [at_least! //.at_least "at least"]] - ) + [[exactly ?.exactly "exactly"] + [at_most ?.at_most "at most"] + [at_least ?.at_least "at least"]]) (the .public (between minimum additional projection) (-> Natural Natural (Projection Text) (Projection Text)) (|> projection - (//.between minimum additional) - (by //.monad each /.together))) - -(the .public (between! minimum additional projection) - (-> Natural Natural (Projection Slice) - (Projection Slice)) - (with_slices - (//.between minimum additional projection))) + (?.between minimum additional) + (by ?.monad each /.together))) (the .public (enclosed [start end] projection) (for_any (_ it) (-> [Text Text] (Projection it) (Projection it))) (|> projection - (//.before (this end)) - (//.after (this start)))) + (?.before (this end)) + (?.after (this start)))) (the .public (local local_input projection) (for_any (_ it) @@ -437,8 +284,8 @@ (the .public (then structured text) (for_any (_ state it) (-> (Projection it) - (//.Projection state Text) - (//.Projection state it))) - (do //.monad + (?.Projection state Text) + (?.Projection state it))) + (do ?.monad [raw text] - (//.of_try (..value structured raw)))) + (?.of_try (..value structured raw)))) diff --git a/stdlib/source/projection/lux/data/text/slice.lux b/stdlib/source/projection/lux/data/text/slice.lux new file mode 100644 index 000000000..d22be50a2 --- /dev/null +++ b/stdlib/source/projection/lux/data/text/slice.lux @@ -0,0 +1,165 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.using + [library + [lux (.except and not) + [abstract + [monad (.only do)] + ["?" projection (.use "[1]#[0]" functor)]] + [control + ["[0]" maybe]] + [error + ["[0]" try (.use "[1]#[0]" functor)] + ["[0]" exception]] + [data + [collection + ["[0]" list (.use "[1]#[0]" mix)]]] + [math + [number + [/64 + ["n" natural]]]] + [macro + ["[0]" template]] + [aspect + ["[0]" view]] + ["[0]" function (.only) + [predicate (.only Predicate)]]]] + ["[0]" // (.only Projection)] + ["/" \\library (.only) + ["[1][0]" // (.only) + [character (.only Character)]]]) + +(the .public slice + (-> (Projection /.Slice) + (Projection Text)) + (?#each (view.as /.text))) + +(the with_slices + (-> (Projection (List /.Slice)) + (Projection /.Slice)) + (?#each (list#mix (function.flipped /.+) + /.empty))) + +(the .public any + (Projection /.Slice) + (function (_ [offset tape]) + (try#each (|>> [[(.i64_+# 1 offset) tape]]) + (/.partial offset 1 tape)))) + +(the .public (not projection) + (for_any (_ it) + (-> (Projection it) + (Projection /.Slice))) + (function (_ input) + (when (projection input) + {try.#Failure msg} + (..any input) + + _ + (exception.except //.expected_to_fail [input])))) + +(the .public (range minimum maximum) + (-> Natural Natural + (Projection /.Slice)) + (do ?.monad + [it ..any + .let [character (maybe.trusted (///.character 0 (view.as /.text it)))] + _ (?.assertion (text "Character is not within range: " (///.of_character minimum) "-" (///.of_character maximum)) + (.and (n.>= minimum character) + (n.<= maximum character)))] + (pure it))) + +(template.with [ ] + [(the .public + (Projection /.Slice) + (..range (character ) + (character )))] + + [["A" "Z" upper] + ["a" "z" lower] + ["0" "9" decimal] + ["0" "7" octal]]) + +(the .public alpha + (Projection /.Slice) + (?.either ..lower ..upper)) + +(the .public alpha_num + (Projection /.Slice) + (?.either ..alpha ..decimal)) + +(the .public hexadecimal + (Projection /.Slice) + (all ?.either + ..decimal + (..range (character "a") (character "f")) + (..range (character "A") (character "F")))) + +(template.with [ ] + [(the .public ( options) + (-> Text + (Projection /.Slice)) + (function (_ [offset tape]) + (when (///.character offset tape) + {.#Some output} + (let [output' (///.of_character output)] + (if ( (///.contains? output' options)) + (try#each (|>> [[(.i64_+# 1 offset) tape]]) + (/.partial offset 1 tape)) + (exception.except [options output]))) + + _ + (exception.except /.cannot_slice [tape offset 1]))))] + + [[one_of |> //.character_should_be] + [none_of .not //.character_should_not_be]]) + +(the .public (satisfies projection) + (-> (Predicate Character) + (Projection /.Slice)) + (function (_ [offset tape]) + (when (///.character offset tape) + {.#Some output} + (if (projection output) + (try#each (|>> [[(.i64_+# 1 offset) tape]]) + (/.partial offset 1 tape)) + (exception.except //.character_does_not_satisfy_predicate [output])) + + _ + (exception.except //.cannot_parse [])))) + +(the .public space + (Projection /.Slice) + (..satisfies ///.space?)) + +(the .public (and left right) + (-> (Projection /.Slice) + (Change (Projection /.Slice))) + (do ?.monad + [left left + right right] + (pure (/.+ left right)))) + +(template.with [ ] + [(the .public ( projection) + (Change (Projection /.Slice)) + (with_slices ( projection)))] + + [[some ?.some] + [many ?.many]]) + +(template.with [ ] + [(the .public ( amount projection) + (-> Natural + (Change (Projection /.Slice))) + (with_slices ( amount projection)))] + + [[exactly ?.exactly] + [at_most ?.at_most] + [at_least ?.at_least]]) + +(the .public (between minimum additional projection) + (-> Natural Natural + (Change (Projection /.Slice))) + (with_slices (?.between minimum additional projection))) diff --git a/stdlib/source/test/lux/data/text.lux b/stdlib/source/test/lux/data/text.lux index a4509b1b5..13840019d 100644 --- a/stdlib/source/test/lux/data/text.lux +++ b/stdlib/source/test/lux/data/text.lux @@ -72,7 +72,8 @@ ["[1][0]" regex] ["[1][0]" escape] ["[1][0]" unicode - ["[1]" set]]] + ["[1]" set]] + ["[1][0]" slice]] [\\library ["[0]" /]] ["[0]" \\projection] @@ -246,10 +247,6 @@ (by try.functor each (/.= expected)) (try.else false))) -(the (should_pass! expected projection) - (-> Text (\\projection.Projection \\projection.Slice) Bit) - (..should_pass expected (\\projection.slice projection))) - (the \\projection#character_classes Test (all _.and @@ -348,17 +345,11 @@ invalid (random.only (function (_ character) (not (/.contains? (/.of_character character) options))) (random.character unicode.character))] - (_.coverage [\\projection.one_of \\projection.one_of! \\projection.character_should_be] + (_.coverage [\\projection.one_of \\projection.character_should_be] (and (..should_pass (/.of_character expected) (\\projection.one_of options)) (..should_fail (/.of_character invalid) (\\projection.one_of options)) (..should_fail' (/.of_character invalid) (\\projection.one_of options) - \\projection.character_should_be) - - (..should_pass! (/.of_character expected) (\\projection.one_of! options)) - (..should_fail (/.of_character invalid) (\\projection.one_of! options)) - (..should_fail' (/.of_character invalid) (\\projection.one_of! options) - \\projection.character_should_be) - ))) + \\projection.character_should_be)))) (do [! random.monad] [.let [num_options 3] options (|> (random.character unicode.character) @@ -374,104 +365,78 @@ expected (random.only (function (_ character) (not (/.contains? (/.of_character character) options))) (random.character unicode.character))] - (_.coverage [\\projection.none_of \\projection.none_of! \\projection.character_should_not_be] + (_.coverage [\\projection.none_of + \\projection.character_should_not_be] (and (..should_pass (/.of_character expected) (\\projection.none_of options)) (..should_fail (/.of_character invalid) (\\projection.none_of options)) (..should_fail' (/.of_character invalid) (\\projection.none_of options) \\projection.character_should_not_be) - - (..should_pass! (/.of_character expected) (\\projection.none_of! options)) - (..should_fail (/.of_character invalid) (\\projection.none_of! options)) - (..should_fail' (/.of_character invalid) (\\projection.none_of! options) - \\projection.character_should_not_be) ))) )) (the \\projection#runs Test - (let [octal! (\\projection.one_of! "01234567")] - (all _.and - (do [! random.monad] - [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) - right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) - .let [expected (.text left right)] - invalid (|> random.natural - (by ! each (natural.% 16)) - (random.only (natural.>= 8)) - (by ! each (by natural.hex as)))] - (_.coverage [\\projection.many \\projection.many!] - (and (..should_pass expected (\\projection.many \\projection.octal)) - (..should_fail invalid (\\projection.many \\projection.octal)) - - (..should_pass! expected (\\projection.many! octal!))))) - (do [! random.monad] - [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) - right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) - .let [expected (.text left right)] - invalid (|> random.natural - (by ! each (natural.% 16)) - (random.only (natural.>= 8)) - (by ! each (by natural.hex as)))] - (_.coverage [\\projection.some \\projection.some!] - (and (..should_pass expected (\\projection.some \\projection.octal)) - (..should_pass "" (\\projection.some \\projection.octal)) - (..should_fail invalid (\\projection.some \\projection.octal)) - - (..should_pass! expected (\\projection.some! octal!)) - (..should_pass! "" (\\projection.some! octal!))))) - (do [! random.monad] - [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] - first octal - second octal - third octal] - (_.coverage [\\projection.exactly \\projection.exactly!] - (and (..should_pass (.text first second) (\\projection.exactly 2 \\projection.octal)) - (..should_fail (.text first second third) (\\projection.exactly 2 \\projection.octal)) - (..should_fail (.text first) (\\projection.exactly 2 \\projection.octal)) - - (..should_pass! (.text first second) (\\projection.exactly! 2 octal!)) - (..should_fail (.text first second third) (\\projection.exactly! 2 octal!)) - (..should_fail (.text first) (\\projection.exactly! 2 octal!))))) - (do [! random.monad] - [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] - first octal - second octal - third octal] - (_.coverage [\\projection.at_most \\projection.at_most!] - (and (..should_pass (.text first second) (\\projection.at_most 2 \\projection.octal)) - (..should_pass (.text first) (\\projection.at_most 2 \\projection.octal)) - (..should_fail (.text first second third) (\\projection.at_most 2 \\projection.octal)) - - (..should_pass! (.text first second) (\\projection.at_most! 2 octal!)) - (..should_pass! (.text first) (\\projection.at_most! 2 octal!)) - (..should_fail (.text first second third) (\\projection.at_most! 2 octal!))))) - (do [! random.monad] - [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] - first octal - second octal - third octal] - (_.coverage [\\projection.at_least \\projection.at_least!] - (and (..should_pass (.text first second) (\\projection.at_least 2 \\projection.octal)) - (..should_pass (.text first second third) (\\projection.at_least 2 \\projection.octal)) - (..should_fail (.text first) (\\projection.at_least 2 \\projection.octal)) - - (..should_pass! (.text first second) (\\projection.at_least! 2 octal!)) - (..should_pass! (.text first second third) (\\projection.at_least! 2 octal!)) - (..should_fail (.text first) (\\projection.at_least! 2 octal!))))) - (do [! random.monad] - [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] - first octal - second octal - third octal] - (_.coverage [\\projection.between \\projection.between!] - (and (..should_pass (.text first second) (\\projection.between 2 1 \\projection.octal)) - (..should_pass (.text first second third) (\\projection.between 2 1 \\projection.octal)) - (..should_fail (.text first) (\\projection.between 2 1 \\projection.octal)) - - (..should_pass! (.text first second) (\\projection.between! 2 1 octal!)) - (..should_pass! (.text first second third) (\\projection.between! 2 1 octal!)) - (..should_fail (.text first) (\\projection.between! 2 1 octal!))))) - ))) + (all _.and + (do [! random.monad] + [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + .let [expected (.text left right)] + invalid (|> random.natural + (by ! each (natural.% 16)) + (random.only (natural.>= 8)) + (by ! each (by natural.hex as)))] + (_.coverage [\\projection.many] + (and (..should_pass expected (\\projection.many \\projection.octal)) + (..should_fail invalid (\\projection.many \\projection.octal))))) + (do [! random.monad] + [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + .let [expected (.text left right)] + invalid (|> random.natural + (by ! each (natural.% 16)) + (random.only (natural.>= 8)) + (by ! each (by natural.hex as)))] + (_.coverage [\\projection.some] + (and (..should_pass expected (\\projection.some \\projection.octal)) + (..should_pass "" (\\projection.some \\projection.octal)) + (..should_fail invalid (\\projection.some \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.exactly] + (and (..should_pass (.text first second) (\\projection.exactly 2 \\projection.octal)) + (..should_fail (.text first second third) (\\projection.exactly 2 \\projection.octal)) + (..should_fail (.text first) (\\projection.exactly 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.at_most] + (and (..should_pass (.text first second) (\\projection.at_most 2 \\projection.octal)) + (..should_pass (.text first) (\\projection.at_most 2 \\projection.octal)) + (..should_fail (.text first second third) (\\projection.at_most 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.at_least] + (and (..should_pass (.text first second) (\\projection.at_least 2 \\projection.octal)) + (..should_pass (.text first second third) (\\projection.at_least 2 \\projection.octal)) + (..should_fail (.text first) (\\projection.at_least 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.between] + (and (..should_pass (.text first second) (\\projection.between 2 1 \\projection.octal)) + (..should_pass (.text first second third) (\\projection.between 2 1 \\projection.octal)) + (..should_fail (.text first) (\\projection.between 2 1 \\projection.octal))))) + )) (the \\projection Test @@ -500,19 +465,11 @@ dummy) (!expect (^.multi {try.#Failure error} (exception.is? \\projection.cannot_match error))))))) - (_.coverage [\\projection.Slice \\projection.slice \\projection.cannot_slice] - (|> "" - (\\projection.value (\\projection.slice \\projection.any!)) - (!expect (^.multi {try.#Failure error} - (exception.is? \\projection.cannot_slice error))))) (do [! random.monad] [expected (random.unicode 1)] - (_.coverage [\\projection.any \\projection.any!] + (_.coverage [\\projection.any] (and (..should_pass expected \\projection.any) - (..should_fail "" \\projection.any) - - (..should_pass! expected \\projection.any!) - (..should_fail "" \\projection.any!)))) + (..should_fail "" \\projection.any)))) (do [! random.monad] [expected (random.unicode 1)] (_.coverage [\\projection.next \\projection.cannot_parse] @@ -581,18 +538,12 @@ [invalid (random.upper_cased 1) expected (random.only (|>> (unicode/block.within? unicode/block.upper_case) not) - (random.character unicode.character)) - .let [upper! (\\projection.one_of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ")]] - (_.coverage [\\projection.not \\projection.not! \\projection.expected_to_fail] + (random.character unicode.character))] + (_.coverage [\\projection.not + \\projection.expected_to_fail] (and (..should_pass (/.of_character expected) (\\projection.not \\projection.upper)) (|> invalid (\\projection.value (\\projection.not \\projection.upper)) - (!expect (^.multi {try.#Failure error} - (exception.is? \\projection.expected_to_fail error)))) - - (..should_pass! (/.of_character expected) (\\projection.not! upper!)) - (|> invalid - (\\projection.value (\\projection.not! upper!)) (!expect (^.multi {try.#Failure error} (exception.is? \\projection.expected_to_fail error))))))) (do [! random.monad] @@ -601,21 +552,16 @@ invalid (random.only (function (_ character) (not (or (unicode/block.within? unicode/block.upper_case character) (unicode/block.within? unicode/block.lower_case character)))) - (random.character unicode.character)) - .let [upper! (\\projection.one_of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ") - lower! (\\projection.one_of! "abcdefghijklmnopqrstuvwxyz")]] - (_.coverage [\\projection.and \\projection.and!] + (random.character unicode.character))] + (_.coverage [\\projection.and] (and (..should_pass (.text upper lower) (\\projection.and \\projection.upper \\projection.lower)) (..should_fail (.text (/.of_character invalid) lower) (\\projection.and \\projection.upper \\projection.lower)) - (..should_fail (.text upper (/.of_character invalid)) (\\projection.and \\projection.upper \\projection.lower)) - - (..should_pass! (.text upper lower) (\\projection.and! upper! lower!)) - (..should_fail (.text (/.of_character invalid) lower) (\\projection.and! upper! lower!)) - (..should_fail (.text upper (/.of_character invalid)) (\\projection.and! upper! lower!))))) + (..should_fail (.text upper (/.of_character invalid)) (\\projection.and \\projection.upper \\projection.lower))))) (do [! random.monad] [expected (random.unicode 1) invalid (random.unicode 1)] - (_.coverage [\\projection.satisfies \\projection.character_does_not_satisfy_predicate] + (_.coverage [\\projection.satisfies + \\projection.character_does_not_satisfy_predicate] (and (..should_pass expected (\\projection.satisfies (function.constant true))) (..should_fail' invalid (\\projection.satisfies (function.constant false)) \\projection.character_does_not_satisfy_predicate)))) @@ -929,6 +875,7 @@ /regex.test /escape.test /unicode.test + /slice.test ..\\projection ..\\injection diff --git a/stdlib/source/test/lux/data/text/slice.lux b/stdlib/source/test/lux/data/text/slice.lux new file mode 100644 index 000000000..3f994683a --- /dev/null +++ b/stdlib/source/test/lux/data/text/slice.lux @@ -0,0 +1,300 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.using + [library + [lux (.except) + [abstract + [monad (.only do)] + ["[0]" equivalence (.only) + ["[1]T" \\test]]] + [math + ["[0]" random] + [number + [/64 + ["[0]" natural]]]] + [test + ["_" property (.only Test)]] + [aspect + ["[0]" view (.only) + ["[1]T" \\test]]] + [data + ["[0]" text (.use "[1]#[0]" monoid) + ["[0]" unicode + ["[1]" set] + ["[1]/[0]" block]]] + [collection + ["[0]" list (.use "[1]#[0]" functor)] + ["[0]" set]]] + [meta + ["[0]" static]] + [error + ["[0]" try] + ["[0]" exception (.only Exception)]] + [control + ["[0]" maybe]] + ["[0]" function] + [macro + ["^" pattern] + ["[0]" template]]]] + [\\library + ["[0]" /]] + ["[0]" \\projection (.only) + ["[0]" //]]) + +(the !expect + (template.macro (_ ) + [(when + + true + + _ + false)])) + +(the (should_fail' sample projection exception) + (for_any (_ a e) + (-> Text (//.Projection a) (Exception e) + Bit)) + (when (//.value projection sample) + {try.#Failure error} + (exception.is? exception error) + + _ + false)) + +(the (should_fail sample projection) + (for_any (_ a) + (-> Text (//.Projection a) + Bit)) + (when (//.value projection sample) + {try.#Failure _} + true + + _ + false)) + +(the (should_pass expected projection) + (-> Text (//.Projection /.Slice) Bit) + (|> expected + (//.value (\\projection.slice projection)) + (by try.functor each (text.= expected)) + (try.else false))) + +(the \\projection#character_classes + Test + (all _.and + (do [! random.monad] + [.let [num_options 3] + options (|> (random.character unicode.character) + (random.set natural.hash num_options) + (by ! each (|>> set.as_list + (list#each text.of_character) + text.together))) + expected (by ! each (function (_ value) + (|> options + (text.character (natural.% num_options value)) + maybe.trusted)) + random.natural) + invalid (random.only (function (_ character) + (not (text.contains? (text.of_character character) options))) + (random.character unicode.character))] + (_.coverage [\\projection.one_of] + (and (..should_pass (text.of_character expected) (\\projection.one_of options)) + (..should_fail (text.of_character invalid) (\\projection.one_of options)) + (..should_fail' (text.of_character invalid) (\\projection.one_of options) + //.character_should_be)))) + (do [! random.monad] + [.let [num_options 3] + options (|> (random.character unicode.character) + (random.set natural.hash num_options) + (by ! each (|>> set.as_list + (list#each text.of_character) + text.together))) + invalid (by ! each (function (_ value) + (|> options + (text.character (natural.% num_options value)) + maybe.trusted)) + random.natural) + expected (random.only (function (_ character) + (not (text.contains? (text.of_character character) options))) + (random.character unicode.character))] + (_.coverage [\\projection.none_of] + (and (..should_pass (text.of_character expected) (\\projection.none_of options)) + (..should_fail (text.of_character invalid) (\\projection.none_of options)) + (..should_fail' (text.of_character invalid) (\\projection.none_of options) + //.character_should_not_be)))) + )) + +(the \\projection#runs + Test + (all _.and + (do [! random.monad] + [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + .let [expected (.text left right)] + invalid (|> random.natural + (by ! each (natural.% 16)) + (random.only (natural.>= 8)) + (by ! each (by natural.hex as)))] + (_.coverage [\\projection.many] + (and (..should_pass expected (\\projection.many \\projection.octal)) + (..should_fail invalid (\\projection.many \\projection.octal))))) + (do [! random.monad] + [left (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + right (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural) + .let [expected (.text left right)] + invalid (|> random.natural + (by ! each (natural.% 16)) + (random.only (natural.>= 8)) + (by ! each (by natural.hex as)))] + (_.coverage [\\projection.some] + (and (..should_pass expected (\\projection.some \\projection.octal)) + (..should_pass "" (\\projection.some \\projection.octal)) + (..should_fail invalid (\\projection.some \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.exactly] + (and (..should_pass (.text first second) (\\projection.exactly 2 \\projection.octal)) + (..should_fail (.text first second third) (\\projection.exactly 2 \\projection.octal)) + (..should_fail (.text first) (\\projection.exactly 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.at_most] + (and (..should_pass (.text first second) (\\projection.at_most 2 \\projection.octal)) + (..should_pass (.text first) (\\projection.at_most 2 \\projection.octal)) + (..should_fail (.text first second third) (\\projection.at_most 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.at_least] + (and (..should_pass (.text first second) (\\projection.at_least 2 \\projection.octal)) + (..should_pass (.text first second third) (\\projection.at_least 2 \\projection.octal)) + (..should_fail (.text first) (\\projection.at_least 2 \\projection.octal))))) + (do [! random.monad] + [.let [octal (by ! each (|>> (natural.% 8) (by natural.octal as)) random.natural)] + first octal + second octal + third octal] + (_.coverage [\\projection.between] + (and (..should_pass (.text first second) (\\projection.between 2 1 \\projection.octal)) + (..should_pass (.text first second third) (\\projection.between 2 1 \\projection.octal)) + (..should_fail (.text first) (\\projection.between 2 1 \\projection.octal))))) + )) + +(the \\projection + Test + (<| (_.covering \\projection._) + (all _.and + (_.coverage [\\projection.slice] + (|> "" + (//.value (\\projection.slice \\projection.any)) + (!expect (^.multi {try.#Failure error} + (exception.is? /.cannot_slice error))))) + (do [! random.monad] + [expected (random.unicode 1)] + (_.coverage [\\projection.any] + (and (..should_pass expected \\projection.any) + (..should_fail "" \\projection.any)))) + (do [! random.monad] + [invalid (random.upper_cased 1) + expected (random.only (|>> (unicode/block.within? unicode/block.upper_case) + not) + (random.character unicode.character))] + (_.coverage [\\projection.not] + (and (..should_pass (text.of_character expected) (\\projection.not \\projection.upper)) + (|> invalid + (//.value (\\projection.not \\projection.upper)) + (!expect (^.multi {try.#Failure error} + (exception.is? //.expected_to_fail error))))))) + (do [! random.monad] + [upper (random.upper_cased 1) + lower (random.lower_cased 1) + invalid (random.only (function (_ character) + (not (or (unicode/block.within? unicode/block.upper_case character) + (unicode/block.within? unicode/block.lower_case character)))) + (random.character unicode.character))] + (_.coverage [\\projection.and] + (and (..should_pass (.text upper lower) (\\projection.and \\projection.upper \\projection.lower)) + (..should_fail (.text (text.of_character invalid) lower) (\\projection.and \\projection.upper \\projection.lower)) + (..should_fail (.text upper (text.of_character invalid)) (\\projection.and \\projection.upper \\projection.lower))))) + (do [! random.monad] + [expected (random.unicode 1) + invalid (random.unicode 1)] + (_.coverage [\\projection.satisfies] + (and (..should_pass expected (\\projection.satisfies (function.constant true))) + (..should_fail' invalid (\\projection.satisfies (function.constant false)) + //.character_does_not_satisfy_predicate)))) + + \\projection#character_classes + \\projection#runs + ))) + +(the .public test + Test + (<| (_.covering /._) + (do [! random.monad] + [expected (random.upper_cased 2)]) + (_.for [/.Slice + /.random]) + (all _.and + (_.for [/.equivalence /.=] + (static.when (same? /.equivalence /.=) + (equivalenceT.spec /.equivalence (/.random 2)))) + (_.for [/.text] + (viewT.specification /.text + /.equivalence + text.equivalence + + (/.random 2) + (random.lower_cased 2))) + + (_.coverage [/.whole] + (text.= expected + (view.as /.text (/.whole expected)))) + (_.coverage [/.partial] + (and (by (try.equivalence /.equivalence) = + {try.#Success (/.whole expected)} + (/.partial 0 (text.size expected) expected)) + (by (try.equivalence /.equivalence) = + {try.#Success (/.whole expected)} + (do try.monad + [left (/.partial 0 1 expected) + right (/.partial 1 1 expected)] + (pure (/.+ left right)))))) + (_.coverage [/.cannot_slice] + (and (when (/.partial (++ 0) (text.size expected) expected) + {try.#Failure error} + (exception.is? /.cannot_slice error) + + _ + false) + (when (/.partial 0 (++ (text.size expected)) expected) + {try.#Failure error} + (exception.is? /.cannot_slice error) + + _ + false))) + (_.coverage [/.size] + (natural.= (text.size expected) + (/.size (/.whole expected)))) + (_.coverage [/.empty?] + (/.empty? /.empty)) + (_.coverage [/.empty] + (let [expected (/.whole expected)] + (and (same? expected (/.+ /.empty expected)) + (same? expected (/.+ expected /.empty))))) + (_.coverage [/.+] + (/.= (/.whole (text#composite expected expected)) + (let [expected (/.whole expected)] + (/.+ expected expected)))) + + ..\\projection + ))) diff --git a/stdlib/source/test/lux/math/number/big.lux b/stdlib/source/test/lux/math/number/big.lux index 649e1c135..185dcf316 100644 --- a/stdlib/source/test/lux/math/number/big.lux +++ b/stdlib/source/test/lux/math/number/big.lux @@ -95,10 +95,11 @@ (/.and left right) (/.and right left)))) (_.coverage [/.zero] - (and (same? /.zero (/.not /.zero)) - (by /.equivalence = sample (/.xor /.zero sample)) - (by /.equivalence = sample (/.or /.zero sample)) - (by /.equivalence = /.zero (/.and /.zero sample)))) + (with /.equivalence + (and (= /.zero (/.not /.zero)) + (= sample (/.xor /.zero sample)) + (= sample (/.or /.zero sample)) + (= /.zero (/.and /.zero sample))))) /natural.test /integer.test diff --git a/stdlib/source/test/lux/physics.lux b/stdlib/source/test/lux/physics.lux index aec46c867..9b63997ba 100644 --- a/stdlib/source/test/lux/physics.lux +++ b/stdlib/source/test/lux/physics.lux @@ -5,92 +5,23 @@ [library [lux (.except) [abstract - [monad (.only do)] - ["[0]" equivalence - ["[1]T" \\test]]] + [monad (.only do)]] [math - ["[0]" random (.only Random)] - [number - [/64 - ["[0]" integer] - ["[0]" decimal]]]] + ["[0]" random]] [test - ["_" property (.only Test)]] - [meta - ["[0]" static]] - [logic - ["[0]" bit]] - ["[0]" type - ["[1]" check]]]] + ["_" property (.only Test)]]]] [\\library - ["[0]" / (.only) - ["[0]" /1 (.only) - ["[1]T" \\test]]]]) - -(the (random_vector range) - (-> Natural - (Random /.Vector)) - (random.and (/1.random range) - (/1.random range))) + ["[0]" /]] + [/ + ["[0]" /1] + ["[0]" /2]]) (the .public test Test (<| (_.covering /._) (do [! random.monad] - [.let [range 1,000,000 - random_scalar (/1.random range) - random_vector (..random_vector 1,000)] - origin random_vector - destination random_vector - scale random_scalar - - proximity random_scalar]) + []) (all _.and - (<| (_.for [/.Vector - /.#forward/backward /.#left/right]) - (all _.and - (_.for [/.equivalence /.=] - (static.when (same? /.equivalence /.=) - (equivalenceT.spec /.equivalence (..random_vector 1,000)))) - - (_.coverage [/.+] - (/.= (/.+ origin destination) - (/.+ destination origin))) - (_.coverage [/.opposite] - (/.= /.origin - (/.+ destination - (/.opposite destination)))) - (_.coverage [/.-] - (/.= (/.+ (/.opposite origin) destination) - (/.- origin destination))) - (_.coverage [/.x /./] - (|> destination - (/.x scale) - (/./ scale) - (/.= destination))) - (_.coverage [/.origin] - (and (/.= destination - (/.+ /.origin destination)) - (/.= /.origin - (/.x decimal.zero destination)))) - (_.coverage [/.Orientation - /.magnitude /.orientation - /.polar] - (/.approximately? +0.000,000,000,1 - destination - (/.polar (/.magnitude destination) - (/.orientation destination)))) - (_.coverage [/.distance] - (and (decimal.= decimal.zero - (/.distance destination destination)) - (decimal.= (/.distance origin destination) - (/.distance destination origin)))) - (_.coverage [/.approximately?] - (let [limit (/.distance origin destination)] - (and (/.approximately? limit origin destination) - (bit.= (decimal.< limit proximity) - (not (/.approximately? proximity origin destination)))))) - )) - - /1T.test + /1.test + /2.test ))) diff --git a/stdlib/source/test/lux/physics/1.lux b/stdlib/source/test/lux/physics/1.lux index 0b07e342c..022221652 100644 --- a/stdlib/source/test/lux/physics/1.lux +++ b/stdlib/source/test/lux/physics/1.lux @@ -48,10 +48,11 @@ (<| (_.for [/.Difference]) (all _.and (_.coverage [/.stasis] - (and (decimal.= final_time - (/.stasis final_time)) - (decimal.= decimal.zero - (/.value /.stasis)))) + (decimal.= final_time + (/.stasis final_time))) + (_.coverage [/.origin] + (decimal.= /.origin + (/.value /.stasis))) (_.coverage [/.difference] (decimal.= final_position ((/.difference initial_position final_position) initial_position))) diff --git a/stdlib/source/test/lux/physics/2.lux b/stdlib/source/test/lux/physics/2.lux new file mode 100644 index 000000000..3c00cf3b6 --- /dev/null +++ b/stdlib/source/test/lux/physics/2.lux @@ -0,0 +1,107 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.using + [library + [lux (.except) + [abstract + [monad (.only do)] + ["[0]" equivalence + ["[1]T" \\test]]] + [math + ["[0]" random (.only Random)] + [number + [/64 + ["[0]" integer] + ["[0]" decimal]]]] + [test + ["_" property (.only Test)]] + [meta + ["[0]" static]] + [logic + ["[0]" bit]] + ["[0]" type + ["[1]" check]] + [macro + ["[0]" template] + ["[0]" expansion]]]] + [\\library + ["[0]" / (.only) + [// + ["[0]" /1]]]]) + +(the .public test + Test + (<| (_.covering /._) + (do [! random.monad] + [.let [range 1,000,000 + random_scalar (/1.random range) + random_vector (/.random range)] + origin random_vector + destination random_vector + scale random_scalar + + proximity random_scalar]) + (all _.and + (<| (_.for [/.Vector + /.#forward/backward /.#left/right]) + (all _.and + (_.for [/.equivalence /.=] + (static.when (same? /.equivalence /.=) + (equivalenceT.spec /.equivalence (/.random range)))) + + (_.coverage [/.+] + (/.= (/.+ origin destination) + (/.+ destination origin))) + (_.coverage [/.opposite] + (/.= /.origin + (/.+ destination + (/.opposite destination)))) + (_.coverage [/.-] + (/.= (/.+ (/.opposite origin) destination) + (/.- origin destination))) + (_.coverage [/.x /./] + (|> destination + (/.x scale) + (/./ scale) + (/.= destination))) + (_.coverage [/.origin] + (and (/.= destination + (/.+ /.origin destination)) + (/.= /.origin + (/.x decimal.zero destination)))) + (_.coverage [/.Orientation + /.magnitude /.orientation + /.polar] + (/.approximately? +0.000,000,000,1 + destination + (/.polar (/.magnitude destination) + (/.orientation destination)))) + (_.coverage [/.distance] + (and (decimal.= decimal.zero + (/.distance destination destination)) + (decimal.= (/.distance origin destination) + (/.distance destination origin)))) + (_.coverage [/.approximately?] + (let [limit (/.distance origin destination)] + (and (/.approximately? limit origin destination) + (bit.= (decimal.< limit proximity) + (not (/.approximately? proximity origin destination)))))) + )) + (<| (_.for [/.Static_Friction /.Kinetic_Friction]) + (`` (all _.and + (,, (template.with [,surface ,material] + [(expansion.let [,static (template.name [/._] ["static_friction_of_" ,material "_on_" ,surface]) + ,kinetic (template.name [/._] ["kinetic_friction_of_" ,material "_on_" ,surface])] + (_.coverage [,static ,kinetic] + (decimal.< ,static ,kinetic)))] + + [[concrete rubber] + [steel steel] + [steel aluminium] + [steel copper] + [glass glass] + [wet_snow waxed_wood] + [ice ice]])) + ))) + )))