#clojure log - May 04 2016

The Joy of Clojure
Main Clojure site
Google Group
IRC
List of all logged dates

1:50 mfitton: Hi, I have a question about getting a bunch of user input in Clojure. In essence, I want to collect 8 pieces of input from a user over the command line, then use those 8 pieces of input in a query to an online API

1:50 dysfun: okay. what's your question?

1:50 mfitton: I have everything except for the ability to build up, say, a hashmap of user responses. I can't figure out how to do this since hashmaps are immutable

1:50 dysfun: :)

1:51 mfitton: How can I build up a list of map from a key for the question, to the user's input as a value

1:51 dysfun: a list of map? not just a map?

1:51 mfitton: Hmm, so length 1 maps, and then use cons to put them together into a list?

1:51 dysfun: no

1:52 mfitton: oh

1:52 sorry I misspoke, just a map

1:52 dysfun: hang on, let me put together a sample

1:54 mfitton: https://codeshare.io/qkS9H

1:54 Here's an example of what I'm looking for :)

1:54 dysfun: (defn ask-questions [qs] (reduce #(assoc % (ask-question %2)) {} qs))

1:55 that presumes you have a function ask-question which outputs the question and reads a line and returns it

1:56 oh there should be a %2 right after the %

1:56 mfitton: Ahh, I think I can see how that works, thanks so much for your help. I'll play around in the repl til I'm sure I understand it

1:56 dysfun: assuming it is just a string question

1:56 tolstoy: In general, though, you use a recursive loop/function and just keep passing a new version of the map on each update.

1:56 dysfun: the idea is simple - you incrementally build up the map

1:57 obviously clojure has many ways to build maps, but this is a nice simple one

1:59 TEttinger: among one of those many ways, and one that may prove handy for other kinds of map-makingL

1:59 :

1:59 ,(into {} [[:a 1] [:b 22] [:c 33]])

1:59 clojurebot: {:a 1, :b 22, :c 33}

1:59 TEttinger: any time you have a sequence or vector or whatever of two-element vectors, those two elements can be turned into a key and value by certain fns like into

2:02 mfitton: That's really neat, thanks for all your assistance!

2:04 TEttinger: oh, and the sequence of vectors thing can be more common than it seems at first!

2:04 ,(seq {:d 4 :e 55 :f 66})

2:04 clojurebot: ([:d 4] [:e 55] [:f 66])

2:05 TEttinger: maps can be turned into seqs and used as sequences of pairs, essentially, which is handy because map and other fns like reduce call seq on some arguments normally

2:06 ,(seq "abcd")

2:06 clojurebot: (\a \b \c \d)

2:06 TEttinger: similarly strings can be used as sequences of characters

2:06 lots of handy defaults like this

2:08 mfitton: I'll keep the seq function in mind. When I was trying to figure out how to do this, my initial thought was actually to break the map into pairs, map the pairs into new pairs, then turn it back into a map, but I didn't know about seq, haha

2:08 TEttinger: it's used internally by most clojure fns that deal with collection stuff

2:08 ,(map int "ABC")

2:08 clojurebot: (65 66 67)

2:09 TEttinger: int doesn't do anything on a String, but on a character it gets the codepoint

2:09 ,(map first {:d 4 :e 55 :f 66})

2:09 clojurebot: (:d :e :f)

2:11 TEttinger: that last example works because the map literal I passed, the one with curly braces, gets processed with seq before anything else is done, and remember seq returned ([:d 4] [:e 55] [:f 66])

2:11 so getting the first of [:d 4] gives :d, etc.

2:24 mfitton: Hmm, for some reason when I start the lein repl nothing in my project's core namespace is being loaded

2:24 dysfun: no. you have to load it yourself

2:24 (require '[my.core :as c])

2:25 you can also use the tab key to fill completions from c/

2:25 if you need to reload it, add :reload after the vector

2:26 (i normally put it there anyway because i'm bound to want to reload)

2:26 (then i just find it in the history)

2:26 mfitton: Ahh, that makes sense

2:27 If I get, "no such namespace c" after (require `[`my-project.core :as c])

2:28 sorry, without the ` in front of my-project.core

2:28 ahh it's a quote not a backtick; struggles.

2:32 If my prompt says "my.core=>" I imagine that means I have it loaded, but I still can't access anything I defined in the NS.

2:34 dysfun: it means it's in that module

2:34 anything you evaluate at the repl happens in that namespace

2:34 so that depends whether that module was loaded or you just (in-ns 'my-core)

2:35 mfitton: Hmm, so if I can't access any of the bindings in that namespace... I'll try reloading I guess

2:35 dysfun: usually works :)

3:48 fikgol: (defn test []

3:48 (map #(print %) '(1 2 3))

3:48 ()

3:48 )

3:48

3:48 Hi, why this code do't print "123" when I add () at end ?

3:50 TEttinger: fikgol: the last thing in a fn is what it returns. map returns a lazy seq, and won't actually try to produce results until its result is evaluated. because that returns an empty list when () is the last part of the fn, the map never evaluates

3:50 or rather the result of the map call

3:51 ,(defn test [] (dorun (map #(print %) '(1 2 3))) ())

3:51 clojurebot: #error {\n :cause "denied"\n :via\n [{:type clojure.lang.Compiler$CompilerException\n :message "java.lang.SecurityException: denied, compiling:(NO_SOURCE_PATH:0:0)"\n :at [clojure.lang.Compiler analyzeSeq "Compiler.java" 6875]}\n {:type java.lang.SecurityException\n :message "denied"\n :at [clojurebot.sandbox$enable_security_manager$fn__835 invoke "sandbox.clj" 69]}]\n :trace\n [[clojureb...

3:51 TEttinger: ,(defn test1 [] (dorun (map #(print %) '(1 2 3))) ())

3:51 clojurebot: #'sandbox/test1

3:52 TEttinger: ,(test1)

3:52 clojurebot: 123()

3:52 TEttinger: the last bit is what it returns, the empty list

3:52 dorun is specifically meant for when lazyseqs produce side effects like printing or writing to a database

3:52 http://clojuredocs.org/clojure.core/dorun

3:55 fikgol: TEttinger: oh, I see, thanks so much

3:56 TEttinger: glad to help! there's a bunch of other dosomething style macros

3:56 doseq may be better in this case

3:57 http://clojuredocs.org/clojure.core/doseq#example-5542a62be4b01bb732af0a8e is the simple example

3:57 docs are above

4:08 fikgol: TEttinger: Awesome

5:59 Keksike: Does anyone have experience or knowledge of updating Monger from 2.0 to 3.x? Something in our program breaks but I'm not quite sure what. Possibly bulk-searches?

Logging service provided by n01se.net