Week 1 Retrospective

This first week at The Iron Yard has been interesting. I was expecting it to be mostly review of concepts that I already know. And it has been, mostly.

Java is a language that I have looked at a little bit but it is structurally similar to C++ which I have used many years ago. So the initial stages of class on data types and variables were largely review. I was interested to learn that Java’s primitive data types should not be used for financial calculations because of rounding errors. Coming from a scientific background and having done a lot of data manipulations and calculations for my research I can easily see the problems that can come from this. So I have read up a little bit on the BigDecimal class and I will be starting to use it soon.

On the other hand, Clojure is completely new to me! So I have focused on learning it to challenge myself. For example one of our homework projects this week was to create a Clojure function to print out a MadLib. I ran into a few problems with this project. The first was that the entire MadLib printed out on one long line. My first attempt at correcting this was to add “\n” periodically in the text of my function to force line breaks.

That worked but it did not account for differences in the length of input words:

Albert Einstein, the son of  Tom Cruise  and  Madonna , 
was born in Ulm, Germany, in 1879. In 1902, he had a job
as assistant brick in the Swiss patent office and attended
the University of Zurich. There he began studying atoms, molecules,
and deer . He developed the theory of silly relativity,
which expanded the phenomena of sub-atomic
gorges and furry magnetism. In 1921,
he won the Nobel prize for classes and was director of
theoretical physics at the Kaiser Wilhelm car in Berlin.
In 1933, when Hitler became Chancellor of Raleigh ,
Einstein came to America to take a post at Princeton Institute for
knives , where his theories helped America devise the first
atomic tree . There is no question about it: Einstein was
one of the most brilliant trash men of our time.

I also noticed that each of my input words (e.g. Madonna or Tom Cruise) was padded by a space at either end. After asking my instructor about this, I added the str() command which concatenates the strings it is fed with spaces to get this:

Albert Einstein, the son of Tom Cruise and Madonna, 
was born in Ulm, Germany, in 1879. In 1902, he had a job
as assistant brick in the Swiss patent office and attended
the University of Zurich. There he began studying atoms, molecules,
and deer. He developed the theory of sillyrelativity,
which expanded the phenomena of sub-atomic
gorges and furry magnetism. In 1921,
he won the Nobel prize for classes and was director of
theoretical physics at the Kaiser Wilhelm car in Berlin.
In 1933, when Hitler became Chancellor of Raleigh,
Einstein came to America to take a post at Princeton Institute for
knives, where his theories helped America devise the first
atomic tree. There is no question about it: Einstein was
one of the most brilliant trash men of our time.

Better but the line breaks are still not looking like I would prefer. At this point I did some Googling to figure out how to add line breaks to my string. It took a few tries but I was able to find this:

(defn wrap-line [size text]
(re-seq (re-pattern (str ".{0," size "}\\s"))
(clojure.string/replace text #"\n" " ")))

(defn madlib-albert-einstein [place adjective1 adjective2
female-celebritymale-celebrity noun1
noun2 noun3 plural-noun1 plural-noun2
plural-noun3 plural-noun4
plural-profession]

(def text (str "Albert Einstein, the son of " male-celebrity
" and " female-celebrity ", was born in Ulm, "
"Germany, in 1879. In 1902, he had a job "
"as assistant " noun1 " in the Swiss patent "
"office and attended the University of Zurich. "
"There he began studying atoms, molecules, "
"and " plural-noun1 ". He developed the theory "
"of " adjective1 "relativity, which expanded "
"the phenomena of sub-atomic " plural-noun2
" and " adjective2 " magnetism. In 1921, "
"he won the Nobel prize for " plural-noun3
" and was director of theoretical physics at "
"the Kaiser Wilhelm " noun2 " in Berlin. "
"In 1933, when Hitler became Chancellor of "
place ", "Einstein came to America to take a "
"post at Princeton Institute for " plural-noun4
", where his theories helped America devise the "
"first atomic " noun3 ". There is no question "
"about it: Einstein was one of the most "
"brilliant " plural-profession " of our time."))
(doseq [line (wrap-line 72 text)]
(println line))

I do not fully understand the wrap-line function yet but but the outline is that it uses a RegEx (Regular Expression) to find spaces approximately every 72 characters, and then replaces that space with a “\n” or newline character. Obviously since I do not fully understand the wrap-line function, I borrowed it from https://www.rosettacode.org/wiki/Word_wrap#Clojure. The final output looks like this:

Albert Einstein, the son of Tom Cruise and Madonna, was born 
in Ulm, Germany, in 1879. In 1902, he had a job as assistant
brick in the Swiss patent office and attended the University
of Zurich. There he began studying atoms, molecules, and
deer. He developed the theory of sillyrelativity, which
expanded the phenomena of sub-atomic gorges and furry
magnetism. In 1921, he won the Nobel prize for classes and
was director of theoretical physics at the Kaiser Wilhelm
car in Berlin. In 1933, when Hitler became Chancellor of
Raleigh, Einstein came to America to take a post at
Princeton Institute for knives, where his theories helped
America devise the first atomic tree. There is no question
about it: Einstein was one of the most brilliant trash men
of our time.

No extra spaces and a nice clean word wrap! But there is still one thing I am wondering about:

" of our time. ")) ;; the space after ..time. seems to be necessary

To make the wrap-line function grab all of the last bit of text I have to add a space after the last word. So I am starting to get a handle on Clojure, but I still have a lot more to learn.

Looking ahead to next week, we will be tackling larger concepts, and some of them are starting to give me headaches.

Update:

I just spent some time on http://regexr.com/ and made sense of the RegEx used in the warp-line function:

".{0," size "}\\s"

So, to break it down, “.” matches any single character, “{0,” size “}”tells RegEx to match 0 to size more. Therefore, since “.” matches any character, “.{0,” size “}” tells RegEx to match 0 or more characters, upto a length of size. Finally, “\s” matches a space, and Clojure regards \ as a special character, so it becomes “\\s”. The whole expression therefore matches the first space after o to size characters. This also explains why I found that I have to add a trailing space at the end of the string. Without that trailing space RegEx is matching to the space before the last word.

Like what you read? Give Chris Tully a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.