Fork me on GitHub
#clojure
<
2019-10-16
>
Honda Subaru04:10:46

Guys... Can anyone please please help me with this?

Honda Subaru04:10:10

I've been at it for several days...seems pretty trivial but still has continued to successfully and repeatedly want to smash my keyboard...the result being one of my command keys has popped out of the keyboard

seancorfield04:10:52

"Guys" is kind of rude. There are 16,000 people here and a lot of them are not guys. Please use "Folks" or "Everyone" or "All" -- something inclusive.

👍 4
the2bears04:10:02

Kind of funny, but in (western) Canada "guys" is as general a term as "folks". It's what I used growing up. 🙂 But typed out it feels different, so I agree with your point. Better to be more precise.

seancorfield04:10:57

Yeah, it can mean different things to different people even in different regions of the same country. And the Admin team have had complaints about it -- and I've seen complaints about it in other communities too. So a little nudge to be a bit more aware is often worth any discomfort it may cause...

seancorfield05:10:03

(any further discussion should go to either #off-topic or #community-development depending on which way you want to take the subject -- but I don't really think it needs much elaboration)

the2bears22:10:04

No more discussion needed, as I said I agree.

4
seancorfield04:10:21

(I'm one of the Admins here, BTW)

Honda Subaru04:10:34

I am so sorry..I absolutely agree with you...that was insensitive of me..

seancorfield04:10:13

Looking over your question, I think the simple answer is "You can't write atoms out like that to a file and read them back in".

seancorfield04:10:51

You'd need to arrange for your neural map to write out plain data -- with the atoms dereferenced

Honda Subaru04:10:17

What if I serialize and deserialize it?

seancorfield04:10:32

Hmm, I'm not sure how an atom serializes...

seancorfield04:10:59

My approach would definitely be to transform the data structure to a plain Clojure data structure and use that for the persistent file format -- unless you have cyclic data? But I would avoid records and atoms etc for the data structure if possible.

Honda Subaru04:10:28

I see...ok thanks...

Honda Subaru04:10:40

I'll try some things out and see

seancorfield04:10:45

If your data isn't cyclic it should be fairly straightforward. Otherwise you might need to synthesize some "labels" for nodes so you can write references out instead of cycles 🙂

yuhan04:10:00

I think fundamentally that's because an atom is a Place and not a Value, and Clojure doesn't allow you to simply serialize something that's a mutable reference to something else?

seancorfield04:10:37

Yeah, serializing "variables", whether Var, atom, ref, agent, is not a default built-in operation. clojure.datafy has a way to render an atom as "pure data" but with complex metadata:

[email protected]:~/clojure$ clj
Clojure 1.10.1
user=> (require '[clojure.datafy :as d])
nil
user=> (def a (atom 42))
#'user/a
user=> (d/datafy a)
[42]
user=> (meta *1)
#:clojure.datafy{:obj #object[clojure.lang.Atom 0x5328a9c1 {:status :ready, :val 42}], :class clojure.lang.Atom}
user=>      

seancorfield04:10:15

(so it still isn't suitable for serialization to a file directly but...)

Honda Subaru06:10:27

@seancorfield solved it using handler functions...it is possible to save and load atoms from files directly

Honda Subaru06:10:45

Posted it on stack overflow

seancorfield06:10:04

Cool! Off to read that now. Thank you for posting back with a solution!

seancorfield06:10:02

Instead of def (which always creates a top-level binding) inside defn, you probably want let to create local bindings.

seancorfield06:10:12

Your solution will fail if anything other than an atom expands to #object within your data -- just so you're aware that solution is very specific to your current data model.

akond07:10:38

i keep getting this error

Error updating class definitions:
Execution error (NullPointerException) at java.util.concurrent.ConcurrentHashMap/get (ConcurrentHashMap.java:936).
i have no idea where it comes from.

andy.fingerhut07:10:47

Do you get a stack trace? Or if you get this in a REPL session, do you know how to get one, e.g. via something like (pst *e) ?

akond07:10:50

this is a repl

akond07:10:14

i get the error when i start a repl

akond07:10:05

it was a stack trace

akond07:10:24

but somehow after several restarts the message disappeared.

jumar07:10:49

Is this run via lein repl?

jumar07:10:21

Is it possible that it could be affected by OmitStackTraceInFastThrow?

jumar07:10:40

Ah, sorry, too early - that should be disabled by default I think in lein

jumar08:10:00

Can you post the full command and output?

akond08:10:02

yes, there is -XX:-OmitStackTraceInFastThrow in the command

akond08:10:21

i am not sure how it got there

akond08:10:42

it seems that it is a intellij default

jumar08:10:11

Anyway, that means that the stacktrace shouldn't be elided by the JVM

jumar08:10:31

I'd try to run it from command line then

jumar08:10:39

If that works ask in #cursive

akond08:10:34

thank you for your help

vlaaad07:10:36

evaluate *e to see full stacktrace?

akond07:10:49

is there a channel for manifold library?

akond07:10:58

or i just go right here?

sogaiu08:10:31

don't know if it will help, but there is an aleph channel

akond08:10:19

there is. but it's almost dead.

sogaiu08:10:16

perhaps it's worth trying here then :)

akond08:10:06

i expected to see this code to take about 1.5 seconds to complete, but it takes 3.5 seconds. obviously, i don't yet understand how to do concurrent programming in clojure properly.

(let [pool  (e/fixed-thread-executor 5)
		  xform (fn [xf]
					(fn
						([] (xf))
						([result] (xf result))
						([result value]
						 (xf result (d/future-with pool
										(timbre/info "Got " value)
										(Thread/sleep 500)
										(inc value))))))
		  ch    (ms/stream 5 xform)]
		@(d/future-with pool (ms/connect (ms/->source [1 2 3 4 5 6 7]) ch))
		(time (vec (ms/stream->seq (ms/realize-each ch)))))

Nazral09:10:11

Hi, I'm using sente and I'd like to expose an API through a /foo endpoint, but I have the "anti-forgery" middleware which makes my requests end with a Invalid anti-forgery token , is there a way to request the CSRF token from outside ?

Nazral09:10:40

And how can I use it with curl for example? (Because worst case I can dump to csrf token to a file when starting it)

borkdude14:10:15

any best practices considering javadoc generated documentation for java sources and how to package that along your Clojure library?

Alex Miller (Clojure team)14:10:25

Maven has a convention for this

Alex Miller (Clojure team)14:10:47

you upload it with same coordinates, but classifier of "javadoc" (as a jar file)

borkdude14:10:43

what I would rather want is generate these docs so they become visible on github

Alex Miller (Clojure team)14:10:25

well you can use github-pages for that

borkdude14:10:31

right, was thinking about that

Alex Miller (Clojure team)14:10:37

either on a gh-pages branch or in a gh-pages directory now

borkdude14:10:49

aah, thanks, I'll look into that

Alex Miller (Clojure team)14:10:51

that's how all the clojure core docs are posted

Alex Miller (Clojure team)14:10:08

https://clojure.github.io/clojure/ - is just a gh-pages branch in the clojure repo

Alex Miller (Clojure team)14:10:56

(this pre-dates the directory option)

borkdude15:10:37

I actually wanted to suppress the _val method documentation but it seems javadoc doesn't have something for that

borkdude15:10:43

Now I'll have to figure out how to do a javadoc for a class generated with :gen-class 😛

borkdude15:10:05

Maybe I'll just write a wrapper class and be done with it

Alex Miller (Clojure team)15:10:52

that would be my recommendation

Yehonathan Sharvit15:10:01

Is there a way to catch all exceptions. For instance, the following try/catch block doesn’t catch the exception:

(try (1/0)
       (catch Throwable e
         42))
here is the exception thrown:
3. Unhandled clojure.lang.ExceptionInfo
   (No message)
   #:clojure.error{:phase :read-source}
                  main.clj:  410  clojure.main/repl/read-eval-print/fn
                  main.clj:  409  clojure.main/repl/read-eval-print
                  main.clj:  435  clojure.main/repl/fn
                  main.clj:  435  clojure.main/repl
                  main.clj:  345  clojure.main/repl
               RestFn.java:  137  clojure.lang.RestFn/applyTo
                  core.clj:  665  clojure.core/apply
                  core.clj:  660  clojure.core/apply
                regrow.clj:   18  refactor-nrepl.ns.slam.hound.regrow/wrap-clojure-repl/fn
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   79  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   55  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  142  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  171  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  170  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  745  java.lang.Thread/run

2. Caused by clojure.lang.LispReader$ReaderException
   java.lang.ArithmeticException: Divide by zero
   #:clojure.error{:line 33, :column 12}
           LispReader.java:  314  clojure.lang.LispReader/read
           LispReader.java:  216  clojure.lang.LispReader/read
           LispReader.java:  205  clojure.lang.LispReader/read
                  core.clj: 3768  clojure.core/read
                  core.clj: 3741  clojure.core/read
    interruptible_eval.clj:   99  nrepl.middleware.interruptible-eval/evaluate/fn
                  main.clj:  410  clojure.main/repl/read-eval-print/fn
                  main.clj:  409  clojure.main/repl/read-eval-print
                  main.clj:  435  clojure.main/repl/fn
                  main.clj:  435  clojure.main/repl
                  main.clj:  345  clojure.main/repl
               RestFn.java:  137  clojure.lang.RestFn/applyTo
                  core.clj:  665  clojure.core/apply
                  core.clj:  660  clojure.core/apply
                regrow.clj:   18  refactor-nrepl.ns.slam.hound.regrow/wrap-clojure-repl/fn
               RestFn.java: 1523  clojure.lang.RestFn/invoke
    interruptible_eval.clj:   79  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:   55  nrepl.middleware.interruptible-eval/evaluate
    interruptible_eval.clj:  142  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                  AFn.java:   22  clojure.lang.AFn/run
               session.clj:  171  nrepl.middleware.session/session-exec/main-loop/fn
               session.clj:  170  nrepl.middleware.session/session-exec/main-loop
                  AFn.java:   22  clojure.lang.AFn/run
               Thread.java:  745  java.lang.Thread/run

1. Caused by java.lang.ArithmeticException
   Divide by zero

              Numbers.java:  188  clojure.lang.Numbers/divide
           LispReader.java:  512  clojure.lang.LispReader/matchNumber
           LispReader.java:  350  clojure.lang.LispReader/readNumber
  ...

ghadi15:10:14

1/0 is a read time exception

Alex Miller (Clojure team)15:10:18

Throwable does catch all exceptions at execution time

ghadi15:10:25

but your code has a runtime try/catch

Alex Miller (Clojure team)15:10:33

as ghadi says, this is invalid syntax

ghadi15:10:59

if you wrote (/ 1 0) inside the try, it would be caught

Yehonathan Sharvit15:10:51

Is there a way to catch read time exceptions?

Alex Miller (Clojure team)15:10:34

if you're the one invoking the reader, sure

Alex Miller (Clojure team)15:10:52

you haven't said in what context you're doing this

Alex Miller (Clojure team)15:10:31

looks like slamhound plugin?

Yehonathan Sharvit15:10:00

No. I’m just hacking in the REPL

Alex Miller (Clojure team)15:10:36

the read is occurring in the repl

Alex Miller (Clojure team)15:10:08

you, as a user of the repl are not in the loop on that

ghadi15:10:50

you have to control the R if you want to catch read-time exceptions

ghadi15:10:15

another invalid thing in the same category is {:singlekey}

ghadi15:10:29

user=> {:singlekey}
Syntax error reading source at (REPL:6:13).
Map literal must contain an even number of forms

Yehonathan Sharvit16:10:09

My original problem had to do with lazy sequences. I figured it just now. Here is an illustration:

(defn read-numbers [nums]
    (try (map read-string nums)
         (catch Throwable e
           nil)))

  (first  (read-numbers ["1aa"]))
The problem is that read-numbers uses map - which is lazy. Therefore the evalution of the code is delayed and the exception is not caught by the try/catch block

Yehonathan Sharvit16:10:31

I guess it’s a common pitfall of lazy seqs

Yehonathan Sharvit16:10:43

Any idea how to avoid this kind of issues?

bronsa16:10:08

force the map or move the try catch inside of it

Alex Miller (Clojure team)16:10:02

or really in this case the "catching of bad input" is something that should be done around each read of the input, not around the whole job

Alex Miller (Clojure team)16:10:59

(map #(try (read-string %) (catch Throwable e <something>)) nums)

seancorfield17:10:52

I'd probably use mapv in situations like this @viebel

✔️ 4
kenny19:10:45

Alright folks, I've got a head scratcher. I have a map that I'm trying to serialize with Nippy. I think it's a map, at least. When I call Nippy's freeze method on my "map", I get this exception:

(taoensso.nippy/freeze
  (apply dissoc as (keys as)))
Execution error (ExceptionInfo) at taoensso.nippy/throw-unfreezable (nippy.clj:720).
Unfreezable type: class clojure.core$partial$fn__5839
The apply dissoc is to just empty the map:
(apply dissoc as (keys as))
=> {}
The map appears to be a map:
(type (apply dissoc as (keys as)))
=> clojure.lang.PersistentHashMap
Typing in a map myself and calling freeze works:
(taoensso.nippy/freeze {})
=> #object["[B" 0x54ce414 "[[email protected]"]
The exception appears to be saying that I'm trying to serialize something that is a function but I have no idea where that's coming from. This map came from a Datomic pull so I thought it could be something weird Datomic was doing but the map seems to be "just" a regular old map. Next step is to start diving into Nippy, I guess. Anyone have any idea what could be happening here?

hiredman19:10:52

what makes you thing it is "just" a regular old map

kenny19:10:00

The type call.

dpsutton19:10:34

what makes you "think" its a map versus know its a map. where's the doubt coming from?

ghadi19:10:13

it's a gif that says "don't elide stacktraces"

kenny19:10:17

Well, I've gotten bitten by the printer outputting something that looks like something normal but is actually something totally different. I don't think that's the case here.

kenny19:10:24

I also know Datomic can output things in a somewhat unexpected type. Can't recall the exact instances. I don't think that applies here because the type is a PersistentHashMap.

dpsutton19:10:21

Does nippy serialize meta?

kenny19:10:24

I've tried restarting the REPL so there's no weird state.

kenny19:10:27

Ooo, maybe.

kenny19:10:37

That's gotta be it!

kenny19:10:41

(meta (apply dissoc as (keys as)))
=>
#:clojure.core.protocols{nav #object[clojure.core$partial$fn__5839 0x7cfd1de5 "[email protected]"]}

kenny19:10:32

Yep, that's it:

(taoensso.nippy/freeze (with-meta (apply dissoc as (keys as)) {}))
=> #object["[B" 0x7cfeb74d "[[email protected]"]

ghadi19:10:15

seems like a weird default to serialize meta

kenny19:10:36

Yeah, I agree.

kenny19:10:04

I don't even see an obvious way to disable metadata serialization 😞

kenny19:10:47

For those following, I opened an issue here: https://github.com/ptaoussanis/nippy/issues/122

jpmonettas20:10:47

hi! I'm trying to read a clojure file forms using tools.reader/read, but it is failing because one form contains ::stc/opts keyword and that alias is defined with (alias 'stc 'clojure.spec.test.check) any ideas on how to read this file or how to configure tools reader to skip that form instead of throwing ?

jpmonettas20:10:57

yeah I'm using it, extracting it from ns declaration using tools.namespace, but don't know what to do about aliases defined with alias, I'm looking for a automated way for any file, not just for this file

hiredman20:10:04

it is one of the problems with using ::, its meaning is context dependent and the only sure fire way to have the context is to eval the file

jpmonettas20:10:04

hmmmm, will have to pre-parse the file looking for alias forms then

hiredman20:10:43

I could put a function call in a file that called a function in another namespace, which altered the alias for the current namespace, which would change how subsequent :: keywords are resolved

jpmonettas20:10:12

but I have a keyword with a alias

hiredman20:10:22

because clojure is a form at a time, a given toplevel form can be executed and effect the environment subsequent forms are read in

hiredman20:10:16

so for example, I could having something like (foo) in my file, in a namespace call it A, where foo is brought in from namespace B, and is defined like (defn foo [] (change-all-aliases-in-*ns*-to-point-to-clojure-core))

jpmonettas20:10:06

quite a hack

hiredman20:10:12

and clojure would happily load that file, and the aliases for keywords or symbols would be whatever before (foo) and after foo they would be all different

hiredman20:10:56

that is a pathological case, and I don't know what your intent is, I am just saying the bar for handling that completely generally is very high

jpmonettas20:10:03

yeah I'm doing clojure static code analysis and I'm struggling with this cases

hiredman20:10:16

it is very similar to the case of determining what the symbol foo/bar refers to in a given namespace, for the majority of cases you can figure it out just by looking at the ns form at the top of the file, but that is just due to convention and good taste

jpmonettas20:10:42

yeah it would be nice if we could tell the reader what to do when it can't resolve something

hiredman20:10:00

that is more or less what the alias map does

jpmonettas20:10:26

I was thinking more on common lisp condition system he

hiredman20:10:06

I believe the tools.reader code calls the alias-map as a function, which means you can rebind it to a function instead of a map

jpmonettas20:10:22

oh that is interesting

jpmonettas20:10:17

it is relying on a impl detail but it will work for what I'm trying to do, thx again @hiredman

kenny21:10:01

Does anyone know if it is possible to load native libraries from the classpath?

hiredman21:10:41

by which I mean, I bet you could figure out some way to do it, but you will be so far off the beaten path that you would have to invent your own ffi to do it

hiredman21:10:26

usually if the native library is distributed in a jar you extract it somewhere (/tmp) and load it from there

kenny21:10:08

That's kinda what I was thinking. It's a bit annoying because that means you need some custom startup script and everything can't be done within the JVM.

kenny21:10:34

How about including the native libs on my classpath, copying them at runtime in my -main to a path on the java.library.path and then proceeding with app boot?

hiredman22:10:12

why do you need a custom startup script?

kenny22:10:31

To do the "extract it" step.

hiredman22:10:02

looks like you got there, yeah, just do it in your app