This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-02-27
Channels
- # beginners (106)
- # boot (124)
- # cider (11)
- # clojure (105)
- # clojure-poland (2)
- # clojure-russia (28)
- # clojurescript (89)
- # core-async (14)
- # cursive (10)
- # datomic (7)
- # emacs (12)
- # garden (5)
- # hoplon (345)
- # immutant (127)
- # mount (2)
- # off-topic (24)
- # om (24)
- # onyx (8)
- # parinfer (51)
- # proton (2)
- # slack-help (4)
- # spacemacs (1)
when did we get a emothingy?
If I want to hand a JS object over to a macro that generates a defn
for each property in the JS object... how would best I do that?
The macro would be called like this from CLJS: (defcomponents js/MyJSObject)
and if MyJSObject
has the properties foo
and bar
it should define two CLJS functions, (defn foo-component [...] ...)
and (defn bar-component [...] ...)
.
@jannis: At a first look I'd js->clj
the object and map over it?
oh you don't have js->clj
in macro expansion
Well, I can do '(let [components# (~'js->clj ~components)] ...)
in the macro (where '
is the backtick) but after that?
I can even get the macro to generate (do (clojure.core/defn Button-component [] foo) (clojure.core/defn Card-component [] foo) (clojure.core/defn Heading-component [] foo) (clojure.core/defn Layout-component [] foo) (clojure.core/defn Text-component [] foo)))
for me but then I'd still need to evaluate it somehow for the defn
s to take effect.
@jannis: that's an interesting problem. Since the compiler is Clojure, there is really no JavaScript engine in sight be there to help with the runtime aspects (apart from Nashorn). … hmm
@jannis: whole different thing if using bootstrapped compiler from within JavaScript, though
Yeah, then I could just have the compiler generate JS from the generated form, I suppose?
I have no idea how the bootstrapped compiler works or in what form it is available. I know it exists because people talk about it, that's all 😉
@jannis: Snoop around this project to see how David was wielding Nashorn to do things with JavaScript from Clojure
Another idea: If there was a way to apply js->clj
before the data is passed to the macro then that might work too. My understanding of data readers is limited, could I do something like (defcomponents #clj js/MyJSObject)
?
@jannis: at the core of this is the separation of runtime (where JavaScript exists) and compile time
Yeah. I've written macros before that generated CLJS code - but from CLJ symbols and expression lists, not from JS objects that are unknown at compile time.
@jannis: so if you can start Nashorn (from a macro!), start bootstrapped ClojureScript inside of it, then you can do anything :)
https://gist.github.com/Jannis/ede3a929833798d7f2b3 - this is my macro at this point
@jannis: Paul Graham has a cool essay talking about Lisp freely mixing things up. Bootstrapped ClojureScript leads to that ability.
@mfikes: I have to admit jsx-fun is over my head right now. I see nashorn evaluating JS but I'm not sure how that helps in this case.
@dmitrig01: right. You can have intern
in bootstrapped ClojureScript, fwiw https://github.com/mfikes/planck/blob/284572b4b3acf7f59cd3e99485a6b9d53d303fc1/planck-cljs/src/planck/core.cljs#L217
it's always an exercise to figure out how to solve a problem like this without a macro if possible
@jannis: right, using Nashorn from within a macro to run bootstrapped ClojureScript is definitely at the deep end of the pool :) but it sounds fun
I assume Nashorn can only evaluate JS? So something would have to compile the generated CLJS expressions to JS before Nashorn can process them?
@dmitrig01: I fixed the link.
@jannis: right. I bet you can use the compiler from within a macro. Lots of interesting things could be done. Definitely a bigger problem than the one you set out to solve
I've got Nashorn to eval "5" and return the value to CLJS. Now I just need to eval ClojureScript 😉
@jannis: indeed. You can load the ClojureScript runtime into Nashorn just like any other JavaScript target
@jannis: and you can compile additional ClojureScript firms to JavaScript to be executed in Nashorn
@jannis: it's probably very hacky, but I could see using macroexpansion in bootstrapped ClojureScript to generate your defn forms, etc. at that point you are cobbling together a compiler within s compiler. Hmm
@jannis: if you are just doing this as a one-off, to generate code, maybe you could use an existing bootstrapped ClojureScript environment to do it
I wouldn't say it's a one-off. I have a JS React component library and I want to generate a function for each of the components that takes props and children and returns the result of (js/React.createElement <component> #js <props> <children>)
.
My assumption was that the best way to approach this was to define these functions in the client after it has required the library via :foreign-libs
. It exports a single JS object whose keys are component names and whose values are their implementations (`<component>` in the above snippet).
So I thought I'd take that library object and generate the defn
s for its components on the fly.
I wonder... instead of receiving the JS object from CLJS, could I instead load the corresponding .js
file from classpath with Nashorn, evaluate it, get the library object and generate CLJS code from it? So that I can then just use (defcomponents "my-library.js")
in CLJS?
Perhaps not: :message ReferenceError: "window" is not defined in <eval> at line number 19320 at line 11
Hi, suppose I have N objects and M operations (some of which are doing network I/O). I want to call the sequence of operations in order for each of the N objects but allowing parallelism (across the objects) where possible. There is one synch (fan-in) point in the pipeline let's say at operation M-1. What's the best/easiest way to do this in core.async?
@johanatan: cats or promesa's alet
will do this for you.... dunno about core.async based solutions
@johanatan: I wrote my own helper to get the equivalent of the Promise.all()
fn you find in js promise libs
@mccraigmccraig: ahh cool. I'll take a look. Thx!
@val_waeselynck: promise.all() would be only one aspect of the solution here though no? Core.async already has into
for that.
well you asked for a core.async based solution
you could also look into pipelynce-async
i guess
What are the translation rules to Javascript for clojure ^export
functions. -
becomes _
but what about all the other special characters??
kmandrup: good question, I wonder if it follows the munge rules?
kmandrup: clojure uses a function called munge
it has a matching function called demunge
(the names here are historical, names for automated name translations)
kmandrup: those functions are available in the repl, and have doc strings, etc. in clojure
the idea is that by having that job done by a function, you can accurately do the same translation you expect the compiler to do (in both directions)
Cool I found this: http://stackoverflow.com/questions/22362540/internal-clojure-function-to-convert-non-alphanumeric-symbols-to-java-class-name
kmandrup: and as I expected, cljs uses the same function name (different implementation) https://crossclj.info/ns/receipt/1.0.1/cljs.compiler.html#_munge
Thanks I'm trying to add some basic docs to Datascript https://github.com/tonsky/datascript/wiki/Javascript-API
Is there a way to generate a list of the fully exported Javascript API? There ought to be... just metadata after all
@kmandrup: I’d take a look at how cljs.test
finds all of the test functions as described in the bottom of http://swannodette.github.io/2014/12/17/whats-in-a-var/ and modify the approach to look for ^:export
metadata instead
And, yes, munge
is definitely involved. Planck imitates it to call fns: https://github.com/mfikes/planck/blob/3eff90a881cb4df0ee32813cc02f1f3c245bbb4b/planck/PLKClojureScriptEngine.m#L1131-L1136
@mfikes: why imitate it instead of using it?
For whoever missed it, here is some ClojureScript light in the React.js recent conference,good job @jaredly! https://t.co/eRTJI5p08g
@noisesmith: yes, clients could hard-code the JavaScript needed to call munge
and just call it. Not a bad idea.
oh, I thought you had an interesting reason I had not thought of and I might be about to learn something, heh
Howdy peoples, what would you say its the first boilerplate or tutorial you would recommend for someone starting with clojurescript
nashio: figwheel starter / demo is awesome
figwheel is a great tool, and it's good to start by just forking the demo
Anyone interested in starting a Visual Studio code extension for Clojure via https://github.com/clojure/tools.analyzer ??
Visual Studio code looks to me like an IDE/editor that hits the "sweet spot" and is easy to extend... 😉
@noisesmith: thanks