This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-04-02
Channels
- # arachne (1)
- # bangalore-clj (3)
- # beginners (4)
- # boot (6)
- # cider (3)
- # cljs-dev (14)
- # cljsrn (2)
- # clojure (309)
- # clojure-ireland (1)
- # clojure-russia (7)
- # clojure-spec (10)
- # clojure-uk (5)
- # clojureremote (2)
- # clojurescript (68)
- # cursive (7)
- # emacs (7)
- # luminus (3)
- # lumo (21)
- # off-topic (14)
- # om (1)
- # onyx (53)
- # perun (3)
- # re-frame (4)
- # slack-help (14)
- # unrepl (56)
- # untangled (3)
Question: How do you convert a string to a number in ClojureScript? I found myself dropping into the host e.g. js/Number
or js/parseInt
...but what's the Clojure way of doing this?
@U47KWM4UV I'd say that's the right way. I don't know ClojureScript, but I do Clojure proper, and it too, relies on using interop to parse strings into host datatype. You'll come to realize that in Clojure and dialects, it is idomatic to embrace the host platform, and not re-invent the wheel when it does a perfectly fine job at the task. A wrapper around js/Number would add nothing, except changing the name of the function. That said, there's a neat trick, for parsing things into Clojure datatype, use clojure.edn/read-string. This will parse string as the Clojure reader would. I think it is cljs.reader/read-string in ClojureScript. read-string parses text as EDN and returns Clojure/Script datatypes. So you can do:
(require '[clojure.edn :as edn])
(type (edn/read-string "23489"))
Not a cljs expert, but I would say you're doing it fine. Even the clojurescript source uses that method https://github.com/clojure/clojurescript/blob/d0be39660f3a65422c3de6a774ceec0b6a863ee2/src/main/cljs/cljs/stacktrace.cljc#L33