Clojure Koans 2 Strings Notebook

Hi there! This is my second Clojure Koans notebook, where I share my Koan exercise answers, questions, and Clojure skill break-downs.

Please note that these notebooks contain Clojure Koan spoilers.

If you are still learning Clojure, and haven't tried to answer these by yourself, I suggest you try first to solve them yourself before seeing my solutions.

Why share your answers, questions, and skill break-downs?

A Clojure friend asked me the other day, "For whom are you creating these notebooks?" My answer is, for a bunch of people!

  • Teachers of Clojure

  • Clojure learners

  • People curious about Clojure

  • Anyone else that is here, hi there!

Those interesting in learning or teaching Clojure can comb through these notebooks, (1) see if they can spot any mistakes, (2) discover new skills that I have not yet observed and named specifically, or (3) find answers to questions I have asked below.

I hope we may collaborate together to make the Clojure community (and learning experience) even more awesome than it already is :)

{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
        ;; complient is used for autocompletion
        ;; add your libs here (and restart the runtime to pick up changes)
        compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.0s
Clojure
(ns koans.02-strings
  (:require [clojure.string :as string]))
0.0s
Clojure
;; "A string is nothing more than text surrounded by double quotes"
(= (str \h \e \l \l \o) "hello")
;; skill 2001: convert a list of character literals to a string using the `str` function
0.0s
Clojure
;; "But double quotes are just magic on top of something deeper"
(= "world" (str 'world))
;; skill 2002: convert a non-string into a string using the `str` function
0.0s
Clojure
;; "You can do more than create strings, you can put them together"
(= "Cool right?" (str "Cool " "right?"))
;; skill 2003: concatenate a list of strings into one string using the `str` function
0.0s
Clojure
;; "You can even get certain characters"
(= \C (get "Characters" 0))
;; skill 2004: access a character at index X of a string using the `get` function
0.0s
Clojure
;; "Or even count the characters"
(= 11 (count "Hello World"))
;; skill 2005: count the # of characters in a string using the `count` function
0.0s
Clojure
;; "But strings and characters are not the same"
(= false (= \c "c"))
0.0s
Clojure
;; "What if you only wanted to get part of a string?"
(= "World" (subs "Hello World" 6 11))
;; skill 2006: extract a substring from a string using the `subs` function with one input string and two index arguments
0.0s
Clojure
;; "How about joining together elements in a list?"
(= "123" (string/join '(1 2 3)))
;; skill 2007: concat list items together into a string using clojure.string/join
0.0s
Clojure
;; "What if you wanted to separate them out?"
(= "1, 2, 3" (string/join ", " '(1 2 3)))
;; skill 2008: concat list items together with interleaved (?) inbetween text using clojure.string/join and a string argument
0.0s
Clojure
;; "Maybe you want to separate out all your lines"
(= ["1" "2" "3"] (string/split-lines "1\n2\n3"))
;; skill 2009: split a string at new-lines into a vector (?) using clojure.string/split-lines
0.0s
Clojure
;; "You may want to make sure your words are backwards"
(= "olleh" (string/reverse "hello"))
;; skill 2010: reverse a string using clojure.string/reverse
0.0s
Clojure
;; "Maybe you want to find the index of the first occurrence of a substring"
(= 0 (string/index-of "hello world" "h"))
;; skill 2011: get the first index occurance of a string sequence in a string using clojure.string/index-of
0.0s
Clojure
;; "Or maybe the last index of the same"
(= 13 (string/last-index-of "hello world, hello" "hello"))
;; skill 2012: get the last index occurance of a string sequnece in a string using clojure.string/last-index-of
0.0s
Clojure
;; "But when something doesn't exist, nothing is found"
(= nil (string/index-of "hello world" "bob"))
0.0s
Clojure
;; "Sometimes you don't want whitespace cluttering the front and back"
(= "hello world" (string/trim "  \nhello world \t \n"))
;; skill 2013: remove leading and trailing whitespace from strings by using clojure.string/trim.
0.0s
Clojure
;; "You can check if something is a char"
(= true (char? \c))
;; skill 2014: check if something is a char using the `char?` function
0.0s
Clojure
;; "Strings are strings"
(= true (string? "sleepy"))
;; skill 2015: check if something is a string using the `string?` function
0.0s
Clojure
;; "Some strings may be blank"
(= true (string/blank? ""))
;; skill 2016: check if a string is devoid of non-whitespace using the `blank?` function
0.0s
Clojure

Initial Observations: There aren't too many big surprises coming from JavaScript, Java, Python, etc. as to how strings and characters behave. I like how the `str` function behaves as concatenation or type-casting, depending on what is passed to it.

Secondary Observations: There are more skills here than in the 1st section ("Equality"). I believe there might be more to equality in Clojure than what the first Koan section indicates (e.g. sorting maps, comparing maps/sets, and custom strict/loose equality). That said, I think it's appropriate that the earlier Koan sections have low complexity / difficulty so as to be welcoming for new Clojurists.

Runtimes (1)