From 2b3b5476f5f9eb4bfd2e4e03b14ad4670ae45b01 Mon Sep 17 00:00:00 2001 From: Ray John Alovera Date: Fri, 30 Jul 2021 13:04:06 +0800 Subject: [PATCH 1/3] - add takeWhile function --- List.ark | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/List.ark b/List.ark index 47204be..c442a8e 100644 --- a/List.ark +++ b/List.ark @@ -245,6 +245,34 @@ _output })) +### +# @meta List +# @brief Take the first n elements of +# @param _L the list to work on +# @param _f the predicate +# @details The original list is left unmodified. +# =begin +# (print (takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3] +# =end +# @author https://github.com/rakista112 +## +(let list:takeWhile (fun (_L _f) { + (mut _index 0) + (mut _output []) + + (mut continue true) + (while continue + (if (_f (@ _L _index)) + { + (set _output (append _output (@ _L _index))) + (set _index (+ 1 _index)) + } + (set continue false) + ) + ) + _output +})) + ### # @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]] # @param _L the list to work on From d940e299d086dfd3b1e1ac016015b63c931a3343 Mon Sep 17 00:00:00 2001 From: Ray John Alovera Date: Fri, 30 Jul 2021 13:04:30 +0800 Subject: [PATCH 2/3] remove whitespace --- List.ark | 1 - 1 file changed, 1 deletion(-) diff --git a/List.ark b/List.ark index c442a8e..b8f3850 100644 --- a/List.ark +++ b/List.ark @@ -259,7 +259,6 @@ (let list:takeWhile (fun (_L _f) { (mut _index 0) (mut _output []) - (mut continue true) (while continue (if (_f (@ _L _index)) From 1e08fc9fc3a031c37d05b62962e065aa0133463d Mon Sep 17 00:00:00 2001 From: Ray John Alovera Date: Fri, 30 Jul 2021 13:14:01 +0800 Subject: [PATCH 3/3] - fix infinite loop - add tests --- List.ark | 2 +- tests/list-tests.ark | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/List.ark b/List.ark index b8f3850..120fca1 100644 --- a/List.ark +++ b/List.ark @@ -260,7 +260,7 @@ (mut _index 0) (mut _output []) (mut continue true) - (while continue + (while (and (< _index (len _L)) continue) (if (_f (@ _L _index)) { (set _output (append _output (@ _L _index))) diff --git a/tests/list-tests.ark b/tests/list-tests.ark index f73fb59..863049c 100644 --- a/tests/list-tests.ark +++ b/tests/list-tests.ark @@ -51,6 +51,11 @@ (set tests (assert-eq (list:take a 1) [1] "take" tests)) (set tests (assert-eq (list:take a 100) a "take" tests)) + (set tests (assert-eq (list:takeWhile a (fun (c) (< c 0))) [] "takeWhile" tests)) + (set tests (assert-eq (list:takeWhile a (fun (c) (< c 2))) [1] "takeWhile" tests)) + (set tests (assert-eq (list:takeWhile a (fun (c) (< c 3))) [1 2] "takeWhile" tests)) + (set tests (assert-eq (list:takeWhile a (fun (c) (< c 5))) [1 2 3] "takeWhile" tests)) + (set tests (assert-eq (list:unzip zipped) [[1 2 3 4] [5 6 7 8]] "unzip" tests)) (set tests (assert-eq (list:unzip []) [[] []] "unzip" tests))