programming-beginners

daveliepmann 2018-03-09T06:57:01.000133Z

Which version of the curriculum are you looking at? What do you like about it?

2018-03-09T14:35:40.000503Z

New question. Why does this:

(def names ["Amelia Cook" "Natalie Bell"])

(defn split [x] (clojure.string/split x #" "))

(split names)
Return this ["[\"Amelia" "Cook\"" "\"Natalie" "Bell\"]"]?

2018-03-09T14:36:36.000641Z

What I want is obviously "Amelia" "Cook" "Natalie" "Bell" but I can’t figure out why it’s doing this so I can try to understand how to fix it.

sundarj 2018-03-09T14:37:34.000133Z

=> (doc clojure.string/split)
-------------------------
clojure.string/split
([s re] [s re limit])
  Splits string on a regular expression.  Optional argument limit is
  the maximum number of splits. Not lazy. Returns vector of the splits.
nil
split expects a string, so it coerces whatever you pass it into a string

sundarj 2018-03-09T14:38:05.000032Z

=> (str ["a" "b" "c"])
"[\"a\" \"b\" \"c\"]"

2018-03-09T14:38:22.000297Z

So when I define the names I shouldn’t put it into a vector?

bronsa 2018-03-09T14:38:25.000141Z

@amelia that’s not valid clojure, are you running clojurescript?

bronsa 2018-03-09T14:38:53.000503Z

right ok

bronsa 2018-03-09T14:39:04.000482Z

if you try that in a clojure repl you’ll get an error

bronsa 2018-03-09T14:39:08.000190Z

saying that a vector is not a string

sundarj 2018-03-09T14:39:36.000664Z

it's fine to put them into a vector, but you cannot call split on that vector

sundarj 2018-03-09T14:40:27.000265Z

since you want to apply it to the strings inside the vector, use split with map: (map split names)

2018-03-09T14:43:53.000659Z

Thank you! I’ll give it a try

2018-03-09T14:44:38.000656Z

It worked! 😄 Thank you!

sundarj 2018-03-09T14:45:04.000719Z

sweet 🙂

sundarj 2018-03-09T14:47:23.000140Z

@amelia note that if you want to have all the splitted strings returned in a single collection, you can replace the map with mapcat:

=> (map (fn [s] (clojure.string/split s #" ")) ["a b" "c d"])
(["a" "b"] ["c" "d"])
=> (mapcat (fn [s] (clojure.string/split s #" ")) ["a b" "c d"])
("a" "b" "c" "d")

2018-03-09T14:47:40.000200Z

Haha, I was just looking for something like that - thanks again!

1
2018-03-09T16:26:24.000755Z

Why does everyone keep using foo in examples?

bronsa 2018-03-09T16:32:45.000600Z

https://en.wikipedia.org/wiki/Foobar

bronsa 2018-03-09T16:33:33.000204Z

when people want to express abstract concepts in code (such as in examples) the name of variables don’t matter as they don’t mean anything

bronsa 2018-03-09T16:33:42.000653Z

so we usually use “foo” “bar” “baz” “fred” and others

bronsa 2018-03-09T16:33:56.000555Z

to mean “this is a variable that we don’t really care what its name should be”

sundarj 2018-03-09T16:34:16.000036Z

"qux" is another one

sundarj 2018-03-09T16:36:17.000602Z

https://en.wikipedia.org/wiki/Metasyntactic_variable

2018-03-09T16:42:28.000547Z

Thank you!

2018-03-09T16:42:47.000088Z

I am really glad this channel exists, no way I could ask this kind of thing in #beginners

5