Skip to content

Commit 524b6fb

Browse files
authored
Merge pull request #26 from Gryfenfer97/master
add list:forAll and list:any
2 parents 7953757 + e71d7e9 commit 524b6fb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

List.ark

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,49 @@
313313
})
314314
_val
315315
}))
316+
317+
###
318+
# @brief Check if a condition is verified for all elements of a list
319+
# @param a the list to work on
320+
# @param f the conditon
321+
# =begin
322+
# (let a [1 2 3 4])
323+
# (let f (fun(e)(< e 5)))
324+
# (print (list:forAll a f)) # true
325+
# =end
326+
# @author https://github.com/Gryfenfer97
327+
##
328+
(let list:forAll (fun (a f) {
329+
(mut verified true)
330+
(mut index 0)
331+
(while (and verified (< index (len a))) {
332+
(if (not (f (@ a index)))
333+
(set verified false)
334+
)
335+
(set index (+ 1 index))
336+
})
337+
verified
338+
}))
339+
340+
###
341+
# @brief Check if a condition if verified for one or more elements of a list
342+
# @param a the list to work on
343+
# @param f the conditon
344+
# =begin
345+
# (let a [1 2 3 4])
346+
# (let f (fun(e)(< e 3)))
347+
# (print (list:any a f)) # true
348+
# =end
349+
# @author https://github.com/Gryfenfer97
350+
##
351+
(let list:any (fun (a f) {
352+
(mut verified false)
353+
(mut index 0)
354+
(while (and (not verified) (< index (len a))) {
355+
(if (f (@ a index))
356+
(set verified true)
357+
)
358+
(set index (+ 1 index))
359+
})
360+
verified
361+
}))

tests/list-tests.ark

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
(set tests (assert-eq a [1 2 3] "unmodified list" tests))
6565
(set tests (assert-eq b [4 5 6] "unmodified list" tests))
6666

67+
(set tests (assert-eq (list:forAll a (fun (e) (< e 4))) true "list:forAll" tests))
68+
(set tests (assert-eq (list:forAll a (fun (e) (< e 2))) false "list:forAll" tests))
69+
(set tests (assert-eq (list:forAll [] (fun (e) (= e 2))) true "list:forAll" tests))
70+
(set tests (assert-eq (list:any a (fun (e) (< e 2))) true "list:any" tests))
71+
(set tests (assert-eq (list:any a (fun (e) (> e 8))) false "list:any" tests))
72+
(set tests (assert-eq (list:any [] (fun (e) (= e 8))) false "list:any" tests))
73+
6774
(recap "List tests passed" tests (- (time) start-time))
6875

6976
tests

0 commit comments

Comments
 (0)