Fork me on GitHub
#clojurescript
<
2022-01-03
>
Drew Verlee01:01:52

That's an interesting question. Does extending a protocol somehow slow down the data structure at all? What do you want (pop []) to return? nil? Are you suggesting the change be made to the cljs core or are you asking if this is a good idea in general? I think in the later case then my gut is that i would want (pop []) to return nil to. I feel like it just allows for slightly more concise code in some cases because you can use if-let and when-let (when-let [x (pop s)] (...)) as opposed to (when (seq s) (let [x (pop s) ...))

phronmophobic02:01:44

> I think in the later case then my gut is that i would want (pop []) to return nil to. I feel like it just allows for slightly more concise code in some cases because you can use if-let and when-let There are some tradeoffs here. I believe clojure has chosen difference tradeoffs and I would implement core protocols such as peek and pop to be consistent with the clojure core data structures. See https://clojure.org/reference/lazy#_the_victim_nil_punning. I don't see anything wrong with your implementation of peek and pop using first and rest since that's essentially how List is implemented (see https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L3113). I'm not that familiar with IndexedSeq and Cons, but I think your example looks ok. Just note that first might not be the right option for peek depending on the data structure (See https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L672).

misha17:01:50

I think popping [] gonna throw just to keep backward compatibility. But popping nil is a precedent that there are cases where it does not blow up, and therefore it is sort of ok for any empty collection not to blow up on pop. My question is both "am I not considering some unintended consequences by extending these coll types?" and "was it just overlooked in cljs.core, or there is an actual reason those don't implement stack".

misha17:01:46

> first might not be the right option that's right. The snippet is just an illustration.

lilactown18:01:48

if you're building an app, extending core types is probably fine. make sure you don't do it in a library though!

lilactown18:01:10

tbh I've only ever used peek/pop on a queue

zendevil.eth19:01:38

what’s the best way to check whether every character in a string is a digit?

Kelvin19:01:57

Generally you would want to use a regex, e.g.

(re-matches #"[0-9]*" "12345")

kardan19:01:08

Or

(every? #(Character/isDigit %) "12345")

seancorfield19:01:42

@U051F5T93 That relies on a JVM class and won't work in ClojureScript, right?

kardan19:01:06

Meh, true 🙂

kardan19:01:32

Thanks for correcting me

kardan19:01:33

Didn’t think about that we were in the ClojureScript channel

Noah Bogart19:01:17

In JavaScript, you’d write /^\d+$/.test(str), I’m not sure how to do that with re-matches

simongray08:01:58

@UEENNMX0T Like how @U02FU7RMG8M wrote. The ^ and $ characters become superfluous when using matches. You could also write \d instead of [0-9] like you did. The choice between + or * is up to @U01F1TM2FD5, i.e. allow "" or not.

👍 1
Noah Bogart15:01:00

Ah, didn’t realize re-matches was whole string. Thank you for the clarification

zendevil.eth19:01:02

“12764” “236235" etc