Fork me on GitHub
#clojure
<
2020-11-06
>
didibus01:11:31

What's people's thoughts on extending nil to support qualifications. For example, imagine you could do: nil/missing-key or nil/file-not-found These would all be treated as a standard nil except there'd also be a function to get the qualified part as a keyword so you could possibly branch on it if you wanted. Also, it would print with the qualification so the programmer can understand the reason for the nil. And I'm imagining they'd get interned, so two nil/missing-key would be equal.

👎 3
phronmophobic01:11:16

overloading null and falsey values is common mistake in other languages/systems/databases. I think using some other alternative would be more appropriate. (eg. [nil :missing-key] over nil/missing-key)

seancorfield01:11:12

Also nil is currently the underlying Java null (and JavaScript null I assume?) so you'd have to deal with all the conversion back and forth between a native host value and the "metadata" you wanted to attach -- which could be horrendous from a performance p.o.v.

6
didibus08:11:15

I'm not thinking of overloading it. Nil would still be nil and behave like nil. But it provide a qualification that can let you specify what was nil.

didibus08:11:20

Ya, the performance interop is interesting. But Clojure allows you extend nil with protocols, so I'm assuming it already has some facilities? But maybe won't work for something that need seperate instances of nil.

didibus08:11:32

The idea for example, when you call (get map :key), now both of those return nil: `(get {} :key) (get {:key nil} :key)` So for example, with qualified nils, the former could return: nil/missing-key and the latter just nil

phronmophobic08:11:52

what you're proposing is still an aggregate value of nil and a qualification or meta data on a nil value. clojure already has a way to describe aggregate values: vectors, sets, and maps. metadata seems like a more appropriate way to do what you're describing, but it's not available and I'm pretty sure it's for performance and due to the limitations imposed by interop with java.

didibus22:11:18

Ya, meta-data would work as well. Ya, the performance implication and interop are a good point in terms of feasability for Clojure

didibus22:11:48

Unless it could be handled at compile time, but I don't think it has the information, or that this feature is important enough for it

suren03:11:57

Hi guys, for past few months I have been playing with Clojure and Clojurescript. Its been really fun. Along the way I have created few packages that suits my need. Please check them out. Feedbacks are welcome. https://github.com/ludbek/sql-compose https://github.com/ludbek/validator

👍 3
Ivan Fedorov13:11:26

Is there a bridge from Clojure to Moldable Development? http://youtube.com/watch?v=Pot9GnHFOVU

markaddleman16:11:29

Given a multimethod and its arguments, how can I obtain the result of the dispatch fn? This would be useful for logging purposes

metal 3
vncz16:11:19

That's something I'd also be interested.

markaddleman16:11:44

It turns out that multimethod functions are defined in Java space have have a public dispatchFn field. So, it looks like calling (.dispatchFn -multimethod-) does the trick

markaddleman16:11:17

It returns the dispatch function itself so we can invoke the dispatch fn on the args to get the dispatch value

vncz19:11:34

Seems a lot of hassle, but it is something. Thanks for the pointer!

martinklepsch17:11:08

I’m trying to implement a version of the change-making problem in Clojure with the twist that I’d like to actually have all possible change-combinations returned instead of just the number of different combinations. I found this https://stackoverflow.com/questions/41806819/find-all-combinations-of-a-given-set-of-integers-summing-up-to-a-given-sum but the Haskell and Python solutions make me feel like a terrible imposter 🙈 If anyone feels like adding a Clojure answer (or helping me come up with one) I’d be very thankful 🙂

martinklepsch17:11:24

The Python answer is probably not very Clojure-y but the Haskell answer seems like it might be somewhat translateable:

partitions 0 xs = [[]]
partitions _ [] = []
partitions n (xxs@(x:xs)) | n < 0 = []
                          | otherwise = (map (x:) (partitions (n-x) xxs)) ++ partitions n xs

martinklepsch17:11:01

The piece I don’t quite understand is this I think:

(map (x:) (partitions (n-x) xxs))

rdivyanshu18:11:55

It means find all way to get sum (n-x) form list. And for returned list prepend x to it

martinklepsch18:11:39

ah cool, the (x:) wasn’t quite clear to me but prepending makes sense

rdivyanshu18:11:38

It is haskell version of cons sort of

martinklepsch18:11:00

(defn partitions [n [x & xs :as xxs]]
     (cond
      (zero? n) [()]
      (empty? xxs) ()
      (neg? n) ()
      :else (concat (mapv #(conj % x) (partitions (- n x) xxs))
                    (partitions n xs)) ))

   (partitions 10 [3 4 5])

martinklepsch18:11:16

thank you @U0666D1EK, that was the pointers I needed 🙂

👍 3
Matias Francisco Hernandez Arellano23:11:31

Hey Clojurians. I was able to succesful speak at a meetup with a tan "Clojure 101 for Javascripters" and ea good! Ends with a video/live coding (http://egghead.io style) about building a web server with httpkit and compojure. Now I was invited to another meetup but same talk and I want to give some spin to the content. One thing I miss in the first time was a real example of using the repl (I'm new to Clojure too but less new that the audience) and now I'm thinking in a good example of repl driving development. Do you think that just continue with the server example, run the server un the repl and dinamically add a new route to it will be a good example?

👍 3