Fork me on GitHub
#clojure
<
2015-09-25
>
mikecarter00:09:40

i'm trying to process some XML data and I've used clojure.xml to get something like this - https://gist.github.com/mikos/dea83b5c507eccc93e80 - can anyone point me in the right direction to convert that to a more traditional clojure map structure?

bhagany01:09:26

@cfleming: there's run!, new in 1.7

bhagany01:09:03

Annoying there's no backtick on the iOS keyboard in slack, unless I'm just missing it

meow03:09:23

@bhagany: I like Stuart's Clojure Don'ts. The times I typically find myself forcing the activation of lazy sequences is with Criterium benchmarking where you want to make sure you're actually timing some activity. I've had a few momentary dramatic performance improvements because I forgot what I was testing was lazy and it wasn't really measuring any activity. simple_smile

meow03:09:05

I keep forgetting about run! and haven't used it yet. There isn't much on ClojureDocs. Is (run! proc coll) pretty much the same as (dorun (map f coll))?

meow03:09:46

I guess the source speaks for itself:

(defn run!
  "Runs the supplied procedure (via reduce), for purposes of side
  effects, on successive items in the collection. Returns nil"
  {:added "1.7"}
  [proc coll]
  (reduce #(proc %2) nil coll))

cfleming03:09:55

@bhagany: That’s exactly what I was after, thanks!

meow03:09:31

Kind of funny that here's a new "feature" of version 1.7 of Clojure and you go look at it only to discover that it comes down to (reduce #(proc %2) nil coll)

meow03:09:13

For me it's disappointing and amazing and refreshing and encouraging all at the same time.

meow04:09:44

Hey! I just learned about empty. Wish I knew about it yesterday. Now I need to change more code that should have used empty in the first place. Hmm...

cfleming04:09:11

I’m trying to use humane-test-output for clojure.test, and I’m not seeing any diff output.

cfleming04:09:42

Does anyone have any ideas as to why that might happen?

mbertheau06:09:58

What's a shorter way to say this?

;; original
(into {} (map (fn [[value {:text text}] choices-map]
                [value text]) my-choices-map))
;; progress so far
(into {} (for [[value {:text text}] my-choices-map]
           [value text]))
;; by <@U0517LEP0>
(into {} (map (fn [[value {text :text}]]
                [value text])) my-choices-map)
;; by <@U054D2JUV>
(into {} (map (juxt key (comp :text val)) my-choices-map))

;; by <@U05224H0W>
(reduce-kv #(assoc %1 %2 (:text %3)) {} my-choices-map)

;; by <@U051MTYAB>
(zipmap  (keys my-choices-map)  (map :text (vals my-choices-map)))

danielcompton06:09:09

@cfleming: I remember having weird interactions between human-test-output and Cursive. You’ve set up your profile.clj as well?

sander06:09:59

@mbertheau: not really shorter but (into {} (map (fn [[value {text :text}]] [value text])) my-choices-map) uses transducers and may be slightly quicker or more readable

cfleming06:09:09

@danielcompton: I’m actually not doing this through Cursive, just normal REPL

mbertheau06:09:55

@sander: I like it! Thanks simple_smile

danielcompton06:09:23

I'm assuming you've followed instructions for profile.clj?

luxbock07:09:10

@mbertheau: (into {} (map (juxt key (comp :text val))) my-choices-map) would also work

mbertheau08:09:57

@luxbock: Nice one! Thanks simple_smile

mbertheau08:09:15

True function level programming simple_smile

thheller09:09:49

@mbertheau: (reduce-kv #(assoc %1 %2 (:text %3)) {} my-choices-map)

mikethompson09:09:22

@mbertheau: (zipmap (keys my-choices-map) (map :text (vals my-choices-map)))

martinklepsch10:09:36

Is there a difference between (filter (constantly true) ...) and (filter identity …)?

danielcompton10:09:15

@martinklepsch: there is if you’re filtering a sequence with a nil in it

martinklepsch10:09:43

right, good point.

martinklepsch10:09:04

I was actually more wondering about it in terms of performance

mikecarter11:09:52

what's the best way to turn this into a more traditional map? https://gist.github.com/mikos/dea83b5c507eccc93e80

mikecarter11:09:30

(that's the output from running clojure.xml/parse :))

martinklepsch11:09:07

@mikecarter: do you need the attributes?

leonoel11:09:44

is there a standard function or a canonical way to make a "sliding window" transducer, e.g like when you do (partition-all 4 1 coll)

borkdude11:09:01

@leonoel: "Returns a stateful transducer when no collection is provided"

leonoel11:09:56

@borkdude yes but I need the step argument

leonoel12:09:25

(partition-all 4 1) assumes 1 is a collection

mikecarter12:09:49

@martinklepsch: some attributes, yeah but i'd be happy with a solution without them for now

martinklepsch12:09:11

@mikecarter: unfortunately I don’t have any definitive answer. last time I worked with xml I came to the conclusion that it’s best to familiarize myself with zippers etc. xml is too different from regular maps I’m afraid. maybe others have different experiences to share.

mikecarter12:09:48

that's cool, thanks for taking a look @martinklepsch simple_smile

mikecarter12:09:58

i have a solution here i think, just not sure it's the most efficient!

dexter12:09:30

its not quite the same thing but I found parsing RSS feeds as XML into structmaps using https://github.com/scsibug/feedparser-clj was very nice. I wonder if you could reuse the concepts there.

mikecarter12:09:07

thanks @dexter - i'll take a look at that now simple_smile

borkdude12:09:15

I'm looking at this page from JRebel and the name Clojure support. http://zeroturnaround.com/software/jrebel/ What does that mean? Something like a REPL makes a tool like this obsolete right?

agile_geek12:09:31

@borkdude: I guess it depends on your deployment environment and dependencies? What if your heavily interoperating with Java? Integration with other systems? Test environments for end to end func/non-func testing?

agile_geek12:09:57

In the enterprise world you’re rarely testing in isolation

sashton13:09:22

@mbertheau:

(medley.core/map-vals :text my-choices-map)
dep: [medley "0.7.0"]

borkdude13:09:26

I am trying to eval this code in my repl (pasted from changes.md): #?(:clj Double/NaN :cljs js/NaN :default nil) result I get is: CompilerException java.lang.RuntimeException: Conditional read not allowed, compiling:(/tmp/foo.clj:2:5)

borkdude13:09:56

o wait, .cljc?

gary16:09:41

environment variable

gary16:09:39

in a nix environment 'export LEIN_PASSWORD=chang3me1'

gary16:09:06

on windows windowskey+Pause/Break > Advanced System Settings > Environmental Variables > New...

harshdeep16:09:17

Can someone explain me the use of "let" in Clojure?

harshdeep16:09:30

(let [developer "Alice in Wonderland"]developer)

harshdeep16:09:41

and what does this code actually mean and do?

potetm16:09:19

@harshdeep: It creates a binding for the symbol developer within the scope of the let. In other words, as long as you’re within the let form (i.e. inside the parenthesis surrounding let), developer will resolve to ”Alice in Wonderland”.

potetm16:09:55

I would recommend running that in the repl and playing with it if you want more info.

jstew17:09:12

When I first started clojure, it was helpful to think of let as an analog of a variable assignment inside of a method. It's not useful to think of things from an imperative language mindset long term, but at first it can be a nice crutch.

csmith17:09:47

Good suggestions. I’ll just add that once you’re comfortable with thinking of let as a series of assignments consider how you would accomplish the same in functional programming if let wasn’t there. You’d define a little inner function with something like a developer parameter, and then immediately call it with the string you want. This is what is meant when folks say “Let is a lambda"

csmith17:09:37

If you are familiar with JavaScript, it is the same idea as

(function(developer) {…}(“Alice in Wonderland”));

jeffh-fp17:09:05

people say "Let is a lambda”?

csmith17:09:57

At least the sort that I hang with 😉 (and at least in some lisp implementations, it can be written as such if it is not primitive)

csmith17:09:42

I may’ve not placed my thumb precisely on the lisp programmer zeitgeist, but I’d at least encourage the use of the phrase

borkdude18:09:34

"The simple, non recursive let expression was defined as being syntactic sugar for the lambda abstraction applied to a term" https://en.wikipedia.org/wiki/Let_expression#Laws_relating_lambda_calculus_and_let_expressions

borkdude18:09:32

((fn [x] .... something with x ....) value-for-x) or (let [x value-for-x] ..... something with x ....) are essentially the same thing

shriphani19:09:21

hi, I’ve got a compojure-api question. I am trying to have just one end-point return text/html - I have wrapped it in some simple middleware but it seems adamant to return json. Any ideas ?

juhoteperi19:09:01

@shriphani: Are you returins a string from handlers or something else?

shriphani19:09:21

let me paste my endpoint code

juhoteperi19:09:30

shriphani: Remove :return String, it tells compojure-api to serialize the result

shriphani19:09:53

@juhoteperi: worked perfectly ty. 💯

stathissideris21:09:53

is there any way to do an alts! in core.async and also get information for when all the channels have been closed?

nooga22:09:24

@beppu: you were right about this pulsar + refactor-nrepl issue. I tried to find out what’s wrong but failed. Weird stuff.