Fork me on GitHub
#clojure
<
2016-06-03
>
hiredman01:06:51

I would double check

hiredman01:06:10

for example, could res be the string "nil"?

hiredman01:06:08

you can log the pr-str, or log the type as well

hiredman01:06:50

or log the result of (nil? res)

payal01:06:04

No its not a string ..as it throws null pointer exception later

hiredman01:06:36

something throws a null pointer exception later

hiredman01:06:32

res definitely doesn't throw an npe, because values don't throw npes, calling methods on nil does

hiredman01:06:05

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

hiredman01:06:02

like maybe

(.someMethod (get (make-map ...) :address))

payal01:06:39

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...

noonian01:06:19

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.

hiredman01:06:56

well, so that code is entirely different from what you asked about

hiredman01:06:31

what makes you think res is a map with a non-nil value for the :body key?

payal06:06:07

@noonian: I am handling nil response in the caller..

payal06:06:42

@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

tjg10:06:20

@hiredman: Thanks for the link!

Macroz11:06:59

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.

Macroz11:06:49

Or maybe there is a Leiningen template available that contains the best setup?

annarcana11:06:30

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.

annarcana11:06:17

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.

nathanmarz12:06:39

@jarcane what's an example of what you're trying to do?

annarcana12:06:22

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.

nathanmarz12:06:35

i mean like an example of input/output

m1dnight12:06:13

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]}}}

m1dnight12:06:21

Thats an example of post walk, if that helps.

bronsa12:06:46

@m1dnight: there's postwalk-demo that does essentially that

m1dnight12:06:57

With the chance of being bad code, you could check if the value is of type X and then convert it.

m1dnight12:06:22

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.

nathanmarz12:06:48

yea, that's the problem with using walk approaches for nested transformations

annarcana12:06:52

Yeah, I think I just figured it out.

nathanmarz12:06:10

it targets everything so you have to carefully filter out the things you don't want to touch

nathanmarz12:06:32

also performance-wise much less than optimal

annarcana12:06:46

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))

bronsa12:06:17

@jarcane: FYI satisfies? is really slow

annarcana12:06:25

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"}}}

annarcana12:06:54

@bronsa: Would it be faster instead to import and look for the JodaTime object?

annarcana12:06:02

With instance?

bronsa12:06:09

orders of magnitude faster

bronsa12:06:22

but you loose the dynamicity of satisfies?

annarcana12:06:24

Cool. I’ll have another try at getting that working. Thanks for the tip.

bronsa12:06:39

if you don't need it, definitely use instance?

m1dnight12:06:34

You could look there for inspiration as well.

sekao13:06:52

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.

mpenet13:06:18

sounds like a ring-buffer, for which you can probably find implementations in clj

jimmy13:06:53

hi guys how do we convert ByteArrayInputStream to string in clojure ?

jimmy13:06:32

@mpenet: thanks it works 😄

m1dnight13:06:00

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.

m1dnight13:06:09

But as already pointed out, it might already exist..

m1dnight13:06:45

Or you want to fill it first, and only then take the elements out?

m1dnight14:06:10

sekao: This is what I came up with:

m1dnight14:06:12

(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))

m1dnight14:06:38

==> [3 4 5 6 7]

m1dnight14:06:54

Don’t we need a Clj slack bot in here btw? 😆

sekao14:06:08

@m1dnight: thanks! that looks better than using take-last for sure

m1dnight14:06:33

It’s O(1) in comparison to the linear performance of take-last

ghadi14:06:40

m1dnight: there's a clojure bot here /clj

ghadi14:06:12

seems busted right now actually

ghadi14:06:21

oh, just delayed

m1dnight14:06:24

Mine triggered the evaluation? 😆

ddellacosta14:06:00

so, this is probably a known thing but I'm not able to run (is (not (thrown? …))) in tests

ddellacosta14:06:34

(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

m1dnight14:06:38

Long story short: is is a macro and it just does not work.

ddellacosta14:06:42

well then, thanks for googling that for me

ddellacosta14:06:49

my google attempts did not work for some reason...

m1dnight14:06:00

No biggie, it’s friday and Im procrastinating 😛

ddellacosta14:06:22

bummer though, seems like a bug to me

m1dnight14:06:40

No, the thrown? is a special dispatch symbol. I think (is (not (instance? ..))) will not work either.

benzap15:06:44

ya, macros can really muddle up a lot of use-cases, but they're powerful on their own terms

loganmhb16:06:07

@frankk38506: you want (:Description %2) to be outside the path vector for assoc-in where that empty map literal currently is, I think

frankk3850616:06:35

Thanks @loganmhb, I’ll give that a try

loganmhb16:06:20

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

loganmhb16:06:04

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

loganmhb16:06:20

(reduce fn (first datal) datal)

frankk3850616:06:54

awesome, thank you for explaining that, it makes sense now

arohner17:06:00

Has anyone debugged Java 8 SNI SSL crap and the difference in behavior between clj-http and clj-http-lite?

arohner17:06:54

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

Lambda/Sierra17:06:23

@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.

Lambda/Sierra17:06:51

At the time, none of the Clojure HTTP libraries supported it.

ragge17:06:49

@arohner: -Djavax.net.debug=all can be helpful

ragge17:06:52

If you haven't already gone there...

ragge17:06:49

@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

ragge17:06:13

@arohner: fix for us was to upgrade to a version of clj-http that uses the fixed version of apache http client

josh_tackett18:06:30

Anyone know how to create a "log a call" object for a salesforce opportunity

arohner18:06:02

@ragge: I’m running httpclient-4.5

arohner18:06:23

@stuartsierra: thanks. Now it appears to work in native JDK 1.8, but apache httpclient is breaking it somehow

Lambda/Sierra18:06:40

dunno — all I can suggest is to check your dependency versions carefully

josh_tackett18:06:55

@arohner try Lein Deps Tree in command prompt

arohner18:06:03

and it works in clj-http 3.1, but not 2.2.0. Apparently the difference is 3.x uses non-deprecated HTTPClient interfaces

josh_tackett18:06:05

should help you narrow it down

vinnyataide18:06:17

hello, I have a very nooby question: Is there any relation between C pointers and function call and apply?

Prakash19:06:30

@vinnyataide: I didnt get what u mean? u talking about clojures’ apply function?

vinnyataide19:06:05

@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

fasiha19:06:38

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?

Prakash19:06:23

(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

plexus19:06:28

@fasiha: you would have to set up a classpath to clojure and all other dependencies

plexus19:06:08

You would use this if you're already using maven to manage dependencies

seancorfield19:06:26

@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.

fasiha19:06:32

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.

plexus19:06:34

One of the great things of the Java world, it's relatively easy to package and ship a single artifact that "just works"

seancorfield19:06:32

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 🙂

vinnyataide19:06:47

@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?

Prakash19:06:01

@vinnyataide: u mean that apply can use the address of the args elements , rather than copying them to create new args?

vinnyataide19:06:31

@pcbalodi: maybe the concept comes from the resolving methods, like how a key resolves to itself and a symbol doesnt

fasiha19:06:29

@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

vinnyataide20:06:25

@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

Prakash20:06:29

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.

jorgedfbranco20:06:57

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

jorgedfbranco20:06:27

(defn f [& xs] (alength xs)) was what I tried

hiredman20:06:02

clojure var args are a sequence, and actually can be infinite in size

hiredman20:06:02

(apply (fn [& args] (first args)) (range)) 

ghadi20:06:28

to protect against that issue you can do (clojure.lang.RT/boundedLength args 100)

ghadi20:06:57

but then you get a linear count, not a constant time count

seancorfield21:06:59

Who maintains http://crossclj.info ? It seems to be down / not responding… 😞

brian_mingus21:06:59

anyone know how to do multiple loop destructurings without creating a nested loop?

brian_mingus21:06:16

seems to be possible with (dotimes (let [])) but seems a bit weird

noonian21:06:03

What are you trying to accomplish? I don’t really understand what you mean by multiple destructurings.

brian_mingus21:06:46

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]

lewix21:06:25

brian: you could use the for macro for that

brian_mingus21:06:43

i thought i tried that and found it was nestedly looping

lewix21:06:30

brian: what pattern do you want for you output?

brian_mingus22:06:30

[1 4 5] [2 5 6] [3 6 9]

brian_mingus22:06:14

now that i realized it's a transpose, it was much easier: (apply mapv vector [[1 2 3] [4 5 6] [7 8 9]])

brian_mingus22:06:48

actually, though that doesn't solve my problem because i need to perform arbitrary operations, not just transpose

brian_mingus22:06:07

so i still need a looping construct that gives me only the current element of each vector on each iteration

brian_mingus22:06:55

here we go 🙂 (map #(vector %1 %2 %3) [1 2 3] [4 5 6] [7 8 9])

seancorfield22:06:30

or just (map vector [1 2 3] [4 5 6] [7 8 9])

jorgedfbranco22:06:15

What's the reasoning for type hints not working for any primitive types other than long and double?

hiredman22:06:42

that is not true

hiredman22:06:56

the way you signal to the compiler to make a function that takes a primitive overlaps with typehinting, but they are distinct mechanisms

hiredman22:06:51

you can type hint other kinds of primitives, put you cannot pass them as arguments to functions

brian_mingus22:06:10

why does this print the representation of a LazySeq despite that I have a doall?

(str "a" (doall (map #(str %) ["b"])) "c")

hiredman22:06:31

doall doesn't change the type of the object

brian_mingus22:06:01

why doesn't that code print "abc"

hiredman22:06:11

because str just calls the toString method on objects which is not the way to get a readable representation of an object

hiredman22:06:26

you want the pr/prn/pr-str family of functions

hiredman22:06:07

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