Fork me on GitHub
#clojure
<
2018-03-14
>
mccraigmccraig08:03:47

@mbjarland clojure is at 59.6% in most-loved!

mbjarland08:03:51

Crap, reading this on a tiny screen : ) ok, I'm happy then...

mbjarland08:03:28

Then again, JavaScript beat clojure, so I'll be moderately happy...

gklijs10:03:49

I think only 2.7% of developers who are not developing with clojure have expressed interest in developing with it, leaves room for improvement…

danm11:03:37

What would people use to pretty print (indent etc) XML output, and do stuff like strip unused namespaces?

danm11:03:51

I'm looking for stuff in clojure.data.xml, and not having much luck 😕

danm11:03:17

Don't mind using alpha versions if necessary. We're already on the 0.2.0-alpha5 version to get some other functionality we need

flowthing11:03:44

XSLT would work.

danm11:03:53

Are there function in clojure.data to apply an XSLT to an XML structure before emitting it as a string? That'd be ideal, but I couldn't find 'em 🙂

danm11:03:01

Or is there another lib to handle it?

danm12:03:47

Ace, will give it a look. Ta 🙂

flowthing12:03:34

Let me know if you run into any issues.

Qambar14:03:17

hi, how do you remove the namespace declaration in clojure without rebuilding the whole xml structure again ?

Qambar14:03:23

P.S: i am using zippers

misha15:03:03

just realized, you don't have to pass every previous result to every next form in as->. it is not really useful for vanilla as->, because it does not have any short-circuit behavior, though

misha15:03:38

(clojure.edn/read-string "fn [])")
=> fn

bronsa15:03:06

what about that?

Alex Miller (Clojure team)15:03:27

if you want to ensure you match all input, you need to pass opts

pesterhazy15:03:01

By default, read-string reads a token and stops

misha16:03:14

@alexmiller in doc string I don't see any options to pass for this use case (read everything). @pesterhazy yeah, was not my expectation :)

Alex Miller (Clojure team)16:03:55

yeah, I guess it’s really not options really more in calling structure in that you want to ensure you read to EOF

misha16:03:23

do you have a sample code you could point to? given I pass string, not reading from file, I don't see how I'd know where read-string stopped reading.

kaosko16:03:19

oh boy, my lein jar fails with: java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to clojure.lang.Named but lein compile, everything else works. what could be the problem?

misha16:03:19

(w/o doing something silly, like read-string, print-string, substring loop)

misha16:03:10

@kaosko what do you mean by "lein jar"? call? or main in jar built with lein?

misha16:03:37

if former – you might have an extra set of brackets around some settings in project.clj

kaosko16:03:52

thanks @misha, former, yeah looks like an issue with project.clj

jjttjj18:03:29

Is there any clojure core function I'm missing that might help in building up a sequence conditionally like this? I know there are various options to wrap this just wondering if I'm blanking out on a clojure core function

(let [version        (rand-int 10)
      SERVER-VERSION (atom (rand-int 122))]
  (cond-> [:init1 :init2]
    (>= version 3)           (conj :key1 :key2 :key3)
    (>= @SERVER-VERSION 50)  (conj :key4)
    (>= version 2)           (conj :key5)
    true                     (conj :keyA)
    (>= version 4)           (conj :key6)
    (>= version 6)           (conj :key7 :key8 :key9)
    true                     (conj :keyB :keyC)
    (>= @SERVER-VERSION 100) (conj :key10)
    (>= @SERVER-VERSION 120) (conj :key11 :key12)))

jjttjj18:03:24

I also tried this, which I think is probably worse:

(let [version        (rand-int 10)
      SERVER-VERSION (atom (rand-int 122))]
  (->> [:init1 :init2
        (when (>= version 3)           [:key1 :key2 :key3])
        (when (>= @SERVER-VERSION 50)  :key4)
        (when (>= version 2)           :key5)
        :keyA
        (when (>= version 4)           :key6)
        (when (>= version 6)           [:key7 :key8 :key9])
        :keyB :keyC
        (when (>= @SERVER-VERSION 100) :key10)
        (when (>= @SERVER-VERSION 120) [:key11 :key12])]
       (remove nil?) flatten))

jjttjj18:03:51

cool, thanks. Good point on the atoms

noisesmith18:03:04

@jjttjj: in code like this, (apply concat x) tends to be better than (flatten x)

hiredman18:03:13

or just use concat to start with

hiredman18:03:22

or use syntax quote

hiredman18:03:34

(let [version (rand-int 10)
      SERVER-VERSION (atom (rand-int 122))]
  `[:init1
    :init2
    [email protected](when (>= version 3) [:key1 :key2 :key3])
    ~(when (>= @SERVER-VERSION 50) :key4)
    ~(when (>= version 2) :key5)
    :keyA
    ~(when (>= version 4) :key6)
    [email protected](when (>= version 6) [:key7 :key8 :key9])
    :keyB
    :keyC
    ~(when (>= @SERVER-VERSION 100) :key10)
    [email protected](when (>= @SERVER-VERSION 120) [:key11 :key12])])

jjttjj18:03:14

cool thanks all

bronsa18:03:32

beware of using syntax-quote in hot paths tho

bronsa18:03:44

it's quite slow compaed to a version you could write manually

bronsa18:03:00

t.e.jvm suffers quite a bit in performance because of its use of syntax-quote as a templating language

MegaMatt19:03:54

it essentially makes a function which takes an api response and turns specified keys into js/moment objects

joelsanchez19:03:43

why sorted-map?

MegaMatt19:03:35

so that {:id 1 :createdAt moment } comes out the way it was put in rather than the [[:id 1] [:createdAt moment]] which is the result of the map

joelsanchez19:03:25

I think this should work but bear in mind I'm assuming many things about params and your intentions in general

MegaMatt19:03:25

cool, I'll give it a try

MegaMatt19:03:28

definitely looks better than mine

joelsanchez19:03:29

a few things: - using ->> when using into, map, filter and anything that takes coll as the last arg usually leads to easier to follow code - for creating a map from a collection of k-v pairs it's common to do (into {} coll) - using doall when there are no side effects is usually useless or a code smell. in your example you could have also realized the coll using mapv instead of map - when you want to check if A is one of many things, use a set

Sallide19:03:07

Whether I use #'update or just update doesn't seem to make a difference

joelsanchez19:03:04

from a brief read it looks like you're blocking the repl thread

joelsanchez19:03:19

(hence nothing will happen until you restart it)

Sallide19:03:35

I'm new to clojure so I don't fully understand how it works, but I thought that you could update a running clojure process with the repl?

joelsanchez19:03:54

yes, but not if you're running an infinite loop directly in the repl

Sallide19:03:35

I see, so would the solution be to use (future) to run it on a different thread?

joelsanchez19:03:36

a future might work for you...

Sallide19:03:20

So in general, it's impossible to update the repl while the main thread is busy, is that correct?

joelsanchez19:03:35

a more clojurey solution would be to use go-loop and core.async/timeout

hiredman19:03:07

there is no " the repl" there are a couple of different implementations of repls

mfikes19:03:24

For this problem, you could avoid the main thread with (.start (Thread. execute)) instead of (execute)

hiredman19:03:55

and generally a repl is sort of a client/server system, so while client x is waiting for the result of executing whatever, you can create a new client y and execute other stuff

hiredman19:03:38

so the exact behavior you see will depend on what repl client and server you are using

Sallide19:03:55

okay, thank you all

Sallide19:03:18

awesome, it works 😄

mfikes19:03:44

Yeah, it is nice to be able to change the behavior of a running program at will 🙂

Sallide19:03:00

for sure, I come from a JS background and it is like nodemon on steroids

eraserhd20:03:26

I have been having this weird race-condition like problem for ... maybe eight months now? I randomly get a "No namespace: twou.centralpark.async" (our own stuff) when I run clojure.tools.namespace/refresh. I think I've had it happen with another namespace, as well. Doing a lein clean fixes it.

eraserhd20:03:00

Does anyone have any hints for how to fix this, or what to investigate?

eraserhd20:03:03

And, the answer appears to be, "put a %s in your :target-path". Huh. OK

Sallide23:03:23

Hi all, what's the time complexity for comparing maps in clojure? I assume it hashes both, but if the hashes pass there's a chance of collision so does it do any further checks?

phronmophobic23:03:55

It looks like O(n)

Sallide23:03:58

oh great, thank you

noisesmith23:03:13

but if the hashes don't match it can return faster of course

phronmophobic23:03:49

does it look at the hash for =?

Sallide23:03:52

huh, I've never seen { by itself and indented same level before

noisesmith23:03:12

it's a relatively rare coding style, but it even has a name

mikerod01:03:57

I always forget the name

mikerod23:03:48

Ah yes. Thanks for reminder

hiredman23:03:14

hashing isn't an optimization there since hashing has to traverse the entire map anyway

noisesmith23:03:56

but it can cache the hash code, I see that it doesn't check for cached hash, I guess because that would be fiddly

tbaldridge23:03:15

It does check for identical however, it’s pretty rare to compare two instances of huge data sets.