Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / stdlib / collections / collection_primitives.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 working on procedures, using type specialized λs

(defn filter (l p)
      "Runs predicate p over every item in collection l and returns a list consiting solely of items where p is true"
      (case (:type-name l)
            (:nil #nil)
            (:pair   (list/filter  l p))
            (:array  (array/filter l p))
            (:tree   (tree/filter  l p))
            (:map    (map/filter  l p))
            (otherwise (exception :type-error "You can only filter collections" l ))))

(defn reduce (l f α)
      "Combine all elements in collection l using operation F and starting value α"
      (case (:type-name l)
            (:nil    α)
            (:tree   (tree/reduce  l f α))
            (:map    (map/reduce  l f α))
            (:array  (array/reduce l f α))
            (:pair   (list/reduce  l f α))
            (otherwise (exception :type-error "You can only reduce collections" l ))))

(defn map (l f)
      "Runs f over every item in collection l and returns the resulting list"
      (case (:type-name l)
            (:nil    #nil)
            (:pair   (list/map  l f))
            (:array  (array/map l f))
            (otherwise (exception :type-error "You can only use map with a collection" l ))))

(defn sort (l)
      "Sorts the collection L"
      (case (:type-name l)
            (:nil      #nil)
            (:pair     (list/sort  l))
            (:array    (array/sort l))
            (otherwise (exception :type-error "You can only use sort with a collection" l ))))

(defn cut (l start end)
      "Return a subcollection of L from START to END"
      (case (:type-name l)
            (:pair     (list/cut   l start end))
            (:array    (array/cut  l start end))
            (:string   (:cut l start end))
            (otherwise (exception :type-error "You can only use member with a collection" l ))))