Fork me on GitHub
#clojure
<
2019-05-13
>
wei04:05:42

is there a deps.edn version of lein clean?

seancorfield05:05:31

@wei rm -rf .cpcache 🙂

🙏 1
👍 1
Lennart Buit05:05:06

Maybe drop the -f, removing with force always is ill advised

👍 1
seancorfield05:05:40

Decades-long habits are hard to break 🙂

Lennart Buit05:05:27

Haha just as guilty ^^!

seancorfield05:05:22

You can use clj -Sforce to force the dependencies to be recalculated and the cache ignored, but I'm not sure if that overwrites the .cpcache folder...

alexmiller12:05:32

yes, it overwrites (for the specific command you ran)

Dominic Pearson07:05:27

Does anyone have any experience with jnr-ffi? I am trying to write some clj bindings for a c library. i saw from caesium's source code how to do it when the functions return primitives, but the ones i am trying to bind return structs, and i haven't a clue how to approach it. it looks like, from java examples, that I need to extend the struct class with fields, but i don't know how to do that in clojure.

Dominic Pearson09:05:58

(in jnr-ffi, it looks as though in order to use structs you need to extend the Struct class with additional fields. that's the bit i don't know how to do.)

heefoo09:05:14

@lilactown it would be good to wrap your hooks/userReducer to work with multimethods

heefoo09:05:30

or maybe even with whatever implementes Ifn

heefoo09:05:01

had to wrap it myself

lilactown13:05:41

You mean you can't pass a mulimethod to useReducer?

lilactown13:05:13

If so can you open an issue please so I remember

heefoo06:05:09

and yes that was the issue

heefoo06:05:42

multimethods are very convinient with respect to state management

heefoo06:05:30

issue opened

Yehonathan Sharvit10:05:35

A question related to maps and sequences. Some of the sequence functions work on map e.g. first, rest, map (they convert a map to a sequence via seq) However nth doesn’t work on a map

alexmiller12:05:30

nth is for integer indexed cols, maps and sets are not integer indexed

Yehonathan Sharvit06:05:16

Something is still confusing me: On one hand, when we call seq on a map we get a sequence that is not indexed, on the other hand nth works on it

(indexed? (seq {:a 1})) ;; false
(nth (seq {:a 1}) 0) ;;  [:a 1]

alexmiller11:05:24

In general, ops in Clojure have strong perf expectations and you’d expect nth to only work on indexed things. However, nth has a fallback mode that does a linear search so it works on seqs. This is the only example like this I’m aware of in Clojure (and I believe it’s probably to support destructuring)

Yehonathan Sharvit12:05:07

I see. So now I raise back my original question: why nth doesn’t work on a map (with an implicit transformation of the map into a seq)?

alexmiller12:05:05

It’s a collection function, not a sequence function, so does no seq coercion

alexmiller12:05:48

Since maps are unordered, asking for the nth thing is meaningless

alexmiller12:05:20

If you explicitly convert to a seq, then you have something ordered, and you can ask the question

Yehonathan Sharvit12:05:53

Would it be accurate to say that seq coercion occurs only inside functions where the perfs on seq are good?

alexmiller12:05:56

No, seq coercion occurs on all seq functions

alexmiller12:05:40

Not a perf thing

Yehonathan Sharvit13:05:57

What is the exact meaning of a seq function?

alexmiller13:05:06

the functions described at the bottom of https://clojure.org/reference/sequences - functions that take a seqable as last arg and return a sequence (really a seqable)

alexmiller13:05:57

as opposed to collection functions which take a data structure as first argument and operate on the data structure (assoc, conj, nth, disj, contains?, get, etc)

Yehonathan Sharvit15:05:37

I don’t want to drag your attention to unimportant details, but nth appears in the list at the bottom of https://clojure.org/reference/sequences Also, I noticed that last works on maps

alexmiller16:05:32

last is a seq function so coerces to seq

Yehonathan Sharvit19:05:11

according to the previous definition, last is not a seq function as it doesn’t return a sequence

alexmiller19:05:29

ok, "most" return a sequence :)

Yehonathan Sharvit10:05:42

Is there a good reason for that?

souenzzo10:05:36

hash-things (hash-map and hash-set) has no notion of order. Do not make sense ask for "the second item" first and rest make some sense once you can understand "give me 'any' first item"

Yehonathan Sharvit10:05:21

second works on maps

ok12:05:11

who could agree with me: stacktraces should show what was the input...

jumar12:05:33

I wanted this so many times... but input may be huge for example. I guess it's hard to solve in general.

ok12:05:37

so it should be in a separate stacktrace like *e2 or something...

mloughlin12:05:47

disagree, this is one way for personally identifiable information to end up in logs

jumar12:05:07

This could be enabled only in "dev" mode. But I guess the problem is that it really isn't that easy - exceptions can occur at many different levels - at each step you'd need to capture the input and possible decide whether (and what) to show it.

👍 1
mloughlin12:05:55

does clojure have a trace (from CL) equivalent?

mloughlin12:05:10

"Whenever a traced function is invoked, information about the call, about the arguments passed, and about any eventually returned values is printed to trace output."

(defun fact (n) (if (zerop n) 1 (* n (fact (- n 1)))))
=>  FACT
 (trace fact)
=>  (FACT)
;; Of course, the format of traced output is implementation-dependent.
 (fact 3)
>>  1 Enter FACT 3
>>  | 2 Enter FACT 2
>>  |   3 Enter FACT 1
>>  |   | 4 Enter FACT 0
>>  |   | 4 Exit FACT 1
>>  |   3 Exit FACT 1
>>  | 2 Exit FACT 2
>>  1 Exit FACT 6
=>  6

jumar12:05:33

Yes, this is useful and something I (sometimes) use (via Cider). However, you need to instrument the function beforehand - it may be harder if the error isn't that easy to reproduce (which is a problem anyway) or it may just took way longer or it may be deep down in java call stack...

sogaiu12:05:18

haven't tried this, but does this help at all? https://github.com/kkinnear/zpst

jumar12:05:25

Not sure how practical this approach is, but I think you ultimately need JVMTI agent (or something equivalent) - this could be an interesting thing to explore: - https://stackoverflow.com/questions/42226592/how-to-get-the-values-of-method-local-variables-and-class-variables-using-jvmti - https://github.com/cretz/stackparam

👍 1
vemv13:05:25

Can I make (instance? "Ljava/lang/Integer;" x) work? (`instance?` rejects strings as criteria)

vemv13:05:41

which is logical because an array is not an object... anyway my question stands - can I check if something is an array of x?

vemv14:05:54

ok, this is a FAQ easy to find, sorry for the noise

mpenet15:05:23

> (instance? (Class/forName "[Ljava.lang.String;") (into-array String ["s"]) ) => true

vemv15:05:00

Nice one! I ended up avoiding the scary notation completely: (instance? (class (make-array String 0)) (into-array String ["s"]))

Yehonathan Sharvit15:05:08

What are the exact relationships between collections and sequences? Here https://clojure.org/reference/sequences#_the_seq_interface the Seq interface is describe by means of 3 functions first, rest and cons but in the description (and the signature of the function), the term collection is used instead of sequence. Can someone clarify it?

alexmiller15:05:38

sequences are (logical) collections

alexmiller15:05:02

Collections are (in Clojure) defined by IPersistentCollection

alexmiller15:05:23

Sequences are defined by ISeq (which extends IPersistentCollection)

alexmiller15:05:20

IPersistentCollection extends Seqable (to request a seq view of a collection)

Yehonathan Sharvit15:05:51

Thanks @alexmiller. I am going to meditate on it

Alex17:05:12

One thing I think is useful when contemplating sequence vs. collection: A collection is a finite, non-scalar grouping of some 'things', e.g. List, Vector, Set, Map. A seq is an interface to data that makes sense to apply to collections, and also to non-collection, infinite things like (range), (iterate), (cycle), et al. You can compute infinities with a seq, but not with a collection.

alexmiller17:05:20

given that sequences are collections, collections are not necessarily finite

alexmiller17:05:23

one term used in some of the clojure reference docs to refer to list, vector, map, set. sorted set, sorted map, etc is "data structure", which are finite

alexmiller17:05:37

there is no "type" in the Clojure impl that contains those data structures but not sequences

alexmiller17:05:26

sometimes "collection" is used to mean "the data structures but not seqs" and sometimes it is used to mean "all collections, including seqs". this is not done consistently either in the docs, or the code, or the broader community.

Alex17:05:43

Yeah, it's worthwhile to understand the context that the word 'collection' is being used, with more precision than 'talking about Clojure the language'

robertfw17:05:55

I need to write some code that writes to a unix socket (in this case, /dev/log so I can write a timbre appender to send logs direct to syslog). Right now I'm puzzling my way through netty code as it seems to be the only java library that packages the native integration sensibly, but their relative lack of docs is making that pretty slow going. Does anyone know of a clojure library/feature I may have missed for doing so?

Yehonathan Sharvit17:05:32

@alexmiller I am trying to understand exactly what is the meaning of “Collections are represented by abstractions, and there may be one or more concrete realizations.” from https://clojure.org/reference/data_structures#Collections

lilactown17:05:43

I think it means that the actual underlying type might change, but the interface will stay the same if you have an operation that produces the same collection type

lilactown17:05:58

for instance, I believe Clojure maps use a PersistentArrayMap under the hood up until a certain size, which then switches to a different type PersistentHashMap

lilactown17:05:13

for various performance reasons

dpsutton17:05:17

like in scheme and common lisp there are map-hash, map-vector, but in clojure you just use map?

lilactown17:05:28

this difference is opaque to you, because they have the exact same interface

lilactown17:05:00

yes, the seq interface is even more general

dpsutton17:05:16

i think F# is kinda annoying in this way.

lilactown17:05:57

it must have gotten it from OCaml 😛 +, +., oh-my!

noisesmith17:05:05

clojure of course has + and +', but the meaning is much different

robertfw18:05:32

Yes, unfortunately only supports udp/tcp (and is also abandoned)

Alex18:05:47

If Unix socket's a hard req I dunno if there's much pre-existing help out there. You can find some other stuff that'll do the JNI lifting for you (e.g. https://github.com/jnr/jnr-unixsocket, and I'm inclined to trust Charles Nutters' code...)

Alex18:05:01

But you're still gonna be doing the Java interop yourself

robertfw18:05:05

Yeah I'm chatting with our sysadmins to see if they'll consider opening up a plain old net socket, that'll simplify things considerably

robertfw18:05:26

jnr-unixsocket looks like a possible option though, much simpler than going the netty route. thanks 🙂

Yehonathan Sharvit18:05:17

Another question about sequences and collections. In https://clojure.org/reference/sequences#_the_seq_interface the Seq interface is said to be made of first, rest and cons. Why empty? is omitted?

noisesmith18:05:44

empty? is just the negation of clojure.lang.RT/seq

alexmiller18:05:35

http://insideclojure.org/2016/03/16/collections/ expands on the question above in a lot more detail

alexmiller18:05:15

and also in Clojure Applied, ch 2, which has a great picture that I am unable to share with you due to copyright :)

Yehonathan Sharvit19:05:46

No worries. I have this book

eraserhd18:05:37

So, I have a weird problem where I removed a circular namespace dependency, and now I'm getting a spec failure on ns inside a third party library. If I put it back, it works.

eraserhd19:05:00

I'm baffled at how to even debug this.

alexmiller19:05:29

maybe share the error?

alexmiller19:05:40

prob your prior cycle was hiding the load of it somehow

alexmiller19:05:36

latest pedestal is 0.5.5

alexmiller19:05:44

not sure when that was fixed exactly

alexmiller19:05:04

I guess it was in 0.5.5

alexmiller19:05:21

you'll only see that fail as of Clojure 1.10.0

eraserhd19:05:38

welp, I figure you've just saved me three hours of debugging. I'll buy you some kind of bevarage at the next conj.

alexmiller19:05:37

no need, happy to help ;)