This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-02-06
Channels
- # aatree (1)
- # alda (9)
- # beginners (63)
- # boot (124)
- # braid-chat (8)
- # cider (44)
- # cljs-dev (44)
- # clojure (79)
- # clojure-dev (1)
- # clojure-russia (47)
- # clojurescript (105)
- # community-development (16)
- # cursive (3)
- # datavis (1)
- # datomic (54)
- # editors (10)
- # editors-rus (10)
- # emacs (18)
- # garden (1)
- # hoplon (5)
- # jobs (1)
- # ldnclj (6)
- # lein-figwheel (2)
- # luminus (1)
- # off-topic (29)
- # om (49)
- # overtone (5)
- # parinfer (12)
- # proton (2)
- # re-frame (5)
- # reagent (6)
- # ring-swagger (1)
- # slack-help (3)
- # spacemacs (1)
- # yada (42)
I’m looking over the Numbers section on https://en.wikibooks.org/wiki/Learning_Clojure/Data_Types
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 clojure.data.json/read-str “{\”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: https://github.com/dakrone/cheshire
yes i tried to include cheshire in the cljs but i actually didn’t get it to work either
first, i simply required cheshire, after which the cljsbuild output told me that cheshire should be required under the :require-macros
key
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]
(f)))
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.
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)
.
@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/x
user=> (defn double-x [] (* 2 x))
#'user/double-x
user=> (double-x)
4
user=> (defn some-function [f] (binding [x 3] (f)))
#'user/some-function
user=> (some-function double-x)
6
@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
user=> (foo {})
1 2 3
nil
user=> (foo {:a 5 :c 9})
5 2 9
nil
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 '())
order-rows
(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)
extra-html))))
here’s my example rewritten in the way i’m suggesting:
user=> (defn double-x ([] (double-x {})) ([{:keys [x] :or {x 1}}] (* x 2)))
4#'user/double-x
user=> (double-x)
2
user=> (defn some-function [f] (f {:x 3}))
#'user/some-function
user=> (some-function double-x)
6
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]
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)
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.
Good luck!
What's the difference between (get map key)
and (find map key)
or are there any?
Oh, now I see it...
Answering to myself: find
returns key value pair.