Fork me on GitHub
#clojure
<
2018-06-03
>
lilactown02:06:08

does anyone know how to create user-defined tag literals in clojurescript?

tianshu12:06:40

Is there a function that does (first (filter ...))effectively on a chunked seq?

tianshu13:06:13

I found three ways on stackoverflow, they are (first (drop-while ...)), reduce with reduced and some with a #(when (pred %) %), I wonder if there's a better way?

petterik14:06:12

There's also (first (eduction (filter pred) (take 1) coll))

petterik14:06:58

I benchmarked the different ways of doing this:

(def coll (seq (into [] (range 10000))))
(def pred #(< 1000 %))

(defn filter+first [] (first (filter pred coll)))
(defn some+when [] (some #(when (pred %) %) coll))
(defn eduction+first [] (first (eduction (filter pred) (take 1) coll)))
(defn reduce+reduced [] (reduce #(when (pred %2) (reduced %2)) nil coll))
Here's the mean execution times:
{:filter   "12.65µs"
 :some     "22.15µs"
 :eduction "21.16µs"
 :reduce   "7.14µs"}
(edit: wraps coll in (seq ..) as @doglooksgood's context was chunked seqs)

petterik14:06:12

Benchmarked using criterium {:mvn/version "0.4.4"} with (criterium.core/bench (f))

jumar14:06:33

I'm quite surprised that this works 🙂

(random.namespace.which.does.not.exist/.split "ahoj" "ah")
;;=> ["", "oj"]

😄 4
Ethan Miller15:06:29

Can anyone point me to some documentation regarding the behavior of the leinigen project.clj flag :exclusions. I'm encountering a gordian knot of dependency conflicts between ring and datomic cloud and in the process of trying to fix it have realized I only vaguely understand what actually is being "excluded".

jumar16:06:46

@U3RGL6XNF I'd try to exclude jetty-io dependency if that's the one causing you problems (addes as a comment to SO question). You can't exclude everything, because datomic doesn't bring the complete ring-jetty-adapter (not sure just guessing from what you've shown us. Moreover, the simplest option can be to just add explicit dependency on ring-jetty-adapter with proper version

Ethan Miller16:06:48

Thanks @U06BE1L6T. I will try those suggestions. I've tried so many different options here that I've started to lose track.

jumar16:06:06

Yes, dependency conflicts are often tricky to deal with (and hidden until you actually try to run your application)

Ethan Miller20:06:37

Yeah immensely frustrating. I love writing in clojure but this stuff is 😡

Ethan Miller00:06:48

@U06BE1L6T this is a github repo showing what I've been seeing. Running lein ring server causes the dep error that I can't resolve. https://github.com/ezmiller/datomic-ring-dep-conflict

Ethan Miller01:06:14

It seems to happen only when Datomic Cloud's d/client is called.

Vincent Cantin02:06:15

just in case, be aware of the typo :exlcusions -> :exclusions

Ethan Miller02:06:11

Where are you seeing that?

Vincent Cantin02:06:50

there: Can anyone point me to some documentation regarding the behavior of the leinigen project.clj flag :exlcusions. I'm encountering a gordian knot of dependency conflicts between ring and datomic cloud and in the process of trying to fix it have realized I only vaguely understand what actually is being "excluded".

Ethan Miller02:06:12

Oh sorry. I thought you mean in the code somewhere. sorry about the typo.

Vincent Cantin02:06:39

I was thinking that it may be in your code.

jumar06:06:44

@U3RGL6XNF When I ran lein ring server I got java.lang.IllegalArgumentException Cannot open <nil> as a Reader. I don't have db-config.edn you are referencing in your example so I cannot really try it.

jumar09:06:08

See my answer on SO and #datomic

tianshu16:06:00

@petterik great work! which should I choice if the pred function is heavy. use reduce + reduced?

petterik17:06:46

@doglooksgood the pred function will be run the same amount of times for all examples, so it shouldn't matter. I'd choose filter+first or some+when when performance doesn't matter because it's easier to understand. The reduce+reduced when perf matters

hagmonk18:06:08

say I have a collection of 3-tuples, what's a good way to map over that and apply a different function to each tuple element?

Timo Freiberg18:06:58

(map (fn [f x] (f x)) [f1 f2 f3] tuple) ?

Timo Freiberg18:06:59

mapv is probably a better choice

hagmonk18:06:12

yah, just trying out a few options for mapping that over a collection

Timo Freiberg18:06:23

ah, a collection of tuples?

hagmonk18:06:27

[[:a :b :c] [:d :e :f]]

hagmonk18:06:42

let's say we want to map [str name keyword?] over that

hagmonk18:06:26

(->> [[:a :b :c] [:d :e :f]]
       (map (partial interleave [str name keyword?]))
       (map (partial partition 2)))

hagmonk18:06:28

halfway there ....

hagmonk18:06:35

but then it's hard to map apply across because the fn and args are in one collection, end up having to map (apply (first %) (rest %))

hagmonk18:06:51

I'm shaving this yak pretty hard ;)

Timo Freiberg18:06:38

(for [[a b c] tuples]
        [(str a) (name b) (keyword? c)])
like that?

Timo Freiberg18:06:17

it's a bit harder to parametrize this snippet by functions as well, but i think it's the easiest to understand

hagmonk18:06:35

I do in fact want to parameterize the fn list

noisesmith18:06:29

or did they want (for [[a b c] tuples f [str name keyword]] [(f a) (f b) (f c)])

hagmonk18:06:39

the fn I'm ultimately writing will look like: (clean-collection [fn1 fn2 fn3] some-coll), where some-coll is a collection of 3-tuples (in this case)

noisesmith18:06:04

and what does the result look like?

noisesmith18:06:21

does fn1 only get called on the first item of some-coll, all of them?

hagmonk18:06:30

it's positional, so [[:a :b :c] [:d :e :f]] will end up like [[(fn1 :a) (fn2 :b) (fn3 :c)] [(fn1 :d) (fn2 :e) (fn3 :f)]]

noisesmith18:06:40

oh (map #(%1 %2) fns coll)

noisesmith18:06:55

oh, for each coll

noisesmith18:06:27

(for [coll colls] (map #(%1 %2) fns coll))

Timo Freiberg18:06:52

i like that one

noisesmith18:06:54

there's a dirty trick where deliver is equivalent to #(%1 %2) but don't use that, it's an implementation detail of promise

hagmonk18:06:58

ohh, the #(%1 %2) idiom, that's the kind of thing I was missing … I remember that from stuff like

(condp #(%1 %2) "thing"
    keyword? "keyword!"
    string? "string!")

hagmonk18:06:26

(for [coll [[:a :b :c] [:d :e :f]]]
    (map #(%1 %2) [str name keyword?] coll))
=> ((":a" "b" true) (":d" "e" true))

hagmonk18:06:36

looks nice

hagmonk18:06:17

(mapv (partial mapv #(%1 %2) [str name keyword?]) [[:a :b :c] [:d :e :f]])

hagmonk18:06:36

thanks for bouncing that one around :)

👍 8
noisesmith19:06:12

I think map inside for is nicer than mapv partial mapv