This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-03
Channels
- # admin-announcements (2)
- # alda (4)
- # beginners (15)
- # boot (89)
- # cljs-dev (88)
- # cljsrn (75)
- # clojure (149)
- # clojure-belgium (16)
- # clojure-france (2)
- # clojure-greece (6)
- # clojure-russia (108)
- # clojure-spec (39)
- # clojure-taiwan (3)
- # clojure-uk (7)
- # clojurescript (70)
- # css (3)
- # cursive (17)
- # data-science (2)
- # datascript (7)
- # datomic (41)
- # dirac (3)
- # hoplon (12)
- # instaparse (1)
- # juxt (3)
- # lambdaisland (9)
- # mount (4)
- # off-topic (6)
- # om (71)
- # om-next (4)
- # onyx (22)
- # other-languages (56)
- # perun (15)
- # proton (6)
- # re-frame (32)
- # reagent (42)
- # specter (34)
- # spirituality-ethics (7)
- # tmp-json-parsing (5)
- # untangled (13)
- # vim (4)
- # yada (6)
res definitely doesn't throw an npe, because values don't throw npes, calling methods on nil does
I don't have your code in front of me, but my assumption would be the npe is coming from some derived value (not a method call directly on res), and there is likely some other error in there
umm mayy be I am calling json read-str..forgot to mention in the method above..I am creating hashmap in this way:
(hash-map (keyword (-> res :body (json/read-str :key-fn keyword) :address )) res)
So if res is null npe is thrown..Thats all is what implemented in the method...
What are you doing with the return value of make-map
? If res
is nil
then make-map
will return nil
to its caller so it could be a NPE from the return value like hiredman suggests.
@hiredman: Yes it is .. res is a response returned from an API call where body is a json string and address is one of the key value pair ..But if that api call throws an exception then res is nil...in which case I dont want to move ahead and create a map..and npe is thrown at this line where hashmap is created
Hey is there a good (and comprehensive?) guide to writing CLJC libraries? Pretty much all material I can find is about reader macros only. No best practices, gotchas etc.
So, I need to map a function over the values in a big nested map. I’m running at a loss as to how to do this.
Is there some equivalent to map for nested k/v structures? I looked at clojure.walk but I’m not sure I actually follow how it works.
@jarcane what's an example of what you're trying to do?
nathanmarz: I have a big chunk of query I want to export as EDN, but before I do so I want to go over it and translate any DateTime objects into strings.
i mean like an example of input/output
user=> (clojure.walk/postwalk (fn [val] (do (println “Value: “ val) val)) {:level1-a 1 :level1-b {:level2-a 2 :level2-b 3 :level2-c {:level3-a [1 2 3]}}})
Value: :level1-a
Value: 1
Value: [:level1-a 1]
Value: :level1-b
Value: :level2-a
Value: 2
Value: [:level2-a 2]
Value: :level2-b
Value: 3
Value: [:level2-b 3]
Value: :level2-c
Value: :level3-a
Value: 1
Value: 2
Value: 3
Value: [1 2 3]
Value: [:level3-a [1 2 3]]
Value: {:level3-a [1 2 3]}
Value: [:level2-c {:level3-a [1 2 3]}]
Value: {:level2-a 2, :level2-b 3, :level2-c {:level3-a [1 2 3]}}
Value: [:level1-b {:level2-a 2, :level2-b 3, :level2-c {:level3-a [1 2 3]}}]
Value: {:level1-a 1, :level1-b {:level2-a 2, :level2-b 3, :level2-c {:level3-a [1 2 3]}}}
{:level1-a 1, :level1-b {:level2-a 2, :level2-b 3, :level2-c {:level3-a [1 2 3]}}}
With the chance of being bad code, you could check if the value is of type X and then convert it.
Because post walk also calls your f
on the keywords. So you would really need to filter out the keywords and other values you don’t want to touch.
yea, that's the problem with using walk approaches for nested transformations
it targets everything so you have to carefully filter out the things you don't want to touch
also performance-wise much less than optimal
It seems to work in a simple demo to just do postwalk with a function like:
(defn date->str [v]
(cond
(satisfies? time/DateTimeProtocol v) (tf/unparse (tf/formatters :basic-date-time) v)
:else v))
frex:
boot.user> foo2
{:a 45, :b {:c 3, :d {:e 7, :f #object[org.joda.time.DateTime 0x68bbde59 "1910-01-01T00:00:00.000Z"]}}}
boot.user> (clojure.walk/postwalk backend.export/date->str foo2)
{:a 45, :b {:c 3, :d {:e 7, :f "19100101T000000.000Z"}}}
anyone have a favorite way of setting a size limit on a vector and doing FIFO (first in first out) when the limit is reached? need it to work in clojurescript as well. my current idea is just to run it through take-last
after i conj
something onto it.
You could use a doubly linked list as data store and then remove the last element in O(1) if you keep a pointer to it and add to the front in O(1) as well.
https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L2810-L2819 -> I.e., use subvec
instead, that is O(1)
(defn conj-limit
[stor e limit]
(conj (subvec stor (max 0 (- (count stor) limit -1))) e))
(-> []
(conj-limit 1 5)
(conj-limit 2 5)
(conj-limit 3 5)
(conj-limit 4 5)
(conj-limit 5 5)
(conj-limit 6 5)
(conk-limit 7 5))
so, this is probably a known thing but I'm not able to run (is (not (thrown? …)))
in tests
(deftest test-thrown
(is (not (thrown? Exception (throw (Exception. "throw it!"))))) ; won't compile
(is (thrown? Exception (throw (Exception. "throw it!"))))) ; this is fine though
well then, thanks for googling that for me
my google attempts did not work for some reason...
thanks @m1dnight
bummer though, seems like a bug to me
No, the thrown?
is a special dispatch symbol. I think (is (not (instance? ..)))
will not work either.
ya, macros can really muddle up a lot of use-cases, but they're powerful on their own terms
@frankk38506: you want (:Description %2) to be outside the path vector for assoc-in where that empty map literal currently is, I think
Thanks @loganmhb, I’ll give that a try
since the map containing “Something In English” is the first one in the list and you’re calling reduce with two arguments, that map is being used as the accumulation parameter the first time your anonymous function gets called
if you want the reducing function to be called for the first time with that map as both arguments, you need to pass it in explicitly
awesome, thank you for explaining that, it makes sense now
Has anyone debugged Java 8 SNI SSL crap and the difference in behavior between clj-http and clj-http-lite?
connecting to an Amazon-managed SSL host using SNI, on JDK8. connecting via clj-http gives SSL error: handshake_failure. Connecting via clj-http-lite works
@arohner: I had to debug SNI a couple of years ago … it was only supported in JDK built-in APIs (Java 1.7+) and recent versions of Apache's HTTP Client libraries.
At the time, none of the Clojure HTTP libraries supported it.
@arohner: which reminds me, we a similar issue recently with clj-http (worked with clj-http-lite), turned out to be: https://issues.apache.org/jira/browse/HTTPCLIENT-1119
@arohner: fix for us was to upgrade to a version of clj-http that uses the fixed version of apache http client
Anyone know how to create a "log a call" object for a salesforce opportunity
@stuartsierra: thanks. Now it appears to work in native JDK 1.8, but apache httpclient is breaking it somehow
dunno — all I can suggest is to check your dependency versions carefully
@arohner try Lein Deps Tree in command prompt
and it works in clj-http 3.1, but not 2.2.0. Apparently the difference is 3.x uses non-deprecated HTTPClient interfaces
should help you narrow it down
hello, I have a very nooby question: Is there any relation between C pointers and function call and apply?
@vinnyataide: I didnt get what u mean? u talking about clojures’ apply
function?
@pcbalodi: yeah man, like when you apply the function you change the reference of the arguments to the method being called, like reference args in C
When I run lein uberjar
(I feel so devops-y!), it produces two JARs, a standalone and a non-standalone one. Am I correct in understanding that the standalone one includes Clojure itself, whereas using the non-standalone one would need to set up classpath to a Clojure runtime?
(defn apply
"Applies fn f to the argument list formed by prepending intervening arguments to args."
{:added "1.0"
:static true}
([^clojure.lang.IFn f args]
(. f (applyTo (seq args))))
([^clojure.lang.IFn f x args]
(. f (applyTo (list* x args))))
([^clojure.lang.IFn f x y args]
(. f (applyTo (list* x y args))))
([^clojure.lang.IFn f x y z args]
(. f (applyTo (list* x y z args))))
([^clojure.lang.IFn f a b c d & args]
(. f (applyTo (cons a (cons b (cons c (cons d (spread args)))))))))
@vinnyataide: See the definition for apply here, it simply calls the given function by making a list of args@fasiha: Yes, the standalone JAR is fully-self-contained and you can just say java -jar path/to/my-standalone.jar
As @plexus says, you still need Clojure and other dependencies on your classpath for the non-standalone version.
I feel incredibly proud of myself for making and shipping standalone uberjars. My colleagues are used to shipping scripts and big directories to customers. I have this single 50 MB file 😄 I feel like a grownup.
One of the great things of the Java world, it's relatively easy to package and ship a single artifact that "just works"
We’re also coming from a world where we used to ship source scripts in a folder tree and moving to a world where we ship a JAR that can easily run in a Docker container. The ops folks like us a lot more nowadays 🙂
@pcbalodi: hmm, rich rickey always said that the value is just an time thing. what matters is the identity, I was making a paralel to the ram and how easy is to keep track of the addresses of values, and how C leverages that, do you understand where I'm comming from?
@vinnyataide: u mean that apply
can use the address of the args elements , rather than copying them to create new args?
@pcbalodi precisely
@pcbalodi: maybe the concept comes from the resolving methods, like how a key resolves to itself and a symbol doesnt
@vinnyataide: I'm not familiar with JVM internals, but I do gather that it does some really fancy optimizations. So it may be possible that it does some fancy stack pointer magic like you suggest, or maybe only after warmup. Clojure may or may not impede JVM's ability to do such optimizations (if JVM does any). JVM is one of the most advanced VMs on the planet, it seems hard to give simple explanations for what it will do
@fasiha I don't think its that hard since its built on top of immutable structures, if you can keep it, managing the values and keeping track becomes very simple, as I said its not an advanced question
hmm, I think the optimisations will be still at the JVM level though. Because the variables might get different memory addresses with context switches? Immuatibility is at Clojure level, addresses will be at the JVM implementation level at best.
Is there some way to know what's the length of a varargs param in clojure? I tried alength (as I assumed it all boiled down to an array at the JVM level) but I wasn't lucky
(defn f [& xs] (alength xs))
was what I tried
Who maintains http://crossclj.info ? It seems to be down / not responding… 😞
anyone know how to do multiple loop destructurings without creating a nested loop?
seems to be possible with (dotimes (let [])) but seems a bit weird
What are you trying to accomplish? I don’t really understand what you mean by multiple destructurings.
if you have multiple vectors, [1 2 3], [4 5 6], [7 8 9], and just want [1 4 5] [2 5 6] [3 6 9]
i thought i tried that and found it was nestedly looping
[1 4 5] [2 5 6] [3 6 9]
transpose
now that i realized it's a transpose, it was much easier: (apply mapv vector [[1 2 3] [4 5 6] [7 8 9]])
actually, though that doesn't solve my problem because i need to perform arbitrary operations, not just transpose
so i still need a looping construct that gives me only the current element of each vector on each iteration
here we go 🙂 (map #(vector %1 %2 %3) [1 2 3] [4 5 6] [7 8 9])
or just (map vector [1 2 3] [4 5 6] [7 8 9])
What's the reasoning for type hints not working for any primitive types other than long and double?
the way you signal to the compiler to make a function that takes a primitive overlaps with typehinting, but they are distinct mechanisms
you can type hint other kinds of primitives, put you cannot pass them as arguments to functions
why does this print the representation of a LazySeq despite that I have a doall?
(str "a" (doall (map #(str %) ["b"])) "c")
why doesn't that code print "abc"
because str just calls the toString method on objects which is not the way to get a readable representation of an object
the machinery required in the runtime to support different types of primitive arguments is kind of big, and there would be an exponential explosion of combinations of types and numbers of arguments, rich looked at it and decided supporting doubles, longs, and limiting it to four arguments (if I recall) was the sweet spot