Login
7 branches 0 tags
Ben (X13/Arch) Added some more abstract proccedures for collections 55fe85a 4 years ago 223 Commits
nujel / stdlib / collection.nuj
; A bunch of procedurs working on procedures, using type specialized λs

[defun length [α]
       "Returns the length of collection α"
       [case [type-of α]
             [[:pair]   [list/length   α]]
             [[:string] [string/length α]]
             [otherwise [throw [list :invalid-type "You can only use length with a collection" α [current-lambda]]]]
       ]
]

[defun filter [p α]
       "Runs predicate p over every item in list l and returns a list consiting solely of items where p is true"
       [case [type-of α]
             [[:pair] [list/filter p α]]
             [[:nil] #nil]
             [otherwise [throw [list :invalid-type "You can only use filter with a collection" α [current-lambda]]]]
       ]
]

[defun for-each [f α]
       "Runs f over every item in collection l and returns the resulting list"
       [case [type-of α]
             [[:pair] [list/for-each f α]]
             [[:nil] #nil]
             [otherwise [throw [list :invalid-type "You can only use for-each with a collection" α [current-lambda]]]]
       ]
]

[defun map [f α]
       "Runs f over every item in collection l and returns the resulting list"
       [case [type-of α]
             [[:pair] [list/map f α]]
             [[:nil] #nil]
             [otherwise [throw [list :invalid-type "You can only use map with a collection" α [current-lambda]]]]
       ]
]