#clojure log - Aug 01 2015

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

0:40 slester: Is there an idiomatic way to search a vector of maps for a condition and return the matching indices?

0:40 [{:thing true} {:thing false} {:thing false}], looking for {:thing false}, I want to return [1 2]

0:40 justin_smith: slester: it's easier to return the matching items, but with the help of map and range it's pretty easy

0:41 slester: yeah the matching items I have

0:41 it's the indices that are tripping me up

0:41 also I really appreciate you always answering justin_smith !

0:42 justin_smith: ,(keep (fn [i m] (if (false? (:thing map)) i)) [{:thing true} {:thing false} {:thing false}])

0:42 clojurebot: #<ArityException clojure.lang.ArityException: Wrong number of args (1) passed to: sandbox/eval25/fn--26>

0:42 justin_smith: err

0:42 ,(keep (fn [i m] (if (false? (:thing m)) i)) range [{:thing true} {:thing false} {:thing false}])

0:42 clojurebot: #error {\n :cause "Wrong number of args (3) passed to: core/keep"\n :via\n [{:type clojure.lang.ArityException\n :message "Wrong number of args (3) passed to: core/keep"\n :at [clojure.lang.AFn throwArity "AFn.java" 429]}]\n :trace\n [[clojure.lang.AFn throwArity "AFn.java" 429]\n [clojure.lang.AFn invoke "AFn.java" 40]\n [sandbox$eval51 invoke "NO_SOURCE_FILE" 0]\n [clojure.lang.Compiler e...

0:42 justin_smith: ,(keep identity (map (fn [i m] (if (false? (:thing m)) i)) range [{:thing true} {:thing false} {:thing false}]))

0:42 clojurebot: #<IllegalArgumentException java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.core$range>

0:43 justin_smith: ,(keep identity (map (fn [i m] (if (false? (:thing m)) i)) (range) [{:thing true} {:thing false} {:thing false}])) ; I must be tired

0:43 clojurebot: (1 2)

0:43 justin_smith: or...

0:43 ,(mapcat (fn [i m] (if (false? (:thing m)) [i])) (range) [{:thing true} {:thing false} {:thing false}])

0:43 clojurebot: (1 2)

0:44 slester: justin_smith, mapcat, nice, I've never used or seen that before. Very useful

0:50 justin_smith: slester: at first I didn't realize mapcat wasn't just for putting more than one item into a coll per step - it can also be for putting less than one item into a coll

1:31 TEttinger: woah, I hadn't seen that either justin_smith.

1:31 nice

1:31 (inc mapcat)

1:31 lazybot: ⇒ 1

1:31 TEttinger: (inc justin_smith)

1:31 lazybot: ⇒ 282

1:31 TEttinger: wowza

1:31 (identity amalloy_)

1:31 lazybot: amalloy_ has karma 2.

1:31 TEttinger: (identity amalloy)

1:31 lazybot: amalloy has karma 288.

1:32 TEttinger: oh boy, neck and neck

2:07 J_Arcane: Anyone ever try doing the Little Lisper in Clojure?

3:31 wasamasa: s/lisper/schemer/

4:11 J_Arcane: Heh. True.

7:06 Olajyd: What function can I use to check if a given number exist in a vector of numbers?

7:08 oddcully: ,(some #{5} [1 2 5 4])

7:08 clojurebot: 5

7:10 Olajyd: Thanks Oddcully

7:48 How do i check fro the length of a number of type double : (count (str 10.0000))?

7:49 Check for length of type double: (count (str 10.0000))

7:53 oddcully: ,(count (str (- 0.3 0.2)))

7:53 clojurebot: 19

8:03 kwladyka: is it possible to change hashcode in Clojure? I have Sets. And i am adding there maps {:notation [...] :safe-squares [...]}, but there is no reason to check :safe-squares, i only care about :notation, so i want change hashcode of this map to hashcode of :notation

8:04 justin_smith: kwladyka: you could make :safe-squares a metadata

8:04 that doesn't effect the hashcode

8:04 kwladyka: justin_smith, thx i will read about that

8:05 justin_smith, how good you are always here :)

8:05 justin_smith: ,(def a (with-meta {:notation [:a]} {:safe-squares [1 2]})

8:05 clojurebot: #<RuntimeException java.lang.RuntimeException: EOF while reading>

8:05 justin_smith: ,(def a (with-meta {:notation [:a]} {:safe-squares [1 2]}))

8:05 clojurebot: #'sandbox/a

8:05 justin_smith: ,a

8:05 clojurebot: {:notation [:a]}

8:06 justin_smith: (def b (with-meta {:notation [:a]} {:safe-squares [3 4]}))

8:06 ,(def b (with-meta {:notation [:a]} {:safe-squares [3 4]}))

8:06 clojurebot: #'sandbox/b

8:06 justin_smith: ,(= a b)

8:06 clojurebot: true

8:06 justin_smith: ,(map meta [a b])

8:06 clojurebot: ({:safe-squares [1 2]} {:safe-squares [3 4]})

8:06 justin_smith: kwladyka: that should be enough ^

8:07 the metadata is always a hash-map

8:07 kwladyka: justin_smith, thx

8:07 justin_smith: kwladyka: the tricky part though is operations like conj or assoc don't preserve metadata, you need to pass it along explicitly

8:07 ,(meta (assoc a :b 0))

8:07 clojurebot: {:safe-squares [1 2]}

8:08 justin_smith: oh wait

8:08 haha

8:08 so it does propagate, cool...

8:09 kwladyka: good news, i _think_ i found a bug in my algorithm, it is heuristic bug, but we will see

8:09 justin_smith, How long do you work with Clojure?

8:12 Olajyd: Given a vector of vectors [[a b] [b c] [c d]], I want to implement a filter function to remove the second index (i.e index 1).. How do I go about this?

8:16 Given a vector of vectors [[a b] [b c] [c d]], I want to implement a filter function to remove the vector in the second index (i.e index 1)..the filter will return a result like this [ [a b] [c d] ] How do I go about this?

8:17 pleax: Olajyd: generally you can't do this with filter

8:18 Olajyd: but if you know which index to remove you can try to concat two subvec's

8:18 Olajyd: pleax gotcha

8:21 pleax: what if the index is known?.. Can i implement a filter function with the subvec or how do I go about it

8:25 pleax: Olajyd: simpliest way it something like (concat (subvec v 0 1) (subvec v 2))

8:25 expez: While reading the source for tools.analyzer.jvm I saw (binding [*ns* *ns*] ...) why isn't that a no-op?

8:26 Olajyd: @pleax : Thanks

8:26 expez: Is it there to reset and (binding ..) above it on the stack?

8:26 s/and/any/

9:37 gfredericks: expez: if I understand you correctly, set! does that

10:28 dnolen: gfredericks: do you have link where I can try your build?

10:31 gfredericks: dnolen: https://github.com/gfredericks/exact/commit/d0865ee25a0d9879eb256e4afa6f2d5431a2ec66

10:31 it's on a branch there called fork-goog-integer, if that's easier

10:32 (that link was in the original tweet, I wasn't expecting you to diagnose something pureely based off of "Assert failed")

10:35 dnolen: gfredericks: I just tried transit-cljs, seems to work fine, will take a look at your thing now

10:40 gfredericks: cool, thanks

10:56 dnolen: gfredericks: ah, I see the problem - there is a bug with the feature the .js lib has to be in a JAR

10:56 which is why it worked w/ transit-cljs, but not your example

10:57 gfredericks: I thought of that distinction but didn't seem plausible enough to try

10:57 I can work with that though, thanks!

10:57 (inc dnolen)

10:57 lazybot: ⇒ 24

10:58 dnolen: gfredericks: http://dev.clojure.org/jira/browse/CLJS-1387

11:00 gfredericks: cool

11:10 tmtwd: is -> and --> like function composition in haskell?

11:10 gfredericks: somewhat

11:11 tmtwd: just somewhat?

11:11 they seem very similar

11:11 scriptor: comp is like function composition in haskell

11:11 tmtwd: Now that I think about it, I thik I brought it up at a clojure meetup, but was informed that its not

11:12 scriptor: -> and --> go further and expect the argument to be feedable at a specific point

11:12 tmtwd: what does that mean?

11:13 gfredericks: they're macros so they work with expressions instead of functions

11:17 tmtwd: oh

11:17 but it is a similar idea at least?

11:17 gfredericks: similar use cases

11:17 ,(macroexpand-1 '(-> x (foo 1) (bar 2 3 4) (baz)))

11:17 clojurebot: (baz (bar (foo x 1) 2 3 4))

11:17 tmtwd: I can pretend they are like function composition, knowing that they are not? :)

11:17 gfredericks: ^ macroexpand-1 is good for seeing what it does

11:18 tmtwd: ah

11:18 good tip

11:18 gfredericks: compare to ->>:

11:18 ,(macroexpand-1 '(->> x (foo 1) (bar 2 3 4) (baz)))

11:18 clojurebot: (baz (bar 2 3 4 (foo 1 x)))

11:32 dnolen: tmtwd: I would avoid pretending, nasty surprises lurk

11:33 gfredericks: dnolen: throwing it in a jar works perfectly, thanks again!

11:33 dnolen: gfredericks: great!

11:50 gfredericks: issue fixed in master

12:27 justin_smith: tmtwd: I guess we could say they are about form convolution, rather than function composition

12:28 ,(-> (java.util.HashMap.) (.put :a 0) (.put :b 1)) ; contains things that are not functions

12:28 clojurebot: #error {\n :cause nil\n :via\n [{:type java.lang.NullPointerException\n :message nil\n :at [clojure.lang.Reflector invokeInstanceMethod "Reflector.java" 26]}]\n :trace\n [[clojure.lang.Reflector invokeInstanceMethod "Reflector.java" 26]\n [sandbox$eval25 invoke "NO_SOURCE_FILE" 0]\n [clojure.lang.Compiler eval "Compiler.java" 6850]\n [clojure.lang.Compiler eval "Compiler.java" 6813]\n [clo...

12:28 justin_smith: err...

12:28 haha, put does not return the map

12:29 ,(let [m (java.util.HashMap.)] (-> m (.put :a 0) (.put :b 1)) m) ; contains things that are not functions

12:29 clojurebot: #error {\n :cause nil\n :via\n [{:type java.lang.NullPointerException\n :message nil\n :at [clojure.lang.Reflector invokeInstanceMethod "Reflector.java" 26]}]\n :trace\n [[clojure.lang.Reflector invokeInstanceMethod "Reflector.java" 26]\n [sandbox$eval49 invoke "NO_SOURCE_FILE" 0]\n [clojure.lang.Compiler eval "Compiler.java" 6850]\n [clojure.lang.Compiler eval "Compiler.java" 6813]\n [clo...

12:29 justin_smith: ,(let [m (java.util.HashMap.)] (-> m (.put :a 0)) m)

12:29 clojurebot: {:a 0}

12:33 oddcully: ,(doto (java.util.HashMap.) (.put :a 0) (.put :b 1))

12:33 clojurebot: {:b 1, :a 0}

12:46 justin_smith: oddcully: but that doesn't compose forms of course

12:46 well, actually

12:46 ,(macroexpand-1 '(doto a b c))

12:46 clojurebot: (clojure.core/let [G__27 a] (b G__27) (c G__27) G__27)

12:47 oddcully: i haven't payed attention to the discussion above. i just saw the last bit about "put not returning"

12:47 just ignore me ;)

13:16 arav93: How do we create a node type of thingy in clojure.zip

17:35 tmtwd: does cljs need an 'init' or main method to run?

17:35 I don't think javascript needs one but maybe cljs does

17:47 justin_smith: tmtwd: what I like to do is define a main method and then call it in a script tag - that way I can run tests without invoking my main method

17:47 tmtwd: yes

17:47 i see

17:47 justin_smith: so I have a test.html that instead of ivoking the main, loads my test namespaces and invokes run-all-tests

17:47 tmtwd: it works like javascript mostly

17:47 justin_smith: right, it outputs javascript :)

20:17 justmytwospence: I'm having some trouble connecting an org-babel buffer to CIDER

20:18 CIDER is running, but when I try to execute source code blocks, it says "not connected"

20:19 most of the documentation for using org-babel with clojure is outdated

20:27 arrdem: justmytwospence: do you have cider-mode on in that buffer?

20:27 justmytwospence: that's how CIDER chooses which connection (if any) to eval to

20:28 justmytwospence: if you get this working please please write it up :P

20:28 the docs as you say are quite stale

20:47 justmytwospence: arrdem: i executed cider-jack-in from the org buffer, so it should be connected, yes?

20:50 justin_smith: justmytwospence: no it does not work that way

20:51 justmytwospence: justin_smith: how should I do it?

20:51 justin_smith: justmytwospence: I have no idea, but cider doesn't turn on cider mode in a buffer when you run cider-jack-in from that buffer

20:53 justmytwospence: justin_smith: if i explicitly enable cider mode, it overrides the org-babel C-C C-c keybinding

20:54 justin_smith: justmytwospence: I don't even know if using org-babel with new cider versions is possible, sorry. Wish I could offer more help.

22:12 arrdem: justmytwospence: not the org buffer itself, the bable output buffer

23:38 timvisher: is there a safe way to parse a string to an int, just returning nil if it couldn't be done?

23:39 ,(try (Integer/parseInt "1") (catch Exception e))

23:39 clojurebot: timvisher: Pardon?

23:39 timvisher: ,(try (Integer/parseInt "1") (catch Exception e nil))

23:39 clojurebot: timvisher: Excuse me?

23:39 timvisher: hrm...

23:39 justin_smith: timvisher: clojurebot doesn't do try/catch

23:39 timvisher: oh lol

23:40 anywho, that's how i would write it, but i'm wondering if that already exists in the stdlib somewhere

23:40 maybe in numeric tower somewhere?

23:43 ah looks like it's in useful https://github.com/amalloy/useful/blob/1c436605f31fedca916f0e3c172b0b0c06f621d0/src/flatland/useful/datatypes.clj#L10-L15

23:44 hrm, nope. that still doesn't catch exceptions

Logging service provided by n01se.net