Fork me on GitHub

The second paragraph is confusing me: Java also includes the classes java.lang.BigInteger and java.lang.BigDecimal for arbitrary precision integer and decimal values, respectively. The Clojure arithmetic operations intelligently return these kinds of values as necessary to ensure the results are always fully precise, e.g. adding the Integer values 2000000000 and 1000000000 will return the value 3000000000 as a BigInteger because it is too big to be an Integer.


It returns a Long instead of a BigInteger. And, in any case, should be references to BigInteger be references to BigInt instead?


hello, small question: if one tries to access a map called body {"name":"Cheshire Cat","status":"grinning”} in a .cljs file, how would you do that? I’ve tried (:name body) but it returns null


the only way i have gotten it to work is to define the body differently in the let: let body (.parse js/JSON :original-body) and then use a js method when accessing the value: (.-name body).. which seems a ineffective.. any suggestions?


@timaeus, that’s correct. What you have is JSON rather than a map. You’ll want to use “{\”name\”:\"Cheshire Cat\”,\”status\”:\”grinning\”}” :key-fn keyword) to get a Clojure map with keywords like you’re expecting.


Personally, any time I’m dealing with JSON->map/map->JSON, I use Cheshire:


yes i tried to include cheshire in the cljs but i actually didn’t get it to work either


Which error was it giving you?


first, i simply required cheshire, after which the cljsbuild output told me that cheshire should be required under the :require-macros key


then i did as so: :require-macros [cheshire.core :as json]


which returned a ‘json not defined’ error when i tried to use a method from cheshire


with WARNING: No such namespace: json at


Ah, that’s because Cheshire doesn’t support ClojureScript.


You might want to look into cljs-ajax which bakes in JSON->map coercions.


Hey all, I'm a beginner and am trying to find out something that I'm struggling to look up, wondering if someone can explain if what I am trying to achieve is possible. If I have a function that binds some value to a symbol, e.g.:

(defn some-function [f]
  (let [x 5]
Is there a way to pass in something to some-function that references the locally bound variable? E.g.
(some-function #(* x x))
;;=> 25
I realise that I could adapt these to be like this:
(defn some-function2 [f]
  (let [x 5]
    (arg 5)))

(some-function2 #(* % %))
;;=> 25
So this is more a conceptual question of whether the first example is possible? It relates to the design of a function I have, I would like the flexibility of being able to reference symbols defined within the let without being bound (pun unintended) in the form of what I can pass into the function.


I have tried playing with x and 'x. I have also tried playing with (delay ...) to get the code passed in to be evaluated only from within some-function


@djtango, it behaves as a closure but you’d have to pass x in explicitly as you’ve already demonstrated; when you define a function such as #(* x x) it tries to resolve x immediately from the current closure.


Only way you can do this without it trying to resolve vars is with a macro.


okay @akiva thanks for the advice - will look up macros!


Good luck. A bit more advice: I always try to find a way to solve a problem without using macros. They’re very powerful and thus easily abused and overused when a non-macro solution probably exists such as using partial. For instance, you could probably change the above to pass in a function that takes two arguments and then do (partial f x).


Or even ((f x)) and pass #(* % %) to it or whatever.


@djtango, @akiva : It’s not usually good idea, but what you seem to be looking for is dynamic scoping which Clojure does support:

user=> (def ^:dynamic x 2)
user=> (defn double-x [] (* 2 x))
user=> (double-x)
user=> (defn some-function [f] (binding [x 3] (f)))
user=> (some-function double-x)


@isaac_cambron: thanks for this, I am reading up on ^:dynamic and binding now and will try to make sense of whether I should be using a macro or binding for my use-case or if I'm even thinking about my use-case in the right way at all. Would it be okay to DM you if I have further questions?


I’d prefer we just chat here, since if I’m not around, someone else can jump in


Macros can basically do anything


but they’re often hard to understand and debug


so it really comes down to the details of the real problem you’re solving


I strongly suspect that if you can’t solve the problem you have by just passing in what you need, that dynamic binding would be better than a macro


If you have a situation where you have a bunch of default values of parameters that you may or may not want to override in the caller, I’d suggest parameterizing it with a destructured map with defaults


and just building the map with what you need


Like this:

user=> (defn foo [{:keys [a b c] :or {a 1 b 2 c 3}}] (println a b c))
user=> (foo {})
1 2 3
user=> (foo {:a 5 :c 9})
5 2 9


But I don’t know if that maps cleanly to the problem you’re solving


okay so the use case is that I am using data from a SQL database to populate an HTML table on a webpage. I am using a recursive function to populate the table, but in order to keep the function generic (so the same function can be used elsewhere) I have allowed for additional arguments so that the table can support additional fields.


(defn gen-order-rows
  [order-rows order-data & [extra-html]]
  (let [current-row (first order-data)]
    (if (= order-data '())
      (gen-order-rows (conj order-rows [:tr [:td (str (current-row :name))]
                                        [:td (str (current-row :quantity))]
                                        (if extra-html
                                          (extra-html current-row))])
                      (rest order-data)


here’s my example rewritten in the way i’m suggesting:

user=> (defn double-x ([] (double-x {})) ([{:keys [x] :or {x 1}}] (* x 2)))
user=> (double-x)
user=> (defn some-function [f] (f {:x 3}))
user=> (some-function double-x)


yup, with you so far


That's the sample code. The line (extra-html current-row) is the line I'm talking about. The data passed in is a sequence of maps. So I'd like to be able to pass in keys to the map to unpack data for the current-row.


yup. and i suppose that if the caller wants extra-html, they really need to provide those keys?


i.e. there’s no default set of them


so in order to get it working, I've chosen to pass in extra-html as

(fn [row] [:td (:subtotal row)])
But in the original conception, I had hoped to simply pass in something along the lines of
[:td (:subtotal current-row]


The idea was to keep it flexible, but I could include :subtotal in the if clause


so the basis of my questioning was to further my understanding of clojure by asking a more a general conceptual question of how could I achieve xyz rather than how to implement this feature, if that makes sense


I think if you want to get that just right, a macro would be the way. But honestly, I think the (fn [row] thing that you have is best


Even philosophically, having a function where you need to do a potentially arbitrary thing is the whole point of functions as first-class citizens


and it will give you a ton of flexibility


for example, if you find ourself making a lot of simple table cells, you can just (defn simple-td [key] (fn [row] [:td (get row key)]))


and then pass in (simple-td :subtotal)


i.e. functions are really what get you the most composability


(sorry, had to fix that a couple times)


whoa that's really cool


it's so simple but I can see how you can grow that


thank you! 😄


You’re totally welcome. That “oh, wow, I can build arbitrarily complicated things by composing simple functions!” realization you just had is one of the core things people love about functional programming. It’s fun to watch someone have it.



Mikko Koski19:02:47

What's the difference between (get map key) and (find map key) or are there any?

Mikko Koski19:02:13

Oh, now I see it...

Mikko Koski19:02:36

Answering to myself: find returns key value pair.