What's missing is current complete examples with the library choices and configuration all worked out.
Clojure by Example (2015)
11–20 of 46 posts
Re: Clojure by Example (2015)
#12The 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)
#13 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 structureRe: Clojure by Example (2015)
#14> 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
And this is then consistent in ClojureScript as well.
Re: Clojure by Example (2015)
#15Re: Clojure by Example (2015)
#16There 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…
(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)
#17Show HN: Clojure by Example - https://news.ycombinator.com/item?id=9692281 - June 2015 (35 comments)
Re: Clojure by Example (2015)
#18Learning 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…
Re: Clojure by Example (2015)
#19> 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…
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)
#20Earlier 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…