From 677ae72669dabcad59262287aa4d84c5631bbddd Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 1 May 2018 21:32:29 -0400 Subject: - Re-organized set modules. --- stdlib/source/lux/control/predicate.lux | 2 +- stdlib/source/lux/data/coll/ordered/set.lux | 86 ---------------------- stdlib/source/lux/data/coll/set.lux | 76 ------------------- stdlib/source/lux/data/coll/set/ordered.lux | 86 ++++++++++++++++++++++ stdlib/source/lux/data/coll/set/unordered.lux | 76 +++++++++++++++++++ stdlib/source/lux/lang/type/check.lux | 2 +- stdlib/source/lux/macro/poly/eq.lux | 2 +- stdlib/source/lux/math/logic/fuzzy.lux | 4 +- stdlib/source/lux/math/random.lux | 2 +- stdlib/source/lux/type/object/interface.lux | 2 +- stdlib/source/lux/type/resource.lux | 2 +- stdlib/test/test/lux/control/interval.lux | 4 +- stdlib/test/test/lux/data/coll/ordered/dict.lux | 2 +- stdlib/test/test/lux/data/coll/ordered/set.lux | 94 ------------------------ stdlib/test/test/lux/data/coll/set.lux | 64 ---------------- stdlib/test/test/lux/data/coll/set/ordered.lux | 94 ++++++++++++++++++++++++ stdlib/test/test/lux/data/coll/set/unordered.lux | 64 ++++++++++++++++ stdlib/test/test/lux/lang/type/check.lux | 2 +- stdlib/test/test/lux/math/logic/fuzzy.lux | 2 +- stdlib/test/test/lux/math/random.lux | 2 +- stdlib/test/tests.lux | 6 +- 21 files changed, 337 insertions(+), 337 deletions(-) delete mode 100644 stdlib/source/lux/data/coll/ordered/set.lux delete mode 100644 stdlib/source/lux/data/coll/set.lux create mode 100644 stdlib/source/lux/data/coll/set/ordered.lux create mode 100644 stdlib/source/lux/data/coll/set/unordered.lux delete mode 100644 stdlib/test/test/lux/data/coll/ordered/set.lux delete mode 100644 stdlib/test/test/lux/data/coll/set.lux create mode 100644 stdlib/test/test/lux/data/coll/set/ordered.lux create mode 100644 stdlib/test/test/lux/data/coll/set/unordered.lux (limited to 'stdlib') diff --git a/stdlib/source/lux/control/predicate.lux b/stdlib/source/lux/control/predicate.lux index 641a90b28..de9d0dab0 100644 --- a/stdlib/source/lux/control/predicate.lux +++ b/stdlib/source/lux/control/predicate.lux @@ -1,6 +1,6 @@ (.module: lux - (lux (data (coll [set #+ Set])))) + (lux (data (coll (set ["set" unordered #+ Set]))))) (type: #export (Pred a) (-> a Bool)) diff --git a/stdlib/source/lux/data/coll/ordered/set.lux b/stdlib/source/lux/data/coll/ordered/set.lux deleted file mode 100644 index 5d6ba5478..000000000 --- a/stdlib/source/lux/data/coll/ordered/set.lux +++ /dev/null @@ -1,86 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do Monad] - eq - [order #+ Order]) - (data (coll [list "L/" Monad Monoid Fold] - (ordered ["d" dict])) - ["p" product] - ["M" maybe #+ Functor]) - [macro] - (macro [code] - ["s" syntax #+ syntax: Syntax]))) - -(type: #export (Set a) - (d.Dict a a)) - -(def: #export new - (All [a] (-> (Order a) (Set a))) - d.new) - -(def: #export (member? set elem) - (All [a] (-> (Set a) a Bool)) - (d.contains? elem set)) - -(do-template [ ] - [(def: #export ( set) - (All [a] (-> (Set a) (Maybe a))) - ( set))] - - [min d.min] - [max d.max] - ) - -(do-template [ ] - [(def: #export ( set) - (All [a] (-> (Set a) Nat)) - ( set))] - - [size d.size] - [depth d.depth] - ) - -(def: #export (add elem set) - (All [a] (-> a (Set a) (Set a))) - (d.put elem elem set)) - -(def: #export (remove elem set) - (All [a] (-> a (Set a) (Set a))) - (d.remove elem set)) - -(def: #export (from-list Order list) - (All [a] (-> (Order a) (List a) (Set a))) - (L/fold add (new Order) list)) - -(def: #export (to-list set) - (All [a] (-> (Set a) (List a))) - (d.keys set)) - -(def: #export (union left right) - (All [a] (-> (Set a) (Set a) (Set a))) - (L/fold add right (to-list left))) - -(def: #export (intersection left right) - (All [a] (-> (Set a) (Set a) (Set a))) - (|> (to-list right) - (list.filter (member? left)) - (from-list (get@ #d.order right)))) - -(def: #export (difference param subject) - (All [a] (-> (Set a) (Set a) (Set a))) - (|> (to-list subject) - (list.filter (|>> (member? param) not)) - (from-list (get@ #d.order subject)))) - -(def: #export (sub? super sub) - (All [a] (-> (Set a) (Set a) Bool)) - (list.every? (member? super) (to-list sub))) - -(def: #export (super? sub super) - (All [a] (-> (Set a) (Set a) Bool)) - (sub? super sub)) - -(struct: #export Eq (All [a] (Eq (Set a))) - (def: (= reference sample) - (:: (list.Eq (:: sample eq)) - = (to-list reference) (to-list sample)))) diff --git a/stdlib/source/lux/data/coll/set.lux b/stdlib/source/lux/data/coll/set.lux deleted file mode 100644 index a09300a7a..000000000 --- a/stdlib/source/lux/data/coll/set.lux +++ /dev/null @@ -1,76 +0,0 @@ -(.module: - lux - (lux (control [eq #+ Eq] - [hash #*]) - (data (coll [dict] - [list "List/" Fold Functor])))) - -## [Types] -(type: #export (Set a) - (dict.Dict a a)) - -## [Values] -(def: #export (new Hash) - (All [a] (-> (Hash a) (Set a))) - (dict.new Hash)) - -(def: #export (add elem set) - (All [a] (-> a (Set a) (Set a))) - (dict.put elem elem set)) - -(def: #export (remove elem set) - (All [a] (-> a (Set a) (Set a))) - (dict.remove elem set)) - -(def: #export (member? set elem) - (All [a] (-> (Set a) a Bool)) - (dict.contains? elem set)) - -(def: #export to-list - (All [a] (-> (Set a) (List a))) - dict.keys) - -(def: #export (from-list Hash xs) - (All [a] (-> (Hash a) (List a) (Set a))) - (List/fold add (new Hash) xs)) - -(def: #export (union xs yx) - (All [a] (-> (Set a) (Set a) (Set a))) - (dict.merge xs yx)) - -(def: #export (difference sub base) - (All [a] (-> (Set a) (Set a) (Set a))) - (List/fold remove base (to-list sub))) - -(def: #export (intersection filter base) - (All [a] (-> (Set a) (Set a) (Set a))) - (dict.select (dict.keys filter) base)) - -(def: #export (size set) - (All [a] (-> (Set a) Nat)) - (dict.size set)) - -(def: #export (empty? set) - (All [a] (-> (Set a) Bool)) - (n/= +0 (dict.size set))) - -(def: #export (sub? super sub) - (All [a] (-> (Set a) (Set a) Bool)) - (list.every? (member? super) (to-list sub))) - -(def: #export (super? sub super) - (All [a] (-> (Set a) (Set a) Bool)) - (sub? super sub)) - -## [Structures] -(struct: #export Eq (All [a] (Eq (Set a))) - (def: (= (^@ test [Hash _]) subject) - (:: (list.Eq (get@ #hash.eq Hash)) = (to-list test) (to-list subject)))) - -(struct: #export Hash (All [a] (Hash (Set a))) - (def: eq Eq) - - (def: (hash (^@ set [Hash _])) - (List/fold (function (_ elem acc) (n/+ (:: Hash hash elem) acc)) - +0 - (to-list set)))) diff --git a/stdlib/source/lux/data/coll/set/ordered.lux b/stdlib/source/lux/data/coll/set/ordered.lux new file mode 100644 index 000000000..5d6ba5478 --- /dev/null +++ b/stdlib/source/lux/data/coll/set/ordered.lux @@ -0,0 +1,86 @@ +(.module: + lux + (lux (control [monad #+ do Monad] + eq + [order #+ Order]) + (data (coll [list "L/" Monad Monoid Fold] + (ordered ["d" dict])) + ["p" product] + ["M" maybe #+ Functor]) + [macro] + (macro [code] + ["s" syntax #+ syntax: Syntax]))) + +(type: #export (Set a) + (d.Dict a a)) + +(def: #export new + (All [a] (-> (Order a) (Set a))) + d.new) + +(def: #export (member? set elem) + (All [a] (-> (Set a) a Bool)) + (d.contains? elem set)) + +(do-template [ ] + [(def: #export ( set) + (All [a] (-> (Set a) (Maybe a))) + ( set))] + + [min d.min] + [max d.max] + ) + +(do-template [ ] + [(def: #export ( set) + (All [a] (-> (Set a) Nat)) + ( set))] + + [size d.size] + [depth d.depth] + ) + +(def: #export (add elem set) + (All [a] (-> a (Set a) (Set a))) + (d.put elem elem set)) + +(def: #export (remove elem set) + (All [a] (-> a (Set a) (Set a))) + (d.remove elem set)) + +(def: #export (from-list Order list) + (All [a] (-> (Order a) (List a) (Set a))) + (L/fold add (new Order) list)) + +(def: #export (to-list set) + (All [a] (-> (Set a) (List a))) + (d.keys set)) + +(def: #export (union left right) + (All [a] (-> (Set a) (Set a) (Set a))) + (L/fold add right (to-list left))) + +(def: #export (intersection left right) + (All [a] (-> (Set a) (Set a) (Set a))) + (|> (to-list right) + (list.filter (member? left)) + (from-list (get@ #d.order right)))) + +(def: #export (difference param subject) + (All [a] (-> (Set a) (Set a) (Set a))) + (|> (to-list subject) + (list.filter (|>> (member? param) not)) + (from-list (get@ #d.order subject)))) + +(def: #export (sub? super sub) + (All [a] (-> (Set a) (Set a) Bool)) + (list.every? (member? super) (to-list sub))) + +(def: #export (super? sub super) + (All [a] (-> (Set a) (Set a) Bool)) + (sub? super sub)) + +(struct: #export Eq (All [a] (Eq (Set a))) + (def: (= reference sample) + (:: (list.Eq (:: sample eq)) + = (to-list reference) (to-list sample)))) diff --git a/stdlib/source/lux/data/coll/set/unordered.lux b/stdlib/source/lux/data/coll/set/unordered.lux new file mode 100644 index 000000000..a09300a7a --- /dev/null +++ b/stdlib/source/lux/data/coll/set/unordered.lux @@ -0,0 +1,76 @@ +(.module: + lux + (lux (control [eq #+ Eq] + [hash #*]) + (data (coll [dict] + [list "List/" Fold Functor])))) + +## [Types] +(type: #export (Set a) + (dict.Dict a a)) + +## [Values] +(def: #export (new Hash) + (All [a] (-> (Hash a) (Set a))) + (dict.new Hash)) + +(def: #export (add elem set) + (All [a] (-> a (Set a) (Set a))) + (dict.put elem elem set)) + +(def: #export (remove elem set) + (All [a] (-> a (Set a) (Set a))) + (dict.remove elem set)) + +(def: #export (member? set elem) + (All [a] (-> (Set a) a Bool)) + (dict.contains? elem set)) + +(def: #export to-list + (All [a] (-> (Set a) (List a))) + dict.keys) + +(def: #export (from-list Hash xs) + (All [a] (-> (Hash a) (List a) (Set a))) + (List/fold add (new Hash) xs)) + +(def: #export (union xs yx) + (All [a] (-> (Set a) (Set a) (Set a))) + (dict.merge xs yx)) + +(def: #export (difference sub base) + (All [a] (-> (Set a) (Set a) (Set a))) + (List/fold remove base (to-list sub))) + +(def: #export (intersection filter base) + (All [a] (-> (Set a) (Set a) (Set a))) + (dict.select (dict.keys filter) base)) + +(def: #export (size set) + (All [a] (-> (Set a) Nat)) + (dict.size set)) + +(def: #export (empty? set) + (All [a] (-> (Set a) Bool)) + (n/= +0 (dict.size set))) + +(def: #export (sub? super sub) + (All [a] (-> (Set a) (Set a) Bool)) + (list.every? (member? super) (to-list sub))) + +(def: #export (super? sub super) + (All [a] (-> (Set a) (Set a) Bool)) + (sub? super sub)) + +## [Structures] +(struct: #export Eq (All [a] (Eq (Set a))) + (def: (= (^@ test [Hash _]) subject) + (:: (list.Eq (get@ #hash.eq Hash)) = (to-list test) (to-list subject)))) + +(struct: #export Hash (All [a] (Hash (Set a))) + (def: eq Eq) + + (def: (hash (^@ set [Hash _])) + (List/fold (function (_ elem acc) (n/+ (:: Hash hash elem) acc)) + +0 + (to-list set)))) diff --git a/stdlib/source/lux/lang/type/check.lux b/stdlib/source/lux/lang/type/check.lux index 7b7c1e3ee..09af682ca 100644 --- a/stdlib/source/lux/lang/type/check.lux +++ b/stdlib/source/lux/lang/type/check.lux @@ -9,7 +9,7 @@ [maybe] [product] (coll [list] - [set #+ Set]) + (set ["set" unordered #+ Set])) ["e" error]) (lang [type "type/" Eq]) )) diff --git a/stdlib/source/lux/macro/poly/eq.lux b/stdlib/source/lux/macro/poly/eq.lux index c1566b6d3..a5b312a2d 100644 --- a/stdlib/source/lux/macro/poly/eq.lux +++ b/stdlib/source/lux/macro/poly/eq.lux @@ -9,7 +9,7 @@ [sequence] [array] [queue] - [set] + (set ["set" unordered]) [dict #+ Dict] (tree [rose])) [number "nat/" Codec] diff --git a/stdlib/source/lux/math/logic/fuzzy.lux b/stdlib/source/lux/math/logic/fuzzy.lux index 58e716c55..7933027c9 100644 --- a/stdlib/source/lux/math/logic/fuzzy.lux +++ b/stdlib/source/lux/math/logic/fuzzy.lux @@ -2,7 +2,7 @@ lux (lux (data [number "Deg/" Interval] (coll [list] - [set]) + (set ["set" unordered #+ Set])) text/format) [math]) (// ["&" continuous])) @@ -45,7 +45,7 @@ &.~false))) (def: #export (from-set set) - (All [a] (-> (set.Set a) (Fuzzy a))) + (All [a] (-> (Set a) (Fuzzy a))) (from-predicate (set.member? set))) (do-template [
] diff --git a/stdlib/source/lux/math/random.lux b/stdlib/source/lux/math/random.lux index f2f4d2e14..7172309d0 100644 --- a/stdlib/source/lux/math/random.lux +++ b/stdlib/source/lux/math/random.lux @@ -15,7 +15,7 @@ [array] [dict #+ Dict] [queue #+ Queue] - [set #+ Set] + (set ["set" unordered #+ Set]) [stack #+ Stack] [sequence #+ Sequence])) )) diff --git a/stdlib/source/lux/type/object/interface.lux b/stdlib/source/lux/type/object/interface.lux index 9e723548a..a572985ad 100644 --- a/stdlib/source/lux/type/object/interface.lux +++ b/stdlib/source/lux/type/object/interface.lux @@ -8,7 +8,7 @@ [maybe] [ident #+ "ident/" Eq] (coll [list "list/" Functor Fold Monoid] - [set #+ Set])) + (set ["set" unordered #+ Set]))) [macro #+ Monad "macro/" Monad] (macro [code] ["s" syntax #+ syntax:] diff --git a/stdlib/source/lux/type/resource.lux b/stdlib/source/lux/type/resource.lux index 526dbecc9..ce792af96 100644 --- a/stdlib/source/lux/type/resource.lux +++ b/stdlib/source/lux/type/resource.lux @@ -10,7 +10,7 @@ [number] text/format (coll [dict #+ Dict] - [set] + (set ["set" unordered]) [sequence #+ Sequence] [list "list/" Functor Fold])) (concurrency [promise #+ Promise]) diff --git a/stdlib/test/test/lux/control/interval.lux b/stdlib/test/test/lux/control/interval.lux index 05566f464..3187c0b7b 100644 --- a/stdlib/test/test/lux/control/interval.lux +++ b/stdlib/test/test/lux/control/interval.lux @@ -8,8 +8,8 @@ ["r" math/random] (data text/format [number] - ["S" coll/set] - ["L" coll/list]))) + (coll ["S" set/unordered] + ["L" list])))) (context: "Equality." (<| (times +100) diff --git a/stdlib/test/test/lux/data/coll/ordered/dict.lux b/stdlib/test/test/lux/data/coll/ordered/dict.lux index 975b26dd2..301e64927 100644 --- a/stdlib/test/test/lux/data/coll/ordered/dict.lux +++ b/stdlib/test/test/lux/data/coll/ordered/dict.lux @@ -6,7 +6,7 @@ (data [product] [number] (coll (ordered ["&" dict]) - ["s" set] + (set ["s" unordered]) ["d" dict] [list "L/" Functor])) ["r" math/random]) diff --git a/stdlib/test/test/lux/data/coll/ordered/set.lux b/stdlib/test/test/lux/data/coll/ordered/set.lux deleted file mode 100644 index 123613972..000000000 --- a/stdlib/test/test/lux/data/coll/ordered/set.lux +++ /dev/null @@ -1,94 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad]) - (data (coll (ordered ["&" set]) - ["s" set] - [list "" Fold]) - [number] - text/format) - ["r" math/random]) - lux/test) - -(def: gen-nat - (r.Random Nat) - (|> r.nat - (:: r.Monad map (n/% +100)))) - -(context: "Sets" - (<| (times +100) - (do @ - [sizeL gen-nat - sizeR gen-nat - listL (|> (r.set number.Hash sizeL gen-nat) (:: @ map s.to-list)) - listR (|> (r.set number.Hash sizeR gen-nat) (:: @ map s.to-list)) - #let [(^open "&/") &.Eq - setL (&.from-list number.Order listL) - setR (&.from-list number.Order listR) - sortedL (list.sort n/< listL) - minL (list.head sortedL) - maxL (list.last sortedL)]] - ($_ seq - (test "I can query the size of a set." - (n/= sizeL (&.size setL))) - - (test "Can query minimum value." - (case [(&.min setL) minL] - [#.None #.None] - true - - [(#.Some reference) (#.Some sample)] - (n/= reference sample) - - _ - false)) - - (test "Can query maximum value." - (case [(&.max setL) maxL] - [#.None #.None] - true - - [(#.Some reference) (#.Some sample)] - (n/= reference sample) - - _ - false)) - - (test "Converting sets to/from lists can't change their values." - (|> setL - &.to-list (&.from-list number.Order) - (&/= setL))) - - (test "Order is preserved." - (let [listL (&.to-list setL) - (^open "L/") (list.Eq number.Eq)] - (L/= listL - (list.sort n/< listL)))) - - (test "Every set is a sub-set of the union of itself with another." - (let [setLR (&.union setL setR)] - (and (&.sub? setLR setL) - (&.sub? setLR setR)))) - - (test "Every set is a super-set of the intersection of itself with another." - (let [setLR (&.intersection setL setR)] - (and (&.super? setLR setL) - (&.super? setLR setR)))) - - (test "Union with the empty set leaves a set unchanged." - (&/= setL - (&.union (&.new number.Order) - setL))) - - (test "Intersection with the empty set results in the empty set." - (let [empty-set (&.new number.Order)] - (&/= empty-set - (&.intersection empty-set setL)))) - - (test "After substracting a set A from another B, no member of A can be a member of B." - (let [sub (&.difference setR setL)] - (not (list.any? (&.member? sub) (&.to-list setR))))) - - (test "Every member of a set must be identifiable." - (list.every? (&.member? setL) (&.to-list setL))) - )))) diff --git a/stdlib/test/test/lux/data/coll/set.lux b/stdlib/test/test/lux/data/coll/set.lux deleted file mode 100644 index eb43bd0d2..000000000 --- a/stdlib/test/test/lux/data/coll/set.lux +++ /dev/null @@ -1,64 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad]) - (data (coll ["&" set] - [list "" Fold]) - [number]) - ["r" math/random]) - lux/test) - -(def: gen-nat - (r.Random Nat) - (|> r.nat - (:: r.Monad map (n/% +100)))) - -(context: "Sets" - (<| (times +100) - (do @ - [sizeL gen-nat - sizeR gen-nat - setL (r.set number.Hash sizeL gen-nat) - setR (r.set number.Hash sizeR gen-nat) - non-member (|> gen-nat - (r.filter (|>> (&.member? setL) not))) - #let [(^open "&/") &.Eq]] - ($_ seq - (test "I can query the size of a set." - (and (n/= sizeL (&.size setL)) - (n/= sizeR (&.size setR)))) - - (test "Converting sets to/from lists can't change their values." - (|> setL - &.to-list (&.from-list number.Hash) - (&/= setL))) - - (test "Every set is a sub-set of the union of itself with another." - (let [setLR (&.union setL setR)] - (and (&.sub? setLR setL) - (&.sub? setLR setR)))) - - (test "Every set is a super-set of the intersection of itself with another." - (let [setLR (&.intersection setL setR)] - (and (&.super? setLR setL) - (&.super? setLR setR)))) - - (test "Union with the empty set leaves a set unchanged." - (&/= setL - (&.union (&.new number.Hash) - setL))) - - (test "Intersection with the empty set results in the empty set." - (let [empty-set (&.new number.Hash)] - (&/= empty-set - (&.intersection empty-set setL)))) - - (test "After substracting a set A from another B, no member of A can be a member of B." - (let [sub (&.difference setR setL)] - (not (list.any? (&.member? sub) (&.to-list setR))))) - - (test "Every member of a set must be identifiable." - (and (not (&.member? setL non-member)) - (&.member? (&.add non-member setL) non-member) - (not (&.member? (&.remove non-member (&.add non-member setL)) non-member)))) - )))) diff --git a/stdlib/test/test/lux/data/coll/set/ordered.lux b/stdlib/test/test/lux/data/coll/set/ordered.lux new file mode 100644 index 000000000..cb78ad963 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/set/ordered.lux @@ -0,0 +1,94 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad]) + (data (coll (set ["s" unordered] + ["&" ordered]) + [list "" Fold]) + [number] + text/format) + ["r" math/random]) + lux/test) + +(def: gen-nat + (r.Random Nat) + (|> r.nat + (:: r.Monad map (n/% +100)))) + +(context: "Sets" + (<| (times +100) + (do @ + [sizeL gen-nat + sizeR gen-nat + listL (|> (r.set number.Hash sizeL gen-nat) (:: @ map s.to-list)) + listR (|> (r.set number.Hash sizeR gen-nat) (:: @ map s.to-list)) + #let [(^open "&/") &.Eq + setL (&.from-list number.Order listL) + setR (&.from-list number.Order listR) + sortedL (list.sort n/< listL) + minL (list.head sortedL) + maxL (list.last sortedL)]] + ($_ seq + (test "I can query the size of a set." + (n/= sizeL (&.size setL))) + + (test "Can query minimum value." + (case [(&.min setL) minL] + [#.None #.None] + true + + [(#.Some reference) (#.Some sample)] + (n/= reference sample) + + _ + false)) + + (test "Can query maximum value." + (case [(&.max setL) maxL] + [#.None #.None] + true + + [(#.Some reference) (#.Some sample)] + (n/= reference sample) + + _ + false)) + + (test "Converting sets to/from lists can't change their values." + (|> setL + &.to-list (&.from-list number.Order) + (&/= setL))) + + (test "Order is preserved." + (let [listL (&.to-list setL) + (^open "L/") (list.Eq number.Eq)] + (L/= listL + (list.sort n/< listL)))) + + (test "Every set is a sub-set of the union of itself with another." + (let [setLR (&.union setL setR)] + (and (&.sub? setLR setL) + (&.sub? setLR setR)))) + + (test "Every set is a super-set of the intersection of itself with another." + (let [setLR (&.intersection setL setR)] + (and (&.super? setLR setL) + (&.super? setLR setR)))) + + (test "Union with the empty set leaves a set unchanged." + (&/= setL + (&.union (&.new number.Order) + setL))) + + (test "Intersection with the empty set results in the empty set." + (let [empty-set (&.new number.Order)] + (&/= empty-set + (&.intersection empty-set setL)))) + + (test "After substracting a set A from another B, no member of A can be a member of B." + (let [sub (&.difference setR setL)] + (not (list.any? (&.member? sub) (&.to-list setR))))) + + (test "Every member of a set must be identifiable." + (list.every? (&.member? setL) (&.to-list setL))) + )))) diff --git a/stdlib/test/test/lux/data/coll/set/unordered.lux b/stdlib/test/test/lux/data/coll/set/unordered.lux new file mode 100644 index 000000000..bdb896e8b --- /dev/null +++ b/stdlib/test/test/lux/data/coll/set/unordered.lux @@ -0,0 +1,64 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad]) + (data (coll (set ["&" unordered #+ Set]) + [list "" Fold]) + [number]) + ["r" math/random]) + lux/test) + +(def: gen-nat + (r.Random Nat) + (|> r.nat + (:: r.Monad map (n/% +100)))) + +(context: "Sets" + (<| (times +100) + (do @ + [sizeL gen-nat + sizeR gen-nat + setL (r.set number.Hash sizeL gen-nat) + setR (r.set number.Hash sizeR gen-nat) + non-member (|> gen-nat + (r.filter (|>> (&.member? setL) not))) + #let [(^open "&/") &.Eq]] + ($_ seq + (test "I can query the size of a set." + (and (n/= sizeL (&.size setL)) + (n/= sizeR (&.size setR)))) + + (test "Converting sets to/from lists can't change their values." + (|> setL + &.to-list (&.from-list number.Hash) + (&/= setL))) + + (test "Every set is a sub-set of the union of itself with another." + (let [setLR (&.union setL setR)] + (and (&.sub? setLR setL) + (&.sub? setLR setR)))) + + (test "Every set is a super-set of the intersection of itself with another." + (let [setLR (&.intersection setL setR)] + (and (&.super? setLR setL) + (&.super? setLR setR)))) + + (test "Union with the empty set leaves a set unchanged." + (&/= setL + (&.union (&.new number.Hash) + setL))) + + (test "Intersection with the empty set results in the empty set." + (let [empty-set (&.new number.Hash)] + (&/= empty-set + (&.intersection empty-set setL)))) + + (test "After substracting a set A from another B, no member of A can be a member of B." + (let [sub (&.difference setR setL)] + (not (list.any? (&.member? sub) (&.to-list setR))))) + + (test "Every member of a set must be identifiable." + (and (not (&.member? setL non-member)) + (&.member? (&.add non-member setL) non-member) + (not (&.member? (&.remove non-member (&.add non-member setL)) non-member)))) + )))) diff --git a/stdlib/test/test/lux/lang/type/check.lux b/stdlib/test/test/lux/lang/type/check.lux index dd42ceaf4..48a3f35cb 100644 --- a/stdlib/test/test/lux/lang/type/check.lux +++ b/stdlib/test/test/lux/lang/type/check.lux @@ -9,7 +9,7 @@ [text "text/" Monoid Eq] text/format (coll [list "list/" Functor] - [set])) + (set ["set" unordered]))) ["r" math/random] (lang [type "type/" Eq] ["@" type/check])) diff --git a/stdlib/test/test/lux/math/logic/fuzzy.lux b/stdlib/test/test/lux/math/logic/fuzzy.lux index a1c01408c..068e00523 100644 --- a/stdlib/test/test/lux/math/logic/fuzzy.lux +++ b/stdlib/test/test/lux/math/logic/fuzzy.lux @@ -3,7 +3,7 @@ (lux [io] (control [monad #+ do Monad]) (data (coll [list] - [set]) + (set ["set" unordered])) [bool "B/" Eq] [number] text/format) diff --git a/stdlib/test/test/lux/math/random.lux b/stdlib/test/test/lux/math/random.lux index b5e305ff7..eed882ea7 100644 --- a/stdlib/test/test/lux/math/random.lux +++ b/stdlib/test/test/lux/math/random.lux @@ -9,7 +9,7 @@ [array] [queue] [stack] - [set] + (set ["set" unordered]) [dict])) (math ["r" random])) lux/test) diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index 05118f921..9739c63e5 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -46,15 +46,15 @@ ["_." dict] ["_." list] ["_." queue] - ["_." set] + ["_." set/unordered] + ["_." set/ordered] ["_." stack] ["_." sequence] ["_." priority-queue] ["_." stream] (tree ["tree_." rose] ["tree_." zipper]) - (ordered ["ordered_." dict] - ["ordered_." set])) + (ordered ["ordered_." dict])) (text ["_." format] ["_." lexer] ["_." regex])) -- cgit v1.2.3