Login
7 branches 0 tags
Ben (X13/Void) Added day18 test 0562e01 4 years ago 322 Commits
nujel / stdlib / collection.nuj
; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
; This project uses the MIT license, a copy should be included under /LICENSE

;; A bunch of procedurs that work on all collections where the collection primitives are implemented

[defun sum [c]
       "Return the sum of every value in collection C"
       [reduce + c 0]]

[defun join [l glue]
       "Join every element of α together into a string with GLUE inbetween"
       [when-not glue [set! glue ""]]
       [if-not l ""
               [reduce [λ [a b] [if a [cat a glue b] b]] l #nil]]]

[defun for-each [f l]
       "Runs F over every item in collection L and returns the resulting list"
       [reduce [λ [a b] [f b]] l #nil]]

[defun count [l p]
       "Count the number of items in L where P is true"
       [reduce [λ [a b] [+ a [if [p b] 1 0]]] l 0]]

[defun min l
       "Returns the minimum value of its arguments, or collection"
       [reduce [λ [a b] [if [< a b] a b]]
               [if [cdr l] l [car l]]
               #nil]]

[defun max l
       "Returns the minimum value of its arguments, or collection"
       [reduce [λ [a b] [if [> a b] a b]]
               [if [cdr l] l [car l]]
               #nil]]