Updated 1_On_Strings.txt

rhgraysonii authored
revision 0030b6010e7336279d7932e8525eb3f0506414a6
1_On_Strings
# Strings

Strings are one of the most intrinsic things that we use to communicate. This book is essentially being parsed in your mind as a series of strings. Strings are often objects of input/output in programs to be modified, displayed, and fudged with. Lets look at some example strings in Clojure:

(str "Are you wearing pants?")
;=>"Are you wearing pants?")

("Are you wearing pants?")
;=>"Are you wearing pants

As you can see, if using the str method you will just get the original string returned if you are not concatenating anything, as we did earlier.

If you're familiar with a variety of languages you probably are of the understanding that depending on what you are working with, a string can be both an object or a primitive. In Ruby, we could define a string as follows:

this = String.new("I'm not wearing pants")

or even

this = "I'm not wearing pants"

However, the first example lets us look at a core feature of Ruby. *Everything* is an object (as Sherlock may have suspected, it being an object oriented language and all).Let us Look at a Javascript implementation:

pants = "a thing you should not wear when relaxing at home"

Now, in Javascript we could also define it as

String("Man, I sure hate wearing pants")
=>"Man, I sure hate wearing pants"

In Javascript a string is actually considered a primitive. However to be able to call methods on it, JS will coerce this primitive into behaving as an object. This is one of the defining and interesting features I've found personally in JS, but this book is on Clojure. So let us look at Clojure's handling of this.

In Clojure, though it is a representation of a string object in Java, it is not a string object in the sense of how we will utilize it and investigate its properties as well as compare them to others.

(=("I just took off my pants")(str ("I just took off my ")("pants))
;=>true

In Clojure, we aren't functioning in an Object Oriented language. It is primarily functional in its application.

Though Clojure itself is not an object oriented language, it is, however, implemented in Java. The compilation results in the code being run as Java VM code. This results in a string in Clojure being compiled as an instance of a String object in Java, which is a series of char primitives.

Most of Clojure's data types are immutable, as mentioned prior. They never change. Clojure favors comparison of equality much moreso than comparison of identity. In most languages this is discouraged because the values are deeply built into the structure of each instance of an object. Thusly, Clojure compares content. An example:

(= "pants" ( str "pan" "ts"))

;=>true

However, in Ruby:

class Foo
end

Now that we have defined a class, lets make some instances of it

a = Foo.new()
b = Foo.new()
a == b
>false

Clojure makes this comparison cheap on resources. A Clojure object in this situation is keeping around a hash of itself, and this hash is what is compared rather than deeply inspecting it. This, however, only works if all parties involved are completely immutable. As stated, not ALL pieces of Clojure are immutable.

As with many other languages, strings in Clojure have many built in methods. Here are some examples:

(str)
;=>""

The str method will simply return an empty string if there are no other list items.

(string? "x")
;=>true

This operator will determine if a given item is a string. However, it cannot handle multiple arguments.

(string? "x" 1)
;=>clojure.lang.ArityException: Wrong number of args (2) passed to: core$string-QMARK-

Clojure's way of recognizing regular expressions is the '#' operator. The
re-find and re-match are the functions Clojure uses for its matches.

(re-find #"I don't have pants on" "Man I am sure glad I don't have pants on")
;=>"I don't have pants on

(re-find #"pants" "I do not adorm my lower half with garments!")
;=>nil

(rest (re-matches #"(.+)8==Dfun(.+)" "hahahah8===Dit looks like a penisIts all fun and games until someone gets hurt"))
;=>hahahah it looks like a penis

The last version does a more standardized regex rather than a simple text search. These will be examined further later when working with web applications where they are quite useful for formatting and validation.

There is also capitalize and trim-newline (akin to chomp) methods just like Ruby. They perform as expected (capitalize the first letter of a word, and remove a trailing newline, respectively). For an in depth listing of String methods in Clojure check out [the documentation](http://clojure.github.io/clojure/clojure.string-api.html).

There are no exercises for this chapter. Just meditate on what a string is when it is not truly an object. The key of this exercise is the differentiation between the typical OO class model vs allowing rote comparison of contents as Clojure does. This will be just the beginning of our new way of "thinking in Clojure".