This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-31
Channels
- # adventofcode (15)
- # announcements (8)
- # babashka (16)
- # beginners (48)
- # braveandtrue (5)
- # calva (54)
- # cider (7)
- # clara (8)
- # clj-kondo (3)
- # cljdoc (3)
- # clojure (37)
- # clojure-europe (1)
- # clojure-italy (15)
- # clojure-losangeles (2)
- # clojure-nl (15)
- # clojure-uk (6)
- # clojurescript (145)
- # community-development (53)
- # cursive (20)
- # data-science (8)
- # datomic (23)
- # duct (2)
- # emacs (22)
- # fulcro (16)
- # gorilla (7)
- # graalvm (7)
- # hoplon (1)
- # malli (7)
- # off-topic (8)
- # rewrite-clj (2)
- # ring (9)
- # spacemacs (2)
- # specter (1)
- # tools-deps (25)
- # vrac (1)
Just wondering, is there an equivalent to vector-of (http://clojuredocs.org/clojure.core/vector-of) in clojurescript? Or is the only option to use arrays? Perhaps a library?
I have looked semi-carefully for such a thing, and believe there is nothing in ClojureScript like that. Are you hoping to get some reduction in memory usage for large immutable vectors in cljs? Or do you have some other motivation for wanting such a thing?
I am semi-actively working on a replacement for the core.rrb-vector library with hopefully fewer bugs. If someone figures out a use case and an implementation strategy for such a thing in ClojureScript, I could consider adding it to my core.btree-vector library, with a cljs as well as a Clojure/JVM version.
I want fast unboxed operations @U0CMVHBL2 with vector like semantics :)...
I wrote this: https://github.com/Folcon/delaunator-cljc
Trying to improve performance here:
https://www.reddit.com/r/Clojure/comments/eh6gu9/help_for_making_this_fast/
I've had little success on the cljs side. My next step is to just throw up my hands and use int-array
's, which is really annoying, but that appears to be the only viable performance improvement in clojurescript.
@U0CMVHBL2 What do you need? Not sure how helpful I can be =)…
I don't currently know very much about JavaScript runtime engine details. Do they even have a distinction between boxed vs. unboxed numbers, the way JVMs do?
The other thing I’ve been thinking of is writing vector-of in cljs by working out how to support the api…
Clojure/Java has the notion of regular vectors, i.e. immutable vectors where every element is an arbitrary object/value, as does ClojureScript. Java has arrays of Object or any JVM primitive type like long/double/etc., as I guess does JavaScript. In Java, arrays of long/double/etc. take significantly less memory than arrays of Object, if those arrays are restricted to contain only long/double/etc.
Are there JavaScript arrays that are restricted to contain only numbers, and cannot contain other types of values as their elements?
And is the way to declare them consistent across different JavaScript runtime engines?
If so, then introducing vector-of into ClojureScript is certainly possible, and potentially useful to some people. And I would guess could be done as part of a 3rd party library, as it can be done in Clojure/Java.
If you’re aware of this, what’s the way of doing something like this in clojurescript? This sounds like a valid next step =)…
For clojure I’d just have to use deftype I believe and then add methods, I assume it’s the same in cljs?
If you want to take a crack at it (sounds like you would get to it before I would), I would recommend reading the Clojure/Java implementation of vector-of in the file src/clj/clojure/gvec.clj of the https://github.com/clojure/clojure code, and ask questions if you have them -- happy to tell you what I know about it. I am not currently much help with ClojureScript/JavaScript interop details, as I haven't done that before.
Folks in the #clojurescript and/or #cljs-dev channels would be more knowledgeable on the ClojureScript/JavaScript interop than I, if you get stuck on that.
OK, heading to bed right now, so incommunicado for some number of hours starting now -- not ignoring you 🙂
I need to use refs with reagent. I tried the below code :
(ns bharati.binita.frontend.demo2
(:require [reagent.core :as reagent]
[react-simple-keyboard :default Keyboard]
))
(defn create-wrapper [input]
(let [comp-state (reagent/atom {:layoutName "default" :input ""})
keyboardRef (clojure.core/atom nil)]
(reagent/create-class
{:display-name "bini-wrapper-component"
:component-did-mount (fn [this]
(js/console.log (str "component-did-mount: entered. Need to set keyboard ref here!")
))
:reagent-render (fn []
(js/console.log (str "reagent-render: entered with "@comp-state))
[:div
[:input {:value (@comp-state [:input])
:placeholder "Tap on the virtual keyboard to start"
:onChange (fn [event]
(js/console.log (str "onChangeInput: entered with" event.target.value))
(swap! comp-state assoc :input event.target.value)
(js/console.log (str "onChangeInput: keyboardRef = " @keyboardRef))
(.setInput @keyboardRef event.target.value)
)
}]
[:> Keyboard {:layoutName "default"
:onChange (fn [input]
(js/console.log (str "Keyboard -> onChange: entered with " input))
(swap! comp-state assoc :input input)
)
:onKeyPress (fn [button]
(js/console.log (str "onKeyPress: entered with " button))
)
:ref (fn [myRef]
(js/console.log (str "Before setting keyboard ref, keyboardRef = "@keyboardRef))
(reset! keyboardRef myRef)
(js/console.log (str "After setting keyboard ref , keyboardRef = "@keyboardRef)))
}]
]
)
})
))
(defn run []
(reagent/render [create-wrapper]
(js/document.getElementById "root")))
This build fine with shadow-cljs, but shows the following error on browser console :
react-dom.development.js:531 Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `bini-wrapper-component`.
in Unknown (created by bini-wrapper-component)
in div (created by bini-wrapper-component)
in bini-wrapper-component (created by bharati.binita.frontend.demo2.create_wrapper)
in bharati.binita.frontend.demo2.create_wrapper
So, the error seems to be from using :ref within the Keyboard component. How do I go about fixing this ?
Thanks.It's right there, in the Keyboard
component documentation, before the description of the setInput
method that you're using:
https://franciscohodge.com/projects/react-simple-keyboard/documentation/#methods
@U2FRKM4TW - Sorry, did not quite get you. Before the description of setInput
, there is a getInput
method being described. Actually , I have a working React JSX code for the same, which I am trying to convert to Clojurescript. How would I get a reference to the Keyboard
in cljs ? Once, I have a reference, I could probably invoke whatever methods are supported on it. Thanks.
The code block right after the sentence "To access these functions, you need to get the instance of simple-keyboard, like so" in the documentation:
<Keyboard
keyboardRef={r => this.keyboard = r}
[...]
/>
// Then, on componentDidMount ..
this.keyboard.methodName(params);
Note how it uses keyboardRef
instead of just ref
.BTW in Reagent, you can specify such keys either as :keyboardRef
or :keyboard-ref
- both ways works.
Thank you so much ! Its working . Have replaced the incorrect property name ref
with keyboardRef
I got a quick question, I'm fairly new to ClojureScript, I really like it, however I still struggle with converting code to CLJS from JavaScript. Can anyone show me how would I convert this code into CLJS?
const useStyles = makeStyles(theme => ({
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}
}));
Verbatim:
(def use-styles
(makeStyles
(fn [theme]
#js {:paper #js {:marginTop (. theme (spacing 8))
:display "flex"
:flexDirection "column"
:alignItems "center"}})))
(def use-styles
(makeStyles
(fn [theme]
#js {:paper #js
{:backgroundColor "blue"}})))
(defnc main-layout [{:keys [children]}]
(let [[classes] (use-styles)]
[mui/Container {:component "main" :maxWidth "xs"}
[mui/CssBaseline]
[:div {:className (. classes main)} "lets see if it works"
children]
]))
Let me guess - it throws an error about not being able to use hooks within class components?
(let [[classes] (use-styles)] ...)
- why do you destructure the result of (use-styles)
?
export default function SignUp() {
const classes = useStyles();
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Avatar className={classes.avatar}>
<LockOutlinedIcon />
</Avatar>
(.main classes)
calls the function stored in the main
field of the JS object classes
. (.-main classes)
just returns the field's value.
Just make sure you're not calling clj->js
and js->clj
all over the place, except for during macro expansion.
Maybe this will be of use, especially for turning (. classes -main)
into something like (:main classes)
: https://github.com/mfikes/cljs-bean
Exactly what it says in its description. :) It's faster than doing clj->js
in the cases when it can be used at all.
I wrote this: https://github.com/Folcon/delaunator-cljc
Trying to improve performance here:
https://www.reddit.com/r/Clojure/comments/eh6gu9/help_for_making_this_fast/
I've had little success on the cljs side. My next step is to just throw up my hands and use int-array
's, which is really annoying, but that appears to be the only viable performance improvement in clojurescript.
Quick question, if I’m extending the clojurescript protocols to js types, such as Uint32Array
, is there a specific way of doing that beyond just calling:
(extend-protocol IAssociative
js/Uint32Array
(-assoc [coll k v]
(aset coll k v)
coll))
and then requiring them in another namespace?As probably extending the built-in types. At least, I've seen multiple people say that it shouldn't be done.
Well perhaps, I’m trying to get vector-of
like semantics…
So basically wrapping primitives, which is available in clojure, but not clojurescript…
@U050ECB92, I’m not saying this is great, but trying to write cljc compatible code is really frustrating as cljs-core does not support this even though clj-core does…
I’m not finding much stuff around working with primitives without dropping down to raw-arrays…
Fair enough, irrespectively, I’m treating these as persistent in my code. Part of this is trying to bring the clojurescript side of my code in line performance wise with the clojure side… I wrote my algo with vectors, now I’m trying to make it more performant, and I don’t know how to write persistent datastructures…
I can’t do much until this get’s implemented in core, or someone who knows what they’re doing writes a lib, until then I’ll have to do this sort of thing…
I mean if you have a thing that you can point to that isn’t just lots of java or javascript that I can look at to work out how to do this great! I’ll use that instead…
But until then I’d rather get code working until the situation becomes better, instead of throw up my hands and give up until someone writes it…
yeah if you’re treating it persistently that’s cool. I had to pipe up bc no one else was questioning whether this was a compatible impl with assoc
right, but now we’re getting into writing custom functions as opposed to being able to treat it like a seq
it doesn’t exist here, so I’m trying to put something together which gives me similar semantics…
I see. Maybe you could steal the impl of PV in clojurescript but put typed arrays at the bottom?
I’ve been digging into the internals, but I’m still really unfamiliar with stuff at this level, usually I’m writing application level code =)…
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L5336
would this be a case of create a new deftype
similar to PersistentVector
? and then do a similar thing?
great, let’s give that a go =)… Thanks @U050ECB92
I mean they'd only speed up inserts and updates to a vector. If what you want is to store primitive values in a vector that's different