Fork me on GitHub
#clojure
<
2020-05-03
>
Alex Miller (Clojure team)00:05:30

I believe there's a ticket about it

sergey.shvets04:05:08

Hi! If I have a namespace that only has a defmethod in it, how do I force it to load? Just importing it in the namespace that is called on entry-point doesn't seem to do a trick.

seancorfield04:05:45

@bear-z You just require it like any other namespace.

sergey.shvets04:05:26

That's what I'm trying to do, but it doesn't seem that it loads. I even put a println in it and it doesn't fire. I do (:require [myapp.mynamespace]) inside my .app namespace that is called on entrypoint. I do it in ClojureScript, but I don't think it matters in this case.

sergey.shvets04:05:16

I never use it, since I just want to add an implementation of multimethod

seancorfield05:05:44

No idea about cljs so maybe ask in #clojurescript ?

sergey.shvets05:05:16

I'll try, thanks!

solf09:05:48

I don't understand this at all... I haven't been able to make a small reproducible example yet, but I have a function that uses an atom internally. When I return the atom from the function, it shows with the correct contents, when I deref the atom before returning it, it shows empty (with the initial value)

solf09:05:03

Alright... Took me about a hour, somewhere in a function I was using a map, in other words lazy-sequences. Printing the atom resolved the lazy-sequences and made the changes to the atom. Moral of the story: don't use atoms if you can help it

victorb10:05:40

while "don't use atoms if you can help it" is certainly helpful, this seems to rather be about a case for "make sure you're using/not using lazy-sequences where you expect you're using them"

the2bears20:05:36

Yeah, I don't think the moral of this story should be "don't use atoms".

sofra11:05:24

Anyone know if there is anything similar to https://github.com/CITGuru/PyInquirer for Clojure?

phronmophobic18:05:37

@U093DA4QY, there’s https://github.com/mabe02/lanterna, but you have to suffer a Java API

phronmophobic18:05:44

I recently used lanterna to add a terminal graphics backend to https://github.com/phronmophobic/membrane, but I haven’t released it. If you’re interested in something a little experimental, I can make a new release.

phronmophobic18:05:21

here’s TODO mvc running in the terminal (with mouse support!)

sofra23:05:18

interesting, thanks @U7RJTCH6J!

phronmophobic23:05:16

fyi, there was a similar thread in #off-topic with a good suggestion if you’re using cljs, https://clojurians.slack.com/archives/C03RZGPG3/p1588543676426200?thread_ts=1588541027.424900&amp;cid=C03RZGPG3

emccue12:05:39

Yeah there is https://github.com/CITGuru/PyInquirer if you are okay using libpython-clj

borkdude14:05:24

Any comments on this code?

(defn split-when
  "Like partition-by but splits collection only when `pred` returns
  truthy value. E.g. `(split-when odd? [1 2 3 4 5]) => ((1 2) (3 4) (5))`"
  [pred coll]
  (when-first [x coll]
    (if (not (pred x))
      (cons (list x) (split-when pred (rest coll)))
      (let [tail (split-when pred (rest coll))
            coll (first tail)]
        (cons (conj coll x) (rest tail))))))

borkdude14:05:13

Maybe a lazy-seq would be better?

(defn split-when
  "Like partition-by but splits collection only when `pred` returns
  truthy value. E.g. `(split-when odd? [1 2 3 4 5]) => ((1 2) (3 4) (5))`"
  [pred coll]
  (when-first [x coll]
    (if (not (pred x))
      (cons (list x) (lazy-seq (split-when pred (rest coll))))
      (let [tail (lazy-seq (split-when pred (rest coll)))
            coll (first tail)]
        (cons (conj coll x) (rest tail))))))

Ben Sless14:05:03

suggestion: name: split-at api: [pred coll] will return a lazy seq, [pred] will return a transducer?

borkdude14:05:27

@UK0810AQ2 split-at already exists in core

borkdude14:05:10

I don't need a transducer myself, but that would be good to add if this went into a public library

👍 4
Ben Sless14:05:33

so split-when makes most sense, I guess

g7s17:05:04

At the example in the docstring do you mean even? instead of odd?

borkdude17:05:15

I want a new collection every time the predicate returns true.

g7s18:05:22

Oh I see. Then the result should have been something like ((1) (2 3) (4 5)) or am I misinterpreting?

borkdude18:05:10

no, the predicate is odd, so it should only start a new collection when it returns true

borkdude18:05:34

so it starts with 1, because that's odd, until it encounters a new odd number, then you'll get a new coll

g7s18:05:44

Ah ok I had partition-by in my mind that's why I had it the other way

g7s18:05:10

(split-when odd? [0 10 12 3 4 5]) returns ((0) (10) (12) (3 4) (5)) so I assume there is a bug somewhere there?

borkdude18:05:26

you're right

g7s18:05:31

I think it is because it's a bit ambiguous on the starting element. It seems to me that you want to start with a list with the first element in no matter what the predicate, and then split when pred is thruthy else accumulate on the list.

g7s18:05:55

[eeeoe] => ((eee) (oe)) [oeeoe] => ((oee) (oe))

borkdude18:05:12

I was thinking this could maybe also done with partition-by + a stateful predicate

g7s19:05:48

You actually want partition-by but with a twist. When partition-by splits it includes the split element at the new list while you want to retain it in the current list. But with this approach you will end up with an empty list in the sequence in certain occasions if you want to refrain from crazy things like including the first element regardless of the predicate.

markmarkmark20:05:15

how about something like this:

(defn split-when                                                   
  [pred coll]                                                      
  (lazy-seq                                                        
    (when-let [s (seq coll)]                                       
      (let [fst (first s)                                          
            f (complement pred)                                    
            run (cons fst (take-while #(f %) (next s)))]           
        (cons run (split-when pred (seq (drop (count run) s))))))))

💯 4
markmarkmark20:05:16

basically a copy of partition-by with the logic tweaked in the run part

borkdude20:05:20

@U1XTUTPMY That looks like it!

borkdude20:05:48

my original use case was more like this:

(split-when symbol? ['foo 1 2 3 'bar 5 6]) ;;=> ((foo 1 2 3) (bar 5 6))

g7s21:05:35

@U1XTUTPMY nice! One small detail is that you are wrapping the drop call with seq instead of lazy-seq

markmarkmark21:05:52

I'm not sure why/if the seq needs to be there. I just copied the code from partition-by

markmarkmark21:05:13

so... presumably it is correct enough

g7s21:05:38

partition-by has the lazy-seq there and I think it is there to defer the drop call on the "next" consumption

markmarkmark22:05:19

ah, I suppose you're right. The projet I was messing around in is still using 1.8 so (source partition-by) didn't have the lazy seq there. @U04V15CAJ ^

borkdude22:05:03

@U1XTUTPMY oh, so seq should be lazy-seq just before drop on the last line?

borkdude22:05:10

cool, thanks

Chris Lester01:05:39

[.. looks like it's the same answer as above lol .. I just used the first one.] I wound up solving a similar problem (in gap scheduling) with the partition-between from the answer here (for a contrast). https://stackoverflow.com/questions/23207490/partition-a-seq-by-a-windowing-predicate-in-clojure?rq=1

teodorlu14:05:27

Hiccup to Markdown. A small tribute to the conciseness of Clojure!

vlaaad18:05:18

Clojure: operations are efficient by default, so conj adds to beginning or end of the collection depending on it's type to make it always O(1) Also Clojure: when interop is reflecting, it's at most a warning

potetm18:05:48

“operations are efficient by default” - no

potetm18:05:58

take, e.g., last

potetm18:05:07

“operations do what they say on the tin”

potetm18:05:53

If you’re having trouble with interop, I’d recommend Cursive. It takes a lot of pain out of it.

vlaaad18:05:05

good point, but still not true for interop (.foo bar) has wildly different performance implications depending on bar's type hinting

potetm18:05:08

(though it doesn’t catch all scenarios)

potetm18:05:01

Yeah, it’s a pain. I’m so used to it (and used to Cursive’s help), that I don’t feel it much anymore.

vlaaad18:05:04

I use cursive and don't have any troubles with interop, it's just a joke I came up with after seeing this reddit post: https://www.reddit.com/r/Clojure/comments/gcry9d/why_does_my_clojure_implementation_of_conways/

👀 4
potetm18:05:53

Ah, yeah. It really sucks for new folks trying to do perf analysis.

potetm18:05:28

To be honest, primitive hinting is still somewhat amorphous in my head.

cfleming21:05:58

Yeah, Cursive doesn’t have good support for it, at least in part because I don’t have a clear mental model of how it should work.

lol 4
vemv20:05:03

I'm working on the second big codebase in a row with zero reflection warnings (this is, self-inflicted ones; one doesn't have tremendous control over third-party libs) IOW if you reallly want this, you certainly can have it. Like anything, it has bit of a learning curve