r/Clojure • • Mar 19 '20

Wow... it worked!

https://twitter.com/borkdude/status/1237774773813481474
73 Upvotes

9 comments sorted by

7

u/jjttjj Mar 20 '20

I'm really loving sci, and it's really cool seeing the wide range of uses it can have. I feel like it has a lot of untapped potential that I haven't quite wrapped my brain around yet.

I've messed with using it as a sort of client->server "query language" where you build up code structures in cljs, then send them as a string to a server, which hands the code string off to sci with whitelisted functions you want to allow. So instead of making an endpoint for every operation, you just have one endpoint that processes your sci strings, and you expose regular functions via `:allow``. What's more is you can easily use clojure language facilities to do aggregation and data processing ON the server but FROM the client. At the very least, this feels really nice to do during an exploratory phase, you can always create real/normal endpoints once the operations/functions you need are more solidified.

I also think it has a lot of potential as a cljs interpreter with a sort of real eval that is smoother than bootstrapping cljs in many situations.

2

u/flaheadle Mar 20 '20

You could even type these strings into a text box in the frontend, and save a history of them too. I'm considering it.

7

u/didibus Mar 20 '20

Embedded Clojure, I love it!

6

u/tincholio Mar 20 '20

I am in awe of /u/borkdude 's productivity, he rocks!

1

u/olymk2 Mar 20 '20

so does this basically mean clojure with out java, just native execution ?

1

u/didibus Mar 21 '20

A subset of Clojure yes.

You can try it out right now by using babashka here: https://github.com/borkdude/babashka

Depending how you were using the word native though, your code won't be compile to native, it is running interpreted and will run more slowly than with Clojure JVM, but you won't need a JRE to run it, you will just need the babashka binary.

P.S.: Also, a more native version of Clojure is to use Graal Native to compile your Clojure code. In which case, you will not need a JRE or any other binary to run it after. Though you will need a JRE (the Graal one) to develop it.

1

u/olymk2 Mar 21 '20

cool i was aware of graal and babashka, so basically you have take the clojure coded run it through graal to create a clojure binary that works with out the jvm ?

so that you can run code via that binary with out the jvm, or am i mis-understanding still also that feels like it would run faster or is it basically because its a bit of a hack ?

1

u/didibus Mar 21 '20 edited Mar 21 '20

so basically you have take the clojure coded run it through graal to create a clojure binary that works with out the jvm ?

Yup, with Graal, you can compile Clojure code to a native binary for Linux, Mac or Windows. Same as if you were using Go-lang for example.

The only catch for now is that there are certain things that will fail to compile. So you're kind of restricted to a subset of Clojure, it's a pretty big subset though, and Graal keeps improving trying to support more and more.

This wiki lists some of those restrictions and ways to workaround some of them: https://github.com/lread/clj-graal-docs/blob/master/README.md

also that feels like it would run faster or is it basically because its a bit of a hack ?

No, contrary to what most people think, ahead of time compilation often performs worse than just in time, because just in time has more information available to it to perform optimization. That said, it will start way faster, like almost instantly, since there is nothing to compile, you can just start executing the compiled code right away.

I'd say Graal seems to do a pretty good job at optimizing the native binary though, so it should still be pretty performant.

Also, no hacks involved here. This isn't a bundler. It's a full on native compiler. Though instead of going Clojure -> native machine code. It goes: Clojure -> Java byte code -> native machine code.

1

u/olymk2 Mar 21 '20

sounds awesome will give it a try when i get a chance :)