Skip to content

A peer-to-peer stack for code collaboration

4.4 KB
Raw Blame
{:module 'prelude/strings
 :doc    "String manipulation functions."
 :exports '[intercalate unlines unwords split-by words lines map-string
            reverse-string ends-with? pad-right-to] }

(import prelude/basic :unqualified)
(import prelude/seq :unqualified)
(import prelude/list :unqualified)
(import prelude/patterns :unqualified)

(def intercalate
  "Intercalates a string in a list of strings"
  (fn [sep strs]
    (if (eq? (length strs) 0)
        ""
        (foldl
          string-append
          (first strs)
          (map (fn [ln] (string-append sep ln)) (tail strs))))))

(:test "intercalate"
  [ (intercalate "aa" ["hi" "there"]) ==> "hiaathere" ]
)

(def unlines
  "Concatenate a list of strings, with newlines in between."
  (fn [x]
    (intercalate "\n" x)))

(:test "unlines"
  [ (unlines ["hi" "there"]) ==> "hi\nthere" ]
)

(def unwords
  "Concatenate a list of strings, with spaces in between."
  (fn [x]
    (intercalate " " x)))

(:test "unwords"
  [ (unwords ["hi" "there"]) ==> "hi there" ]
)

(def space-chars
  "A list of all white space character."
  [" " "\t" "\n"])

(def is-space-char?
  "Returns true if the character `x` is a space character `space-chars`."
  (fn [x]
    (elem? x space-chars)))

(:test "is-space-char?"
  [ (is-space-char? "s") ==> #f]
  [ (is-space-char? "s ") ==> #f]
  [ (is-space-char? " ") ==> #t]
  [ (is-space-char? "\t") ==> #t]
  [ (is-space-char? "\n") ==> #t])

;; split-by

(def split-by
  "Splits a string `xs` into a list of strings whenever the function `splitter?`
  returns true for a character."
  (fn [splitter? xs]
    (def f (fn [acc new-char]
      (def ls (nth 0 acc))
      (def current (nth 1 acc))
      (if (splitter? new-char)
          (if (eq? current "")
              [ls ""]
              [(add-right current ls) ""])
          [ls (string-append current new-char)])))
    (match (foldl-string f [[] ""] xs)
      ['ls ""] ls
      ['ls 'x] (add-right x ls))))

(:test "split-by"
  [ (split-by (fn [x] (eq? "b" x)) "foobar") ==> ["foo" "ar"]]
  [ (split-by is-space-char? "foo bar") ==> ["foo" "bar"]])

;; words

(def words
  "Splits a string `xs` into a list of strings by whitespace characters."
  (fn [xs]
    (split-by is-space-char? xs)))

(:test "words"
  [ (words "foo bar") ==> ["foo" "bar"]]
  [ (words "foo bar\nfoo ") ==> ["foo" "bar" "foo"]]
  [ (words "foo   bar") ==> ["foo" "bar"]]
  [ (words "foo") ==> ["foo"]]
  [ (words " \n \t") ==> []]
  [ (words "") ==> []])


;; lines

(def lines
  "Splits a string `xs` into a list of strings by linebreaks."
  (fn [xs]
    (split-by (fn [x] (eq? "\n" x)) xs)))

(:test "lines"
  [ (lines "foo bar") ==> ["foo bar"]]
  [ (lines "foo bar\nfoo ") ==> ["foo bar" "foo "]]
  [ (lines "foo bar\n\n\nfoo") ==> ["foo bar" "foo"]]
  [ (lines " \n \t") ==> [" " " \t"]]
  [ (lines "") ==> []])

;; pad-to

(def pad-right-to
  "Appends the `word` with whitespace to get to length `l`. If `word` is longer
  than `l`, the whole word is returned without padding."
  (fn [l word]
    (def w-length (length word))
    (if (< (- l 1) w-length)
      word
      (do
        (def pad (intercalate "" (map (fn [x] " ") (range 1 l))))
        (string-append word (take (- l w-length) pad))))))

(:test "pad-right-to"
  [ (pad-right-to 0 "foobar") ==> "foobar"]
  [ (pad-right-to 3 "foobar") ==> "foobar"]
  [ (pad-right-to 6 "") ==> "      "]
  [ (pad-right-to 6 "foobar") ==> "foobar"]
  [ (pad-right-to 7 "foobar") ==> "foobar "])

;; map-string

(def map-string
  "Returns a string consisting of the results of applying `f` to each character
  of `xs`. Throws a type error if `f` returns something other than a string"
  (fn [f xs]
    (def g
      (fn [acc new-char]
        (string-append acc (f new-char))))
    (foldl-string g "" xs)))

(:test "map-string"
  [ (map-string (fn [x] "A") "foo") ==> "AAA"]
  [ (map-string (fn [x] "Ah") "foo") ==> "AhAhAh"]
  [ (map-string (fn [x] "A") "") ==> ""]
  [ (map-string (fn [x] "") "foo") ==> ""]
  [ (catch 'type-error (map-string (fn [x] 2) "foo") (fn [x] :caught)) ==> :caught])

(def reverse-string
  "Reverses `str`. E.g.: `(reverse-string \"abc\")` == `\"cba\"`."
  (fn [str]
    (def g (fn [a b] (string-append b a)))
    (foldl-string g "" str)))

(:test "reverse-string"
  [ (reverse-string "abc") ==> "cba" ]
)

(def ends-with?
  "True if `str` ends with `substr`"
  (fn [str substr]
    (starts-with? (reverse-string str) substr)))

(:test "ends-with?"
  [ (ends-with? "abc" "c") ==> #t]
)