Created
October 5, 2024 15:15
-
-
Save chase-lambert/2acee06be1b153f905bc53160eb7ae6a to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 24.09.30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns split | |
"Prompt: Implement your own String split() function | |
in your preferred programming language. | |
My solution is only for single character separators | |
so is not nearly as useful as Clojure's version | |
allowing for regexes." | |
(:require [clojure.test :refer [deftest is]])) | |
(defn split [s sep] | |
(if (= sep "") | |
(map str s) | |
(loop [s s | |
acc [] | |
curr ""] | |
(if (seq s) | |
(let [c (str (first s))] | |
(if (= c sep) | |
(recur (rest s) (conj acc curr) "") | |
(recur (rest s) acc (str curr c)))) | |
(conj acc curr))))) | |
(deftest split-test | |
(let [s "This is so, so silly!"] | |
(is (= (split s " ") | |
["This" "is" "so," "so" "silly!"])) | |
(is (= (split s "") | |
["T" "h" "i" "s" " " "i" "s" " " "s" "o" "," " " "s" "o" " " "s" "i" "l" "l" "y" "!"])) | |
(is (= (split s ",") | |
["This is so" " so silly!"])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment