Fork me on GitHub
#clojure
<
2016-08-06
>
pesterhazy13:08:09

@ag, a pattern I use is (filter #(-> % :id (= needle)) coll)

pesterhazy13:08:37

works nicely, especially if you build up an expr from the repl

Chris O’Donnell13:08:10

@ag: with the specter library (which is really great for working with nested data like that), (select-any [:customers ALL #(= (:id %) id-arg)] cs)

pesterhazy13:08:28

oh yes, I have to get into specter

lvh14:08:29

It might be silly and superficial, but the uppercase symbols really throw me off.

lvh14:08:37

It reminds me of my CL interpreter yelling at me.

akiva15:08:14

@lvh, yeah, I don’t like it either but I can’t put my finger on exactly why.

blueberry15:08:51

what's wrong with (filter (partial = needle) coll)?

blueberry15:08:01

seems the simplest of all those to me...

blueberry15:08:13

ah, sorry, forgot the :id part

blueberry15:08:12

(comp (map :id) (filter (partial = needle)))

blueberry15:08:08

oooor (filter (comp (partial = needle) :id) coll)

achesnais16:08:32

So I’m playing around and trying to implement my own Jupyter Kernel. I see the Clojupyter implementation sets up a repl server to handle all code execution (https://github.com/roryk/clojupyter/blob/master/src/ipython_clojure/core.clj#L260-L270). I’m trying to understand what the the advantage of this is vs just using clojure.core/read-string and clojure.core/eval. I would assume it’s for security reasons, but does an nrepl server actually deliver a sandbox experience?

bwstearns18:08:37

I'm trying to make a custom sorter key for max-key and I am getting a confusing error.

(def foo [{:a 5, :b [1 2 3 4]} {:a 5, :b [1 2 3]} {:a 2, :b [1 2 3 4 5 6 7]}])
(apply max-key (juxt :a (comp count :b)) foo)

bwstearns18:08:51

I'm trying to make a custom sorter key for max-key and I am getting a confusing error.

(def foo [{:a 5, :b [1 2 3 4]} {:a 5, :b [1 2 3]} {:a 2, :b [1 2 3 4 5 6 7]}])
(apply max-key (juxt :a (comp count :b)) foo)
ClassCastException clojure.lang.PersistentVector cannot be cast to java.lang.Number   (Numbers.java:229)

bwstearns18:08:07

ohh. it's not because of the (comp count :b) like I thought, max-key won't sort vectors.

ericnormand20:08:31

I've been hard at work on this Guerrilla Guide to using Clojure at your job: https://purelyfunctional.tv/courses/introducing-clojure-to-the-enterprise/

mjhamrick20:08:36

Is there a better way to initialize a clojure vector of k items all with the same value than

(let [k 10
      initialization-val 0]
  (->> (range k)
       (mapv (constantly initialization-val))))

anmonteiro21:08:29

@mjhamrick: e.g.: (vec (repeat 10 0)) ?

mjhamrick21:08:46

@anmonteiro: That works too. I'm wondering in particular if there's anything better than calling vec on a seq. Thinking about it more, I don't know how there could be a better way to do it.

edbond21:08:58

Why there are 6 results (true) here?

edbond21:08:05

(for [x [f1 f2 f3 f4]]
  (future-cancel x))
;;=> [true true true true true true]

edbond21:08:56

Sorry, that’s a bad comment. The result is actually (true true true true) as expected.

weavejester21:08:57

mjhamrick: (vec (repeat 10 0)) is the fastest way I can find (and most concise).

gnejs21:08:22

good evening all!

gnejs21:08:32

I’d like to interleave to collection that might differ in length (one might be 1 element bigger than the other). Is there a recommended way to do that? interleave drops the last element in the largest collection. I’d like (interleave-all [1 2 3] [4 5 6 7]) ;;=> [1 4 2 5 3 6 7].

gnejs21:08:41

thanks @gfredericks - I’ll need to look at that for a while to understand what’s happening 🙂

gfredericks21:08:00

oops I used the wrong thing on the last line

gfredericks21:08:06

it still works that way it's just dumber

gnejs21:08:59

I was thinking about a solution where the cols are padded to max length and the using (mapcat concat col1 col2) or something similar 😛 Maybe it’s actually more readable to use if and append the remaining element after using regular interleave 😛

gnejs21:08:40

(obviously “readability” is subjective… :-))

gfredericks21:08:36

if you pad them then how do you remove the padding elements later?

gnejs22:08:38

ok, (mapcat clever-concat-that-drop-nil-element c1 c2) 🙂

gnejs22:08:03

Plain concatwouldn’t do.

gnejs22:08:36

I like your solution though - I just need to understand how it works.

gfredericks22:08:11

you don't want to drop nils because there might be nils in the original collections

gnejs22:08:37

dang. That’s a good point!