Create the meditations of a string koan
Covers * String creation * String concatenation * Counting strings characters and getting them * Difference between chars and strings * Substrings * string/join w/ and w/o separators * string/split-lines * .indexOf and .lastIndexOf * string/trim * char? and string? * blank?
This commit is contained in:
parent
e2a9f9556b
commit
5a836be75c
@ -10,9 +10,29 @@
|
||||
"hello"
|
||||
3]}]
|
||||
|
||||
["02_strings" {"__" [
|
||||
|
||||
]}]
|
||||
["02_strings" {"__" ["hello"
|
||||
"world"
|
||||
"Cool "
|
||||
"right?"
|
||||
0
|
||||
11
|
||||
false
|
||||
6 11
|
||||
"123"
|
||||
", "
|
||||
"1" "2" "3"
|
||||
"olleh"
|
||||
"hello"
|
||||
13
|
||||
-1
|
||||
"hello world"
|
||||
true
|
||||
false
|
||||
false
|
||||
"a"
|
||||
true
|
||||
true
|
||||
false]}]
|
||||
|
||||
["03_lists" {"__" [1 2 3 4 5
|
||||
1
|
||||
|
@ -1,4 +1,70 @@
|
||||
(ns koans.02-strings
|
||||
(:require [koan-engine.core :refer :all]))
|
||||
(:require [koan-engine.core :refer :all]
|
||||
[clojure.string :as string]))
|
||||
|
||||
(meditations)
|
||||
(meditations
|
||||
"A string is nothing more than text surrounded by double quotes"
|
||||
(= __ "hello")
|
||||
|
||||
"But double quotes are just magic on top of something deeper"
|
||||
(= __ (str 'world))
|
||||
|
||||
"You can do more than create strings, you can put them together"
|
||||
(= "Cool right?" (str __ __))
|
||||
|
||||
"You can even get certain characters"
|
||||
(= \C (get "Characters" __))
|
||||
|
||||
"Or even count the characters"
|
||||
(= __ (count "Hello World"))
|
||||
|
||||
"But strings and characters are not the same"
|
||||
(= __ (= \c "c"))
|
||||
|
||||
"What if you only wanted to get part of a string?"
|
||||
(= "World" (subs "Hello World" __ __))
|
||||
|
||||
"How about joining together elements in a list?"
|
||||
(= __ (string/join '(1 2 3)))
|
||||
|
||||
"What if you wanted to seperate them out?"
|
||||
(= "1, 2, 3" (string/join __ '(1 2 3)))
|
||||
|
||||
"Maybe you want to seperate out all you're lines"
|
||||
(= [__ __ __] (string/split-lines "1\n2\n3"))
|
||||
|
||||
"You may want to make sure you're words are backwards"
|
||||
(= __ (string/reverse "hello"))
|
||||
|
||||
"Maybe you want to find the index of the first occurence of a substring"
|
||||
(= 0 (.indexOf "hello world" __))
|
||||
|
||||
"Or maybe the last index of the same"
|
||||
(= __ (.lastIndexOf "hello world, hello" "hello"))
|
||||
|
||||
"But when something doesn't exist, it turns up negative"
|
||||
(= __ (.indexOf "hello world" "bob"))
|
||||
|
||||
"Sometimes you don't want whitespace cluttering the front and back"
|
||||
(= __ (string/trim " \nhello world \t \n"))
|
||||
|
||||
"You can check if something is a char"
|
||||
(= __ (char? \c))
|
||||
|
||||
"But it may not be"
|
||||
(= __ (char? "a"))
|
||||
|
||||
"But chars aren't strings"
|
||||
(= __ (string? \b))
|
||||
|
||||
"Strings are strings"
|
||||
(= true (string? __))
|
||||
|
||||
"Some strings may be blank"
|
||||
(= __ (string/blank? ""))
|
||||
|
||||
"Even if at first glance they aren't"
|
||||
(= __ (string/blank? " \n \t "))
|
||||
|
||||
"However, most strings aren't blank"
|
||||
(= __ (string/blank? "hello?\nare you out there?")))
|
||||
|
Loading…
Reference in New Issue
Block a user