Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active May 17, 2023 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chase-lambert/1bfaf57cc9fb61a61667cd9021279754 to your computer and use it in GitHub Desktop.
Save chase-lambert/1bfaf57cc9fb61a61667cd9021279754 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 22.10.10
(ns truncate
(:require [clojure.test :refer [deftest is]))
(defn truncate [s n]
(let [tokens (re-seq #"\W+|_|[a-zA-Z]+" s)
word? #(re-find #"\w+" %)
truncate-w (fn [token]
(if (and (word? token)
(> (count token) n))
(subs token 0 n)
token))
truncated (map truncate-w tokens)]
(apply str truncated)))
(deftest truncate-test
(let [n 3]
(is (= "nev gon giv you up" (truncate "never gonna give you up" n)))
(is (= "*hel* dar, my ~old_fri" (truncate "*hello* darkness, my ~old_friend" n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment