Fork me on GitHub
#beginners
<
2017-05-18
>
matan02:05:43

is there something that shows you the "shape" of a var in the repl? e.g. some var may hold a map of lists of maps or so on, and groping it with multiple keys count and take so as to see its structure without thousands of console output lines (when the data structure is beyond very small) is rather tedious of course.

alexmiller02:05:02

there’s clojure.inspector/inspect

matan03:05:18

I just get a table

matan03:05:37

without any sense of hierarchy

matan03:05:50

will try on more examples

alexmiller04:05:01

oops, sorry - I meant inspect-tree

matan09:05:01

@alexmiller ah! great, thanks again!

matan11:05:12

I tried the above, and it only sends an unrealized lazy seq to stdout/stderr. Any idea?

matan11:05:34

The idea was to include just the beginning of a collection in an error message

alexmiller11:05:38

because that’s how lazy seqs print, regardless of realization

matan11:05:57

shouldn't doall change that?

matan11:05:13

okay, doall is only for side-effects, not related I guess.

alexmiller11:05:27

this bounded printing capability is actually built into the printer

matan11:05:59

oh, nice to know

alexmiller11:05:04

(binding [*print-length* 4] (print-str (range 1000)))

matan11:05:12

mmmm 🙂

alexmiller11:05:05

so in your code you could replace with something like (binding [*print-length* 4] (print-str coll))

matan11:05:22

and otherwise, lazy seqs will never print their contents??

alexmiller11:05:01

well, it depends. with str, no.

alexmiller11:05:15

the printer will do various things in various circumstances

matan11:05:58

okay, good to know

matan11:05:52

thanks a lot! I'd never realize there's print-str to use for that! now I see the differences between the two 🙂

alexmiller11:05:28

there are untold wonders in the printing system :)

alexmiller11:05:36

I wish they were a little less untold :)

vitruvia13:05:26

does anyone here develop clojure on Neovim?

dominicm13:05:41

@vitruvia Hi, yes I do 🙂. We hang out in #vim-fireplace

vitruvia13:05:45

@dominicm Thanks! I'll head there

lmergen16:05:25

hey, i’m using a library that works with a promise, and i want to listen for the delivery of that promise and invoke a callback when it’s delivered

dominicm22:05:31

lmergen: (.start (Thread. (fn [] (f @thepromise)))) is this suitable?

lmergen16:05:28

is this possible at all ?

lmergen16:05:52

(specifically, i want to “pipe” the promise into a core.async channel)

lmergen16:05:08

i understand that a promise-chan is not what i want for this, correct ?

john16:05:49

@lmergen You might be able to just use a .then on it.

john16:05:22

But you might be able to learn something from promesa (or use it): https://funcool.github.io/promesa/latest/#promise-chaining

lmergen16:05:10

is that compatible with clojure’s regular promises ?

john16:05:20

oh sorry

john16:05:36

I thought you were talking about leveraging JS promises from CLJS 🙂

lmergen17:05:10

well, nevermind, guess i’ll be using another http library then 🙂

john17:05:03

You can use realized? but yeah, you'd have to build your notifier.

noisesmith17:05:27

@lmergen if you use an atom, ref, or agent instead of a promise you can attach a watch that will execute when it takes on a new value

matan17:05:37

Any clojure.core or particularly idiomatic way of merging maps while failing if keys repeat? I was thinking of using merge-with for it, unless there's already something.

noisesmith18:05:43

what kind of fail?

matan18:05:56

an exception (call me extreme, or safety lover)

matan18:05:00

obviously this would be just a specialization of merge-with

john18:05:32

You're trying to merge a non-map with a map? Otherwise you shouldn't have dup keys, right?

matan18:05:57

a map with a map

matan18:05:06

why wouldn't I have dup keys?

john18:05:25

what do you mean "if keys repeat"?

noisesmith18:05:34

both maps have the same key

matan18:05:52

(merge {:a 1} {:a 2}) as a simplified example

john18:05:07

that's not a fail, afaiu

noisesmith18:05:08

(merge-with (fn [& _] (throw (Exception. "duplicate key"))) x y)

matan18:05:22

yep that's what I thought

matan18:05:29

I just like my assumptions explicit in the code

noisesmith18:05:32

sadly merge-with doesn’t see the key that overlapped

matan18:05:20

that's fine, an extra fancy expression can figure that out and add it to the exception

john18:05:21

Oh, you want it to fail. my bad

matan18:05:34

@john no bad, thanks

noisesmith18:05:20

(if-let [dups (set/intersection (keys x) (keys y))] (throw (ex-info "duplicate keys" {:keys dups})) (merge x y))

noisesmith18:05:23

ex-info is cool

matan18:05:41

now this goes into my private util namespace, you beat me to it

matan18:05:18

but I would do the merge first, only calculate the intersection on failure (performance...)

noisesmith18:05:21

you can use ex-data to get the data arg passed to ex-info in a catch block

matan18:05:38

oh, good to know, will read about it

noisesmith18:05:10

ahh - so you could merge-with, and then calculate the duplicated keys inside the merging function

matan18:05:21

yes, either way

john18:05:14

(defn merge-unique [x y] (reduce #(if ((first %2) %1) (throw "error") (conj (or %1 {}) %2)) x y))

john18:05:30

something like that