Fork me on GitHub
#clojurescript
<
2018-09-01
>
lvh01:09:54

I'd like to create a diagramming tool in cljs with a datascript backing. reagent producing svg seems like the obvious place to start. Any prior art I should look at? More details: I'm trying to write a home wiring diagramming tool on steroids: imagine switches, cabling, etc 🙂

lvh01:09:42

(I appreciate that there's a commercial datascript-backed project that does this for datacenters)

idiomancy20:09:36

is there any good way to make components that have [& args]? I want to design my components so that they follow the hiccup convention of being able to do either

[nav-component
 [button-component "sign up"]
 [button-component "log in"]
 [button-component "about"]]
or
[nav-component
 (for [label ["sign up" "log in" "about"]]
   [button-component label])]

gaverhae20:09:04

I guess you'd have to manually check whether args has a list in its list?

idiomancy20:09:04

oh... you know actually I'm cool with just

(into [nav-component]
    (for [name ["hey" "check" "these" "buttons"]]
        [button-component name]))

idiomancy20:09:12

which apparently works

jsa-aerial20:09:01

In ClJS, Is there a way to programmatically get the value of a symbol? Since there are no vars I think the answer to this is probably 'no'. Looking around seems to indicate this is correct - can't find anything that would do the job. I can think of a way to cheat to achieve the same thing (fixed, limited number of such symbols), but if there really is a way out of the box...

mfikes20:09:19

@jsa-aerial If :advanced is being used, then there’s not much you can do. On the other extreme, self-hosted ClojureScript is closer to Clojure with respect to runtime behavior.

mfikes20:09:35

Depending on what you are really doing, you might be able to get away with code like

cljs.user=> (def a 3)
#'cljs.user/a
cljs.user=> @(resolve 'a)
3

jsa-aerial21:09:14

@mfikes Thanks - figured as much. I think the simplest is to just create my own lookup table. I'm lucky enough that there is a fixed up front known set of symbols that I need this for, so I can just create a simple map...

mfikes21:09:24

Cool. And FWIW, symbols work like keywords in that they can look themselves up in a map.

jsa-aerial21:09:52

Right - the map is entries of the form [sym symval] where this is created from a quoted list of the syms and a list of the syms.

jsa-aerial21:09:54

Seems to be working fine - thanks to #specter again as well!

mfikes21:09:21

If it turns out that your symbols are associated with the public vars in a given namespace, ns-publics could be useful. Otherwise, building a fixed-size map sounds like the right solutions.

jsa-aerial21:09:46

@mfikes Actually, that could be useful - many thanks!

mfikes21:09:07

No problem… I’ve written some code like that, and since you are given the vars, you can even filter that map down to a submap that only has entries where the vars have some custom meta on them. For example, (def ^:included a 3)

jsa-aerial21:09:06

@mfikes Yeah - wow, that looks basically like the exact solution, thanks again!

mfikes21:09:00

This could be useful when exposing certain functions in a namespace for external invocation, or other such things. The nice thing is that it boils down to you simply adding some meta on the vars you want included.

mfikes21:09:46

The negative aspect is that it ends up creating a bit of var bloat in the JavaScript, but it is a tradeoff.

jsa-aerial21:09:25

Hmmm, I need the value not the var - since the vars are gone at runtime - or is there a way to get the value from the var at comp time?

jsa-aerial21:09:58

Oh - this is a 'special way' to get vars at runtime?

mfikes21:09:28

If the vars are associated functions, you can just use them directly. But if the vars are somethings else (like numbers), then you can deref them.

mfikes21:09:28

Here is an example of those ideas:

cljs.user=> (defn foo [x] (inc x))
#'cljs.user/foo
cljs.user=> (#'cljs.user/foo 3)
4
cljs.user=> (def bar 10)
#'cljs.user/bar
cljs.user=> @#'cljs.user/bar
10

mfikes21:09:55

The vars are not completely gone at runtime.

jsa-aerial21:09:11

Ahh - so these vars work more or less like real Clojure vars?

mfikes21:09:04

In a lot of static ways, they work like real Clojure vars.

✔️ 4
jsa-aerial21:09:55

Given this info - I think ns-publics gives the required xref map without need of 'manually' constructing it. This is all really useful - @mfikes thanks for all this great info.

👍 4