How do you evaluate a string as a clojure expression?
(load-string “(+ 1 2)”)
(load-string “(+ 1 2)”)
Given that part of the attractiveness of Clojure is that you can use Java class libraries, why not just do: (System/exit 0)
(let [s (java.io.StringWriter.)] (binding [*out* s] (clojure.pprint/pprint {:a 10 :b 20})) (.toString s)) Edit: Equivalent succinct version: (with-out-str (clojure.pprint/pprint {:a 10 :b 20}))
Most of the differences will stem from the fact that F# is descended from ML while clojure is descended from lisp. So F# has a more ML/OCaml/Haskell feel with emphasis on: static typing type inference pattern matching While clojure has more of a Lisp feel with emphasis on: s-expressions macros closures Also as Brian noted, … Read more
An alternative could be to create either a redirect or a direct response in an additional route. Like so: (ns compj-test.core (:use [compojure.core]) (:require [compojure.route :as route] [ring.util.response :as resp])) (defroutes main-routes (GET “https://stackoverflow.com/” [] (resp/file-response “index.html” {:root “public”})) (GET “/a” [] (resp/resource-response “index.html” {:root “public”})) (route/resources “https://stackoverflow.com/”) (route/not-found “Page not found”)) The “https://stackoverflow.com/” route … Read more
I don’t think there’s a syntax-quote equivalent of the quote function. The Clojure reader is (currently) written in Java. The SyntaxQuoteReader class in src/jvm/clojure/lang/LispReader.java in the Clojure source is probably what you’ll want to read. It seems rather complex. You can see it building lists like (seq (concat …)) there. ret = RT.list(SEQ, RT.cons(CONCAT, sqExpandList(seq))); … Read more
You don’t. Macros are expanded during evaluation/compile time, not at runtime, thus the only information they can use are the args passed in, but not what the args evaluate to at runtime. That’s why a literal vector works, because that literal vector is there at compile time, but a is just a symbol; it will … Read more
There is a function to do this in clojure.core called comp. E.g. ((comp not zero?) 5) ; => true You might also want to consider using -> / ->>, e.g. (-> 5 zero? not) ; => true
reify is to defrecord what fn is to defn. “Ah right…… so what’s reify” Put simply, protocols are lists of functions a datatype should support, records are datatypes and reifications are anonymous datatypes. Maybe this is long-winded, but reify can’t be understood concretely without understanding protocols and types/records: Protocols are a way to use the … Read more
Someone has already implememented this on the Clojure group. You can specify how many args a function has, and it will curry itself for you until it gets that many. The reason this doesn’t happen by default in Clojure is that we prefer variadic functions to auto-curried functions, I suppose.