This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-03
Channels
- # aleph (2)
- # announcements (13)
- # babashka (7)
- # beginners (36)
- # calva (26)
- # cider (11)
- # circleci (13)
- # clj-kondo (15)
- # clojure (105)
- # clojure-europe (79)
- # clojure-nl (3)
- # clojure-uk (6)
- # clojurescript (17)
- # conjure (4)
- # core-logic (2)
- # cursive (10)
- # data-science (5)
- # datalevin (11)
- # datalog (14)
- # eastwood (6)
- # emacs (2)
- # figwheel-main (1)
- # fulcro (34)
- # google-cloud (1)
- # graphql (3)
- # introduce-yourself (7)
- # jobs (1)
- # leiningen (17)
- # lsp (46)
- # malli (2)
- # minecraft (3)
- # missionary (19)
- # off-topic (31)
- # other-languages (49)
- # polylith (2)
- # portal (5)
- # practicalli (1)
- # quil (77)
- # releases (1)
- # remote-jobs (1)
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) ...))
> 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).
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".
> first
might not be the right option
that's right. The snippet is just an illustration.
if you're building an app, extending core types is probably fine. make sure you don't do it in a library though!
what’s the best way to check whether every character in a string is a digit?
@U051F5T93 That relies on a JVM class and won't work in ClojureScript, right?
In JavaScript, you’d write /^\d+$/.test(str)
, I’m not sure how to do that with re-matches
@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.
Ah, didn’t realize re-matches was whole string. Thank you for the clarification
“12764” “236235" etc