#clojure logs

2016-05-04

01:50mfittonHi, 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
01:50dysfunokay. what's your question?
01:50mfittonI 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
01:50dysfun:)
01:51mfittonHow can I build up a list of map from a key for the question, to the user's input as a value
01:51dysfuna list of map? not just a map?
01:51mfittonHmm, so length 1 maps, and then use cons to put them together into a list?
01:51dysfunno
01:52mfittonoh
01:52mfittonsorry I misspoke, just a map
01:52dysfunhang on, let me put together a sample
01:54mfittonhttps://codeshare.io/qkS9H
01:54mfittonHere's an example of what I'm looking for :)
01:54dysfun(defn ask-questions [qs] (reduce #(assoc % (ask-question %2)) {} qs))
01:55dysfunthat presumes you have a function ask-question which outputs the question and reads a line and returns it
01:56dysfunoh there should be a %2 right after the %
01:56mfittonAhh, 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
01:56dysfunassuming it is just a string question
01:56tolstoyIn general, though, you use a recursive loop/function and just keep passing a new version of the map on each update.
01:56dysfunthe idea is simple - you incrementally build up the map
01:57dysfunobviously clojure has many ways to build maps, but this is a nice simple one
01:59TEttingeramong one of those many ways, and one that may prove handy for other kinds of map-makingL
01:59TEttinger:
01:59TEttinger,(into {} [[:a 1] [:b 22] [:c 33]])
01:59clojurebot{:a 1, :b 22, :c 33}
01:59TEttingerany 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
02:02mfittonThat's really neat, thanks for all your assistance!
02:04TEttingeroh, and the sequence of vectors thing can be more common than it seems at first!
02:04TEttinger,(seq {:d 4 :e 55 :f 66})
02:04clojurebot([:d 4] [:e 55] [:f 66])
02:05TEttingermaps 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
02:06TEttinger,(seq "abcd")
02:06clojurebot(\a \b \c \d)
02:06TEttingersimilarly strings can be used as sequences of characters
02:06TEttingerlots of handy defaults like this
02:08mfittonI'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
02:08TEttingerit's used internally by most clojure fns that deal with collection stuff
02:08TEttinger,(map int "ABC")
02:08clojurebot(65 66 67)
02:09TEttingerint doesn't do anything on a String, but on a character it gets the codepoint
02:09TEttinger,(map first {:d 4 :e 55 :f 66})
02:09clojurebot(:d :e :f)
02:11TEttingerthat 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])
02:11TEttingerso getting the first of [:d 4] gives :d, etc.
02:24mfittonHmm, for some reason when I start the lein repl nothing in my project's core namespace is being loaded
02:24dysfunno. you have to load it yourself
02:24dysfun(require '[my.core :as c])
02:25dysfunyou can also use the tab key to fill completions from c/
02:25dysfunif you need to reload it, add :reload after the vector
02:26dysfun(i normally put it there anyway because i'm bound to want to reload)
02:26dysfun(then i just find it in the history)
02:26mfittonAhh, that makes sense
02:27mfittonIf I get, "no such namespace c" after (require `[`my-project.core :as c])
02:28mfittonsorry, without the ` in front of my-project.core
02:28mfittonahh it's a quote not a backtick; struggles.
02:32mfittonIf 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.
02:34dysfunit means it's in that module
02:34dysfunanything you evaluate at the repl happens in that namespace
02:34dysfunso that depends whether that module was loaded or you just (in-ns 'my-core)
02:35mfittonHmm, so if I can't access any of the bindings in that namespace... I'll try reloading I guess
02:35dysfunusually works :)
03:48fikgol(defn test []
03:48fikgol (map #(print %) '(1 2 3))
03:48fikgol ()
03:48fikgol )
03:48fikgol
03:48fikgol Hi, why this code do't print "123" when I add () at end ?
03:50TEttingerfikgol: 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
03:50TEttingeror rather the result of the map call
03:51TEttinger,(defn test [] (dorun (map #(print %) '(1 2 3))) ())
03:51clojurebot#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...
03:51TEttinger,(defn test1 [] (dorun (map #(print %) '(1 2 3))) ())
03:51clojurebot#'sandbox/test1
03:52TEttinger,(test1)
03:52clojurebot123()
03:52TEttingerthe last bit is what it returns, the empty list
03:52TEttingerdorun is specifically meant for when lazyseqs produce side effects like printing or writing to a database
03:52TEttingerhttp://clojuredocs.org/clojure.core/dorun
03:55fikgolTEttinger: oh, I see, thanks so much
03:56TEttingerglad to help! there's a bunch of other dosomething style macros
03:56TEttingerdoseq may be better in this case
03:57TEttingerhttp://clojuredocs.org/clojure.core/doseq#example-5542a62be4b01bb732af0a8e is the simple example
03:57TEttingerdocs are above
04:08fikgolTEttinger: Awesome
05:59KeksikeDoes 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?