r/Clojure • u/fingertoe11 • Jul 06 '18
Terrific "Why ClojureScript" presentation.
https://www.youtube.com/watch?v=81_p6PSu0642
2
2
u/jacekschae Jul 07 '18
I would even argue that using shadow-cljs (npm package) is super easy to use npm ecosystem and there is no need for cljsjs.
Shameless Plug - you can see how it all works in this course > https://www.learnreagent.com
1
u/pinkyabuse Jul 07 '18
Great presentation!
For those interested, here is the link to the cljs-for-js-devs-starter.
I'm interested in compile to JS languages such as cljs, elm and Reasonml. Clojure is a beautiful language but I'm preferring elm because I like the idea that if it compiles, it works. That is to say, there are no run time errors due to type safety.
The trade-off with Elm is that it's more verbose and the output js is larger. And compared to Reasonml, compile times are slower. Some of these issues may be resolved in the next version of Elm.
7
u/yogthos Jul 07 '18
One big advantage of using ClojureScript is having seamless interop with the Js ecosystem. For example, I'm using React Semantic UI in a project currently. Here are the steps to add it to the project and instantiate some components:
add the dependency from cljsjs
[cljsjs/semantic-ui-react "0.81.1-0"]require it as a namespace
(:require [cljsjs.semantic-ui-react :as ui])make some Reagent components:
(def menu [:> ui/Menu [:> ui/MenuItem {:name "item 1" :onClick #(js/alert "item 1 clicked")}] [:> ui/MenuItem {:name "item 2" :onClick #(js/alert "item 2 clicked")}]])Meanwhile, my experience is that dynamic typing is problematic in imperative/OO languages. One problem is that the data is mutable, and you pass things around by reference. Even if you knew the shape of the data originally, there's no way to tell whether it's been changed elsewhere via side effects. The other problem is that OO encourages proliferation of types in your code. Keeping track of that quickly gets out of hand.
What I find to be of highest importance is the ability to reason about parts of the application in isolation, and types don't provide much help in that regard. When you have shared mutable state, it becomes impossible to track it in your head as application size grows. Knowing the types of the data does not reduce the complexity of understanding how different parts of the application affect its overall state.
My experience is that immutability plays a far bigger role than types in addressing this problem. Immutability as the default makes it natural to structure applications using independent components. This indirectly helps with the problem of tracking types in large applications as well. You don't need to track types across your entire application, and you're able to do local reasoning within the scope of each component. Meanwhile, you make bigger components by composing smaller ones together, and you only need to know the types at the level of composition which is the public API for the components.
REPL driven development also plays a big role in the workflow. Any code I write, I evaluate in the REPL straight from the editor. The REPL has the full application state, so I have access to things like database connections, queues, etc. I can even connect to the REPL in production. So, say I'm writing a function to get some data from the database, I'll write the code, and run it to see exactly the shape of the data that I have. Then I might write a function to transform it, and so on. At each step I know exactly what my data is and what my code is doing.
Where I typically care about having a formalism is at component boundaries. Spec provides a much better way to do that than types. The main reason being that it focuses on ensuring semantic correctness. For example, consider a sort function. The types can tell me that I passed in a collection of a particular type and I got a collection of the same type back. However, what I really want to know is that the collection contains the same elements, and that they're in order. This is difficult to express using most type systems out there, while trivial to do using Spec. This article has a good comparison of the two approaches with a small real worldish example.
3
u/zogulus Jul 07 '18
you can mitigate a lot of those concerns with good test coverage and if you're feeling a bit more adventurous then have a look at spec
1
u/yen223 Jul 07 '18
The bigger problem with Elm is that it doesn't interact well with the rest of the JS ecosystem. Both ClojureScript and ReasonML do a better job with interop
1
u/FuZzyPImp Jul 08 '18
I like Elm a lot, but I'm looking at alternatives like Clojurescript. I find it difficult to build the abstractions I want in Elm.
1
u/eccentric_j Jul 09 '18
Does anyone know what tools the speaker is using to send his vim code to the repl like that?
5
u/yogthos Jul 07 '18
Great talk, the only note I'd add is that starting with ClojureScript doesn't need to be hard at all if you're using Leiningen. All you need to do is run:
lein new reagent-frontend myapp, and that will set up a skeleton app that's ready to go for you.