Skip to content

Commit 1107dd5

Browse files
author
Ray Alovera
committed
- add takeWhile function
1 parent 26373ce commit 1107dd5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

List.ark

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,34 @@
245245
_output
246246
}))
247247

248+
###
249+
# @meta List
250+
# @brief Take the first n elements of
251+
# @param _L the list to work on
252+
# @param _f the predicate
253+
# @details The original list is left unmodified.
254+
# =begin
255+
# (print (takeWhile [1 2 3 4 5 6 7 8 9 10] (fun (a) (< a 4)))) # [1 2 3]
256+
# =end
257+
# @author https://github.com/rakista112
258+
##
259+
(let list:takeWhile (fun (_L _f) {
260+
(mut _index 0)
261+
(mut _output [])
262+
263+
(mut continue true)
264+
(while continue
265+
(if (_f (@ _L _index))
266+
{
267+
(set _output (append _output (@ _L _index)))
268+
(set _index (+ 1 _index))
269+
}
270+
(set continue false)
271+
)
272+
)
273+
_output
274+
}))
275+
248276
###
249277
# @brief Unzip a list of [[a b] [c d]...] into [[a c ...] [b d ...]]
250278
# @param _L the list to work on

0 commit comments

Comments
 (0)