diff options
author | Eduardo Julian | 2018-12-26 00:50:18 -0400 |
---|---|---|
committer | Eduardo Julian | 2018-12-26 00:50:18 -0400 |
commit | 2cfb7042e464a26a239e7b0f24cc28bd7781e520 (patch) | |
tree | e5aff1a85850df0c9ffba21bd2f856435d92a742 /stdlib/source/lux/data/collection/list.lux | |
parent | ecd1e053a413c5d7caebc2ae0ac2520d827fcd79 (diff) |
Some refactoring & minor additions.
Diffstat (limited to 'stdlib/source/lux/data/collection/list.lux')
-rw-r--r-- | stdlib/source/lux/data/collection/list.lux | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/stdlib/source/lux/data/collection/list.lux b/stdlib/source/lux/data/collection/list.lux index c49a7ba9f..a92175d53 100644 --- a/stdlib/source/lux/data/collection/list.lux +++ b/stdlib/source/lux/data/collection/list.lux @@ -6,7 +6,8 @@ [apply (#+ Apply)] ["." monad (#+ do Monad)] [equivalence (#+ Equivalence)] - [fold (#+ Fold)]] + [fold (#+ Fold)] + [predicate (#+ Predicate)]] [data bit ["." product]]]) @@ -35,22 +36,23 @@ #.Nil xs)) -(def: #export (filter p xs) +(def: #export (filter predicate xs) (All [a] - (-> (-> a Bit) (List a) (List a))) + (-> (Predicate a) (List a) (List a))) (case xs #.Nil #.Nil (#.Cons [x xs']) - (if (p x) - (#.Cons [x (filter p xs')]) - (filter p xs')))) + (if (predicate x) + (#.Cons x (filter predicate xs')) + (filter predicate xs')))) -(def: #export (partition p xs) +(def: #export (partition predicate xs) {#.doc "Divide the list into all elements that satisfy a predicate, and all elements that do not."} - (All [a] (-> (-> a Bit) (List a) [(List a) (List a)])) - [(filter p xs) (filter (complement p) xs)]) + (All [a] (-> (Predicate a) (List a) [(List a) (List a)])) + [(filter predicate xs) + (filter (complement predicate) xs)]) (def: #export (as-pairs xs) {#.doc (doc "Cut the list into pairs of 2." @@ -81,20 +83,20 @@ ) (do-template [<name> <then> <else>] - [(def: #export (<name> p xs) + [(def: #export (<name> predicate xs) (All [a] - (-> (-> a Bit) (List a) (List a))) + (-> (Predicate a) (List a) (List a))) (case xs #.Nil #.Nil (#.Cons [x xs']) - (if (p x) + (if (predicate x) <then> <else>)))] - [take-while (#.Cons [x (take-while p xs')]) #.Nil] - [drop-while (drop-while p xs') xs] + [take-while (#.Cons [x (take-while predicate xs')]) #.Nil] + [drop-while (drop-while predicate xs') xs] ) (def: #export (split n xs) @@ -110,23 +112,23 @@ [(#.Cons [x tail]) rest])) [#.Nil xs])) -(def: (split-with' p ys xs) +(def: (split-with' predicate ys xs) (All [a] - (-> (-> a Bit) (List a) (List a) [(List a) (List a)])) + (-> (Predicate a) (List a) (List a) [(List a) (List a)])) (case xs #.Nil [ys xs] (#.Cons [x xs']) - (if (p x) - (split-with' p (#.Cons [x ys]) xs') + (if (predicate x) + (split-with' predicate (#.Cons [x ys]) xs') [ys xs]))) -(def: #export (split-with p xs) +(def: #export (split-with predicate xs) {#.doc "Segment the list by using a predicate to tell when to cut."} (All [a] - (-> (-> a Bit) (List a) [(List a) (List a)])) - (let [[ys' xs'] (split-with' p #.Nil xs)] + (-> (Predicate a) (List a) [(List a) (List a)])) + (let [[ys' xs'] (split-with' predicate #.Nil xs)] [(reverse ys') xs'])) (def: #export (split-all n xs) @@ -169,18 +171,18 @@ #.None (list x))) -(def: #export (find p xs) +(def: #export (find predicate xs) {#.doc "Returns the first value in the list for which the predicate is #1."} (All [a] - (-> (-> a Bit) (List a) (Maybe a))) + (-> (Predicate a) (List a) (Maybe a))) (case xs #.Nil #.None (#.Cons [x xs']) - (if (p x) + (if (predicate x) (#.Some x) - (find p xs')))) + (find predicate xs')))) (def: #export (search check xs) (All [a b] @@ -231,16 +233,16 @@ (fold (function (_ _ acc) (n/+ 1 acc)) 0 list)) (do-template [<name> <init> <op>] - [(def: #export (<name> p xs) + [(def: #export (<name> predicate xs) (All [a] - (-> (-> a Bit) (List a) Bit)) + (-> (Predicate a) (List a) Bit)) (loop [xs xs] (case xs #.Nil <init> (#.Cons x xs') - (case (p x) + (case (predicate x) <init> (recur xs') @@ -351,7 +353,7 @@ ) (def: #export (empty? xs) - (All [a] (-> (List a) Bit)) + (All [a] (Predicate (List a))) (case xs #.Nil #1 _ #0)) |