From f1768c649501e736452ca50dca76644e01af0518 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 2 May 2018 19:49:03 -0400 Subject: - List ranges can now be generated backwards. --- stdlib/source/lux/data/coll/list.lux | 17 +++++++++++------ stdlib/test/test/lux/control/region.lux | 2 +- stdlib/test/test/lux/data/coll/list.lux | 33 ++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 18 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/lux/data/coll/list.lux b/stdlib/source/lux/data/coll/list.lux index b9b743b2c..f6c19dcb9 100644 --- a/stdlib/source/lux/data/coll/list.lux +++ b/stdlib/source/lux/data/coll/list.lux @@ -303,16 +303,21 @@ xs')] ($_ compose (sort < pre) (list x) (sort < post))))) -(do-template [ ] +(do-template [ ] [(def: #export ( from to) {#.doc "Generates an inclusive interval of values [from, to]."} (-> (List )) - (if ( to from) - (list& from ( ( from) to)) - (list)))] + (cond ( to from) + (list& from ( ( from) to)) - [i/range Int i/<= i/inc] - [n/range Nat n/<= n/inc] + ( to from) + (list& from ( ( from) to)) + + ## (= to from) + (list from)))] + + [i/range Int i/< i/inc i/> i/dec] + [n/range Nat n/< n/inc n/> n/dec] ) (def: #export (empty? xs) diff --git a/stdlib/test/test/lux/control/region.lux b/stdlib/test/test/lux/control/region.lux index a7a577ab5..fc1eb7edf 100644 --- a/stdlib/test/test/lux/control/region.lux +++ b/stdlib/test/test/lux/control/region.lux @@ -26,7 +26,7 @@ (context: "Regions." (<| (times +100) (do @ - [expected-clean-ups (|> r.nat (:: @ map (n/% +100)))] + [expected-clean-ups (|> r.nat (:: @ map (|>> (n/% +100) (n/max +1))))] ($_ seq (test "Clean-up functions are always run when region execution is done." (thread.run diff --git a/stdlib/test/test/lux/data/coll/list.lux b/stdlib/test/test/lux/data/coll/list.lux index 729519823..578b652da 100644 --- a/stdlib/test/test/lux/data/coll/list.lux +++ b/stdlib/test/test/lux/data/coll/list.lux @@ -132,6 +132,8 @@ other-size bounded-size other-sample (r.list other-size r.nat) separator r.nat + from (|> r.nat (:: @ map (n/% +10))) + to (|> r.nat (:: @ map (n/% +10))) #let [(^open) (&.Eq number.Eq) (^open "&/") &.Functor]] ($_ seq @@ -208,17 +210,26 @@ (&/map product.left enum-sample)) (= sample (&/map product.right enum-sample))))) + + (test "Ranges can be constructed forward and backwards." + (and (let [(^open "list/") (&.Eq number.Eq)] + (list/= (&.n/range from to) + (&.reverse (&.n/range to from)))) + (let [(^open "list/") (&.Eq number.Eq) + from (nat-to-int from) + to (nat-to-int to)] + (list/= (&.i/range from to) + (&.reverse (&.i/range to from)))))) )))) ## TODO: Add again once new-luxc becomes the standard compiler. -## (context: "Monad transformer" -## (let [lift (&.lift io.Monad) -## (^open "io/") io.Monad] -## (test "Can add list functionality to any monad." -## (|> (io.run (do (&.ListT io.Monad) -## [a (lift (io/wrap 123)) -## b (wrap 456)] -## (wrap (i/+ a b)))) -## (case> (^ (list 579)) true -## _ false))) -## )) +(context: "Monad transformer" + (let [lift (&.lift io.Monad) + (^open "io/") io.Monad] + (test "Can add list functionality to any monad." + (|> (io.run (do (&.ListT io.Monad) + [a (lift (io/wrap 123)) + b (wrap 456)] + (wrap (i/+ a b)))) + (case> (^ (list 579)) true + _ false))))) -- cgit v1.2.3