application/octet-stream
•
1.62 KB
•
46 lines
; 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 l [λ [a b] [if a [cat a glue b] b]] #nil]]]
[defun for-each [l f]
"Runs F over every item in collection L and returns the resulting list"
[reduce l [λ [a b] [f b]] #nil]]
[defun count [l p]
"Count the number of items in L where P is true"
[if p
[reduce l [λ [a b] [+ a [if [p b] 1 0]]] 0]
[reduce l [λ [a b] [+ a 1]] 0]]]
[defun min/λ [a b] [if [< a b] a b]]
[defun min l
"Returns the minimum value of its arguments, or collection"
[reduce [if [cdr l] l [car l]] min/λ #nil]]
[defun max/λ [a b] [if [> a b] a b]]
[defun max l
"Returns the minimum value of its arguments, or collection"
[reduce [if [cdr l] l [car l]] max/λ #nil]]
[defun delete [l e]
"Returns a filtered list l with all elements equal to e omitted"
[filter l [λ [a] [not [== a e]]]]]
[defun flatten/λ [a b]
[cond [[collection? b ] [append [reduce b flatten/λ #nil] a]]
[#t [cons b a]]]]
[defun flatten [l]
"Flatten a collection of collections into a simple list"
[if-not [collection? l] l
[nreverse [reduce l flatten/λ #nil]]]]