2016-05-08
| 00:16 | user_ | Got it |
| 00:16 | user_ | I was missing the jdk |
| 00:21 | tolstoy | Oh, you just had the JRE? |
| 00:31 | user_ | tolstoy, exactly |
| 00:32 | tolstoy | Heh. For a moment, I was wondering how you'd get a java.lang error with no JVM. ;) |
| 01:15 | etiago | hi everyone, any clojure gurus around? :) need a quick input on functional programming |
| 01:15 | jeaye | etiago: Don't ask to ask; just ask. |
| 01: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 |
| 01:20 | etiago | 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 |
| 01:20 | tolstoy | etiago: I'd say yes. But move that map to a (def table ...) out of the function. ;) |
| 01:21 | etiago | tolstoy: I thought about it, but isn't that creating a global variable and aren't those evil? |
| 01:21 | tolstoy | Global mutable are, but it might as well be a constant, so it should be fine. |
| 01: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 |
| 01: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. |
| 01:22 | tolstoy | Declarative data. If it's in a config.edn, and read in at start up, or something: same basic goodness. |
| 01:23 | etiago | hmm good idea actually |
| 01:23 | etiago | thanks ;) |
| 01:24 | tolstoy | (slurp (clojure.edn/read-string (clojure.java.io/file "config.edn"))) or something, in -main, and passed in. ;) |
| 01:24 | etiago | yep, that will be my next step :D |
| 01: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. |
| 01: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 |
| 01:27 | etiago | but FP is seriously blowing my mind as I go.. |
| 01:28 | etiago | and it all started with emacs and elisp, then I got hooked :p |
| 01:30 | tolstoy | It's kinda hard to tell the difference between a Clojure source file, and a configuration file, sometimes. |
| 01:30 | tolstoy | 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. |
| 01: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 |
| 01:31 | tolstoy | Most people just use a regular map. |
| 01:32 | tolstoy | {:port 5000 :db-spec {:user "scott" :pass "tiger"}} |
| 01:32 | tolstoy | You can (slurp "config.edn") to read it in as a string. |
| 01:32 | etiago | and then evaluate the string? |
| 01:33 | tolstoy | Then (clojure.edn/read-string "{:port 5000}") to get the map back out. |
| 01:33 | tolstoy | edn/read-string is the "safe" eval in that it won't execute functions. |
| 01:33 | etiago | so .edn files are basically clojure data files... syntax is the same except no functions are stored |
| 01:34 | tolstoy | (->> "config.edn" io/file edn/read-string (schema/validate schema)) |
| 01:34 | tolstoy | Yeah. I use ".edn" to signal that it's meant to be data, but you can do the same thing with any clj file. |
| 01:35 | etiago | cool stuff :) |
| 01:35 | tolstoy | slurp in the file, then run repeated reads over it until you get to eof, or something like that. |
| 01:36 | tolstoy | (map (edn/read %) (PushbackReader. (io/reader (io/file "data.clj")))) |
| 01:36 | tolstoy | Something like that. |
| 01:37 | etiago | I'll give that a try in a sec |
| 01: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 | justin_smith | 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 | je_ | 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 | je_ | I'm thinking that I could use concat instead of conj to construct my list of combined elements... |
| 16:58 | je_ | but that seems wrong :-/ |
| 16:59 | je_ | 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 |