Avi Drucker / Apr 28 2021 / Published
Remix of Clojure by Nextjournal
Clojure Koans 20 Interop Notebook
Don't forget, within this notebook there are spoilers to the Clojure Koans exercises.
Original Clojure Koans repository: https://github.com/functional-koans/clojure-koans/
Let's jump right in!
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure
Initial Thoughts
Wow, Java interop is both cool and scary to me at the same time. Hyperbolic? Maybe. Normal reaction for someone new to Clojure? I believe so.
;; "You may have done more with Java than you know"
(= java.lang.String (class "warfare")) ; hint: try typing (javadoc "warfare") in the REPL
;; question 20001: Do Clojure strings have any differences from Java strings, and if yes, what are they?
;; skill 20001: Find out the class of a thing in Clojure by using the `class` function
;; skill 20002: Find out more information about a thing (and its underlying implementation?) in Clojure by using the `javadoc` function
Clojure
;; "The dot signifies easy and direct Java interoperation"
(= "SELECT * FROM" (.toUpperCase "select * from"))
;; skill 20003: Use a native Java function via Java interop
Clojure
;; "But instance method calls are very different from normal functions"
(= ["SELECT" "FROM" "WHERE"] (map clojure.string/upper-case ["select" "from" "where"]))
;; question 20002: Why can the `map` function only take Clojure functions (not Java interop) ? For example, passing a Java interop for the `toUpperCase()` function to the `map` function results in the following error: "Unable to resolve symbol: .toUpperCase in this context"
Clojure
;; "Constructing might be harder than breaking"
(= 10 (let [latch (java.util.concurrent.CountDownLatch. 10)]
(.getCount latch)))
;; question 20003: What exactly is happening here???
;; question 20004: What relevant/pertinent Clojure skill are here to take away?
Clojure
;; "Static methods are slashing prices!"
(== (reduce * (repeat 10 2)) (Math/pow 2 10))
;; question 20005: Does the Math library in Clojure come from Java?
;; question 20006: How can one determine programmatically where libraries come from? See 20005
;; question 20007: What is the significance of static methods in Clojure?
Clojure
Closing Thoughts
I posted this too late at night, and I'm sleepy now. Closing thoughts TBD...