#clojure log - May 08 2016

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

0:16 user_: Got it

0:16 I was missing the jdk

0:21 tolstoy: Oh, you just had the JRE?

0:31 user_: tolstoy, exactly

0:32 tolstoy: Heh. For a moment, I was wondering how you'd get a java.lang error with no JVM. ;)

1:15 etiago: hi everyone, any clojure gurus around? :) need a quick input on functional programming

1:15 jeaye: etiago: Don't ask to ask; just ask.

1:19 etiago: fair 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

1:20 is 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

1:20 tolstoy: etiago: I'd say yes. But move that map to a (def table ...) out of the function. ;)

1:21 etiago: tolstoy: I thought about it, but isn't that creating a global variable and aren't those evil?

1:21 tolstoy: Global mutable are, but it might as well be a constant, so it should be fine.

1:22 etiago: tolstoy: 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

1:22 tolstoy: I suppose if that code were in a module or component, you'd want to pass in that configuration, but the principle is the same.

1:22 Declarative data. If it's in a config.edn, and read in at start up, or something: same basic goodness.

1:23 etiago: hmm good idea actually

1:23 thanks ;)

1:24 tolstoy: (slurp (clojure.edn/read-string (clojure.java.io/file "config.edn"))) or something, in -main, and passed in. ;)

1:24 etiago: yep, that will be my next step :D

1:26 tolstoy: Yeah, 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.

1:27 etiago: hehe 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

1:27 but FP is seriously blowing my mind as I go..

1:28 and it all started with emacs and elisp, then I got hooked :p

1:30 tolstoy: It's kinda hard to tell the difference between a Clojure source file, and a configuration file, sometimes.

1:30 Works 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.

1:31 etiago: well 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

1:31 tolstoy: Most people just use a regular map.

1:32 {:port 5000 :db-spec {:user "scott" :pass "tiger"}}

1:32 You can (slurp "config.edn") to read it in as a string.

1:32 etiago: and then evaluate the string?

1:33 tolstoy: Then (clojure.edn/read-string "{:port 5000}") to get the map back out.

1:33 edn/read-string is the "safe" eval in that it won't execute functions.

1:33 etiago: so .edn files are basically clojure data files... syntax is the same except no functions are stored

1:34 tolstoy: (->> "config.edn" io/file edn/read-string (schema/validate schema))

1:34 Yeah. I use ".edn" to signal that it's meant to be data, but you can do the same thing with any clj file.

1:35 etiago: cool stuff :)

1:35 tolstoy: slurp in the file, then run repeated reads over it until you get to eof, or something like that.

1:36 (map (edn/read %) (PushbackReader. (io/reader (io/file "data.clj"))))

1:36 Something like that.

1:37 etiago: I'll give that a try in a sec

1:37 tolstoy: You can read out using line-seq as well.

10:39 justin_smith: tolstoy: map would give you each character of a PushbackReader, but this works https://gist.github.com/noisesmith/c2b44cd1282fa46453fcc61577589809

10:40 err, map actually tells you it can't make a seq from a PushbackReader, oops

12:09 tolstoy: justin_smith: Yeah, I was kinda paraphrasing. My version of that used loop/recur.

16:55 je_: I have a list/vector of elements based on a condition I want to combine some of the elements in the list/vector

16:56 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:58 I'm thinking that I could use concat instead of conj to construct my list of combined elements...

16:58 but that seems wrong :-/

16:59 any better way of handling both lists and vectors?

17:13 Glenjamin: why is it sometimes a list and sometimes a vector?

17:35 justin_smith: the easiest solution is to use seq or vec inside your function, to ensure you are always using the same conj behavior

Logging service provided by n01se.net