Skip to content

A peer-to-peer stack for code collaboration

3.9 KB
Raw Blame
{:module 'prelude/seq
 :doc "Functions for manipulating sequences, that is lists and vectors."
 :exports '[empty? seq? reverse filter take-while starts-with? /prefix
            concat]}

(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
(import prelude/patterns :unqualified)

(def seq?
  "Returns `#t` if `x` is a list or a vector."
  (fn [x] (or (list? x) (vector? x))))

(:test "seq?"
  [ (seq? (list)) ==> #t ]
  [ (seq? []) ==> #t ]
  [ (seq? :foo) ==> #f ]
  [ (seq? "foo") ==> #f ]
  )

(def empty?
  "True if `seq` is empty, false otherwise."
  (fn [seq] (or (eq? seq '()) (eq? seq []))))

(:test "empty"
  [ (empty? (list 2)) ==> #f ]
  [ (empty? (list)) ==> #t ]

  [ (empty? [2]) ==> #f ]
  [ (empty? []) ==> #t ]
)


(def reverse
  "Returns the reversed sequence `xs`."
  (fn [xs]
    (def-rec go
      (fn [acc new]
        (if (empty? new)
            acc
            (go (cons (head new) acc) (tail new)))))
    (match (type xs)
      :list (go '() xs)
      :vec (go [] xs)
      't (throw 'type-mismatch (string-append "reverse: exepcted sequence as arguemtn got " (show t)))
      )
    ))

(:test "reverse"
  [ (reverse (list 1 2 3)) ==> (3 2 1) ]
  [ (reverse [1 2 3]) ==> [3 2 1] ]
)


(def-rec filter
  "Returns `ls` with only the elements that satisfy `pred`."
    (fn [pred ls]
      (cond
        (empty? ls)      ls
        (pred (head ls)) (cons (head ls) (filter pred (tail ls)))
        :else            (filter pred (tail ls)))))

(:test "filter"
  [ (filter (fn [x] (< x 10)) (list 3 10 11 5))
     ==> (3 5) ]
  [ (filter (fn [x] (< x 10)) [3 10 11 5])
     ==> [3 5] ]
)


(def-rec take-while
  "Returns all elements of a sequence `ls` until one does not satisfy `pred`"
  (fn [pred ls]
    (cond
      (empty? ls)       ls
      (pred (head ls))  (cons (head ls) (take-while pred (tail ls)))
      :else             (if (list? ls)
                          (list)
                          []))))

(:test "take-while"
  [ (take-while (fn [x] (eq? x "bar")) (list "foo" "bar") ) ==> () ]
  [ (take-while (fn [x] (not (eq? x "bar"))) (list "foo" "bar") ) ==> ("foo") ]
  [ (take-while (fn [x] (< x 10)) [3 9 10 11] ) ==> [3 9] ])


(def starts-with?
  "Returns `#t` if `prefix` is a prefix of the sequence `s`. Also works for strings"
  (fn [s prefix]
    (eq? (take (length prefix) s) prefix)))

(:test "starts-with?"
  [ (starts-with? "abc" "ab") ==> #t ]
  [ (starts-with? "abc" "abc") ==> #t ]
  [ (starts-with? "abc" "") ==> #t ]
  [ (starts-with? [1 2 3] []) ==> #t ]
  [ (starts-with? [1 2 3] [1 2]) ==> #t ]
  [ (starts-with? [1 2 3] [1 2 4]) ==> #f ]
  [ (starts-with? [1 2 3] [1 2 3]) ==> #t ]
  [ (starts-with? [] []) ==> #t ]
  [ (starts-with? [] [1]) ==> #f ]
  )

(def /prefix
  "Matches sequences that start with `prefix` and bind the rest of that
  sequence to `rest-pat`. Also works for strings."
  (fn [prefix rest-pat]
    (fn [value]
      (if (or (string? value) (seq? value))
        (if (starts-with? value prefix)
          (match-pat rest-pat (drop (length prefix) value))
          :nothing)
        :nothing)
  )))

(:test "/prefix"
  [ (match [0 2 3]
      (/prefix [0 2] 'rs) rs
      )
    ==> [3] ]
  [ (match '(1 2 3)
      (/prefix '(1 2) 'rs) rs
      )
    ==> (3) ]
  [ (match "abcd"
      (/prefix "ab" 'rs) rs
      )
    ==> "cd" ]
  ;; nested pattern
  [ (match [1 2 3]
      (/prefix [1 2] (/as 'rs _)) rs
      )
    ==> [3] ]
  ;; fail on keyword
  [ (match :foo
      (/prefix [] _) #f
      _              :fall-through
      )
    ==> :fall-through ]
  ;; fail if type differs
  [ (match '()
      (/prefix [] _) #f
      _              :fall-through
      )
    ==> :fall-through ]
  )

(def concat
  "Concatenate a sequence of sequences."
  (fn [ss] (foldl <> [] ss)))

(:test "concat"
       [ (concat []) ==> [] ]
       [ (concat [[]]) ==> [] ]
       [ (concat [[] []]) ==> [] ]
       [ (concat [[1 2] [3 4] [5]]) ==> [1 2 3 4 5]]
       [ (concat [[] [1 2] [] [3 4] [5] []]) ==> [1 2 3 4 5]])