Live data from Hacker News

Clojure by Example (2015)

kimh.github.io

11–20 of 46 posts

Re: Clojure by Example (2015)

#11
The language isn't what's difficult to grasp or what needs more explanation.

What's missing is current complete examples with the library choices and configuration all worked out.

Re: Clojure by Example (2015)

#12
post #11

The language isn't what's difficult to grasp or what needs more explanation. What's missing is current complete examples with the library choices and configuration all worked out.

I wholeheartedly agree. There are so many different ways to set up a project in Clojure that it can be a little overwhelming. There is boot, leiningen and deps.edn for managing dependencies as well as project structure. Then with ClojureScript there is shadow-cljs as well as improvements made with ClojureScript embracing the Javascript tooling by handing off the output of the compiler to a JS bundler.

Re: Clojure by Example (2015)

#13
There are a few things I would like to see added as I have found them very useful in day to day work with Clojure(Script).

  cond-> and cond->> which takes an expression and then threads it through test/form pairs without short circuiting by stopping on the first test that returns true. I have used this a lot when conditionally adding to maps. Something like this:

  (let [base-map {:id "123"}
      name (function-that-could-return-nil)
      address (another-function-that-could-return-nil)]
   (cond-> base-map
      (some? name) (assoc :name name)
      (some? address) (assoc :address address)))

  If name and address are nil then I will still have the base-map.


  I also use some-> and some->> a lot as well. some-> and some->> operate similar to -> and ->> respectively but the difference is that there is a nil check before subsequent form. It will prevent nil results to keep being passed and can help alleviate NullPointerExceptions.

  As for maps, I recommend adding assoc-in, dissoc, update and update-in. assoc-in allows adding nesting to a map. I could do (assoc-in {} [:base :value] "test") which would result in a map that looks like {:base {:value "test"}}. dissoc removes a key from a map. update allows you to "update" a map by passing a key and function where the function has an argument that is the old argument. An example of that is (update {:name "James" :age 26} :age inc) which will increase the value of :age by 1. update-in allows you to do the same as update but in a nested structure

Re: Clojure by Example (2015)

#14
post #7
post #5

> Clojure doesn't provide built-in function for exponential operation It does actually: (Math/pow 2 3) ;;=> 8

“Math” here is short for “java.lang.Math” though. Technically that’s interop rather than Clojure per se. /pedantic

I think for Math/ it's a bit more fuzzy, because it doesn't need to be aliased explicitly, it's always available to use as it is automatically imported as Math.

And this is then consistent in ClojureScript as well.

Re: Clojure by Example (2015)

#16
post #13

There are a few things I would like to see added as I have found them very useful in day to day work with Clojure(Script). cond-> and cond->> which takes an expression and then threads it through test/form pairs without short circuiting by stopping on the first test that returns true. I have used this a lot when conditionally adding to maps. Something like this: (let [base-map {:id "123"} name (function-that-could-re…

I see this kind of map update code usually written using merge, like this (ignoring DRY itch for now)

  (merge base-map
     (when name
        {:name name}
     (when address
        {:address address})
Or to avoid repeating field names, just form one map to be merged like {:name (function-that-may-return nil) :address (function-2)} and filter out nils from the map before merging that with-base-map.

I guess with cond-> you have the advantage that you can do arbitrary map transformations, not just additions, so maybe this is just a case of nitpicking a strawman example - but doing only additions is a common case too.

Re: Clojure by Example (2015)

#18
post #6

Learning a new language by mapping constructs from another language you know better is certainly expedient. But the trade off is you short change yourself on learning core philosophical beliefs underpinning the design of the language. Understanding these poorly means you will not exploit the language very well and arguably should just stick with the prior language. Clojure more than most languages is built around som…

Are there any other resources that you'd recommend to people coming from languages like Python or JavaScript? I'm assuming 'Clojure for the Brave and True'?

Re: Clojure by Example (2015)

#19
post #8
post #5

> Clojure doesn't provide built-in function for exponential operation It does actually: (Math/pow 2 3) ;;=> 8

The very next sentence of the article points out that you can leverage Clojure's interop to invoke Math.pow(), but that's still limited to casting its arguments down to java.lang.Double and suffering the limits therein If you instead used the library org.clojure/math.numeric-tower [1] you can get much more precise answers, for example # start a repl with an extra dep $ clj -Sdeps '{:deps {org.clojure/math.numeric-tow…

> The very next sentence of the article points out that you can leverage Clojure's interop to invoke Math.pow()

Hum, ok, to my defense it's not the very next sentence, it's much later at the very end when talking about Method invocation, so I hadn't read that far.

+1 for linking to the numeric tower though

Re: Clojure by Example (2015)

#20
post #10

Earlier quoted context omitted.

If you remove the Java SDK from Clojure's foundation, you're losing a lot of its power. The interop is part of that foundation.

For sure, but “Clojure doesn't provide built-in function for exponential operation” is still an objectively true assertion. If your point is that it’s wrong in spirit, I totally agree, and I think this is yet another example of the limits of this “cheat sheet” approach. If you’re coming from another language, the concept of easy, well integrated interop with a host language platform would be fairly foreign, so the im…

Think it might just be quibbling on the nature of 'built-in' at that point.
Post reply on HN