#clojure logs

2016-05-08

00:16user_Got it
00:16user_I was missing the jdk
00:21tolstoyOh, you just had the JRE?
00:31user_tolstoy, exactly
00:32tolstoyHeh. For a moment, I was wondering how you'd get a java.lang error with no JVM. ;)
01:15etiagohi everyone, any clojure gurus around? :) need a quick input on functional programming
01:15jeayeetiago: Don't ask to ask; just ask.
01:19etiagofair enough.. the idea is, I get two strings in an HTTP request: location and action. location could be "front" or "back" and action could be "enable", "disable" or "status". then, based on which values are coming in these strings, I want to get a different URL.. so I built a map, like so: http://pastebin.com/8fb22Bxt
01:20etiagois this a Clojure-y way of doing things? I'm already wrapping it in a function that only deals with returning the value from the map based on the arguments
01:20tolstoyetiago: I'd say yes. But move that map to a (def table ...) out of the function. ;)
01:21etiagotolstoy: I thought about it, but isn't that creating a global variable and aren't those evil?
01:21tolstoyGlobal mutable are, but it might as well be a constant, so it should be fine.
01:22etiagotolstoy: alright, fair enough :) I also didn't like having that map in there.. makes it a bit ugly/hard to read, but a global constant it is then
01:22tolstoyI suppose if that code were in a module or component, you'd want to pass in that configuration, but the principle is the same.
01:22tolstoyDeclarative data. If it's in a config.edn, and read in at start up, or something: same basic goodness.
01:23etiagohmm good idea actually
01:23etiagothanks ;)
01:24tolstoy(slurp (clojure.edn/read-string (clojure.java.io/file "config.edn"))) or something, in -main, and passed in. ;)
01:24etiagoyep, that will be my next step :D
01:26tolstoyYeah, I've got a similar process. I have a case statement at first, then I think, that could be a map! and then I think, that could be a configuration! Etc, etc.
01:27etiagohehe yeah and I'm still trying to actually learn Clojure as I go.. but indeed, also in any other language it would make sense to take this in as a configuration
01:27etiagobut FP is seriously blowing my mind as I go..
01:28etiagoand it all started with emacs and elisp, then I got hooked :p
01:30tolstoyIt's kinda hard to tell the difference between a Clojure source file, and a configuration file, sometimes.
01:30tolstoyWorks great for when you need to generate a lot of sample data. Just craft up a map (or similar), read it in as a template and generate.
01:31etiagowell I have no idea what a Clojure config file looks like.. so I'll still have to look into that first but I can imagine
01:31tolstoyMost people just use a regular map.
01:32tolstoy{:port 5000 :db-spec {:user "scott" :pass "tiger"}}
01:32tolstoyYou can (slurp "config.edn") to read it in as a string.
01:32etiagoand then evaluate the string?
01:33tolstoyThen (clojure.edn/read-string "{:port 5000}") to get the map back out.
01:33tolstoyedn/read-string is the "safe" eval in that it won't execute functions.
01:33etiagoso .edn files are basically clojure data files... syntax is the same except no functions are stored
01:34tolstoy(->> "config.edn" io/file edn/read-string (schema/validate schema))
01:34tolstoyYeah. I use ".edn" to signal that it's meant to be data, but you can do the same thing with any clj file.
01:35etiagocool stuff :)
01:35tolstoyslurp in the file, then run repeated reads over it until you get to eof, or something like that.
01:36tolstoy(map (edn/read %) (PushbackReader. (io/reader (io/file "data.clj"))))
01:36tolstoySomething like that.
01:37etiagoI'll give that a try in a sec
01:37tolstoyYou can read out using line-seq as well.
10:39justin_smithtolstoy: map would give you each character of a PushbackReader, but this works https://gist.github.com/noisesmith/c2b44cd1282fa46453fcc61577589809
10:40justin_smitherr, map actually tells you it can't make a seq from a PushbackReader, oops
12:09tolstoyjustin_smith: Yeah, I was kinda paraphrasing. My version of that used loop/recur.
16:55je_I have a list/vector of elements based on a condition I want to combine some of the elements in the list/vector
16:56je_I've been using peek/pop/conj but is now realising that when I give my function a list the results come out in reverse order compared to when using a vector
16:58je_I'm thinking that I could use concat instead of conj to construct my list of combined elements...
16:58je_but that seems wrong :-/
16:59je_any better way of handling both lists and vectors?
17:13Glenjaminwhy is it sometimes a list and sometimes a vector?
17:35justin_smiththe easiest solution is to use seq or vec inside your function, to ensure you are always using the same conj behavior