by dzpower · 7y ago · 0 comments
Gene Kim's Clojure example:
; input is JSON string: "{foo: 2}"
(defn xform [s]
(-> (js/JSON.parse s)
js->clj
(update "foo" + 2)
clj->js
js/JSON.stringify))
Racket equivalent: #lang racket
(require threading json)
; input is (xform "{\"foo\" : 2 }")
(define (xform s)
(~> s
string->jsexpr
(hash-update 'foo (λ (x) (+ 2 x)))
jsexpr->string)) (require [clojure.data.json :as json])
(defn xform [s]
(-> s
json/read-str
(update "foo" + 2)
json/write-str))
Your example is Clojurescript with interopI do prefer the ClojureScript because I don't know how to type "λ" and that seems like a usability handicap. Minor complaint I know.
Clojure and by extension ClojureScript has a lot of tasteful and pragmatic design decisions, e.g. the use of the threading form and implicit lambda illustrated with the "+ 2").
By contrast Racket has been extended to include a Clojure-inspired threading as a library, showing off just a little of its awesome linguistic extensibility, leading in this case to ergonomically similar code.
Clojure/Script is a wonderful opinionated modern Lisp; Racket is a more linguistically extensible modern Lisp. There's considerable cross-fertilisation going on.
If you love Clojure, it's worth keeping an eye on Racket.
* * *
You can spell λ out as lambda in Racket (and Scheme).