Fork me on GitHub
#clojure
<
2022-10-10
>
Gerome07:10:31

Is there a function that separates items from a collection into two collections based on a predicate?

Gerome07:10:18

Ah! Thanks!

elken07:10:38

group-by and destructuring

user=> (let [{evens true odds false} (group-by even? [1 2 4 3 5 6])] [evens odds])

[[2 4 6] [1 3 5]]

👍 1
borkdude08:10:13

There is also split-with , which depending on the exact meaning of your question, could also be applicable ;)

👀 1
reefersleep10:10:43

I also thought they meant split-with 🙂

Brian Beckman13:10:51

partition-by may be helpful, too.

Gerome15:10:50

After fiddling around a bit, split-with was indeed the function I was looking for.

🙌 1
d-t-w03:10:12

As an extension this question leads to my favourite function that isn't in clojure.core (and I learned it from Alex Miller)

d-t-w03:10:35

(def separate (juxt filter remove))

(separate (partial >= 3) [1 2 3 4 5 4 3 2 1])
=> [(1 2 3 3 2 1) (4 5 4)]

(split-with (partial >= 3) [1 2 3 4 5 4 3 2 1])
=> [(1 2 3) (4 5 4 3 2 1)]

👀 1
d-t-w03:10:48

split-with only splits once, separate separates entirely

vemv12:10:01

separate looks like a group-by in disguise :thinking_face: generally I'll take an associative API over a sequential one (edit: unless you want to preserve order?)

d-t-w06:10:49

It’s a binary split on a predicate applied to the input sequence that leads directly to the desired output without another step to pull vals from a map, it’s fairly different from group-by if your requirement is what was originally described.

Gerome07:10:49

I can't be the first one to solve this problem 😄

Brian Beckman13:10:58

SOLVED Hello, clojurians. I am trying to write codox for defmulti and defmethod. I tried all permutations of the positions of the docstrings in the following (with the #_ killer removed). The code works without docstrings, so I’m not worried at the code level. Each attempt to insert a docstring causes deep exceptions from lein codox in java layers. Docstrings for functions and vars work fine for me. Any hints?

(defmulti asdl-form first #_"Convert three `ASDL` kinds into
Clojure hashmaps" )

(defmethod asdl-form :ASDL-SYMCONST
  #_"Convert `ASDL-SYMCONST` into a Clojure hashmap."
  [form]
  (apply hash-map form))

(defmethod asdl-form :ASDL-COMPOSITE
  #_"Convert `ASDL-COMPOSITE` into a Clojure hashmap."
  [form]
  (let [[_ head args-pre] form
        [_ & decls] args-pre] ;; & means listify
    {:ASDL-COMPOSITE {:ASDL-HEAD (second head)
                      :ASDL-ARGS (map decl-map decls)}}))

(defmethod asdl-form :ASDL-TUPLE
  #_"Convert `ASDL-TUPLE` into a Clojure hashmap."
  [form]
  (let [[_ args-pre] form
        [_ & decls] args-pre] ;; & means listify
    {:ASDL-TUPLE (name (gensym "asr-tuple"))
     :ASDL-ARGS (map decl-map decls)}))

zeitstein13:10:44

I'm not familiar with codox, but defmethod doesn't support docstrings. Perhaps that's the reason?

👍 1
rolt13:10:09

(defmulti asdl-form "your docstring" first)

👍 1
Brian Beckman13:10:00

I must have missed that permutation 🙂 ty

Brian Beckman13:10:41

@U02E9K53C9L at least i got the multi working thanks to @U02F0C62TC1

🙌 1
Joshua Suskalo14:10:36

Yeah the reason for defmethod not having a separate docstring is that in theory it should be doing the exact same operation as the defmulti says it's doing, just specialized on the arguments. That said, I've written plenty of multimethods that could've used some documentation for individual defmethods, so it would be cool if some kind of support for this could exist.

rolt14:10:33

what would this do ? append the defmethod docstring to the defmulti var's docstring ? or just ignore it and it's just a comment for the dev reading the code ?

Joshua Suskalo14:10:44

Nah, if I were to implement it myself I'd add a registry that's a map from dispatch value to docstring, and then allow something like

(multidoc the-method arg1 arg2)
and have it print out/return the docstring for whichever method gets invoked by those arguments.

rolt14:10:45

that's a good idea!

Annaia Danvers16:10:43

Are there any docs generators for Clojure with an output format that is less than "an entire website"?

borkdude16:10:32

It outputs an API.md which you can check directly into source control. It looks like this: https://github.com/babashka/fs/blob/master/API.md

Annaia Danvers16:10:10

OMG, this is exactly what I was hoping for, thank you!

borkdude16:10:10

Let me do a new release