Fork me on GitHub
#clojure
<
2017-04-15
>
Tim00:04:59

how can I get this to return 0.08? (* 0.8 0.5 0.2) => 0.08000000000000002

Tim00:04:08

ah, bigdec

seancorfield01:04:47

@j-po why are you asking me? (curious)

j-po01:04:28

You're the last committer on it and I know you

seancorfield01:04:42

Alex Miller updated it after me 🙂 I did almost nothing of substance on that library anyway. Cosmin wrote it.

seancorfield01:04:05

(his last update was August 2012 tho’)

seancorfield01:04:38

The only changes since Cosmin touched it have been purely cosmetic — updating parent pom and adding the contributing notes.

seancorfield01:04:54

postwalk is a strange beast… I think @hiredman is right that you’re hitting MapEntry objects that satisfy vector? and so you’re trying to convert those into arrays of hash maps.

seancorfield01:04:45

map-entry? exists but I think it was only added recently (1.9). What version of Clojure are you using @j-po ?

alexmiller01:04:49

It was added in 1.8 and there were changes related to it in postwalk

alexmiller01:04:10

I think there might actually be an open ticket on something related to this

j-po01:04:21

We do have map-entry? but even

(defn- java-y [form]
  (walk/postwalk #(cond (map? %) (java.util.HashMap. %)
                        (map-entry? %) %
                        (vector? %) (into-array java.util.HashMap %)
                        :else %)
                 (walk/stringify-keys form)))
throws java.lang.IllegalArgumentException: array element type mismatch

seancorfield01:04:56

You’ll have to add some debugging to your function — print out the types of the items it is called on. Using postwalk and prewalk to convert expressions is always pretty fraught in my experience.

seancorfield01:04:47

In the vector? case, you’ll probably want to print (mapv type %) to see what all the element types are.

seancorfield01:04:44

(the discussion there suggests trying to use prewalk instead of postwalk but that may or may not help you)

seancorfield01:04:13

I reported that based on a similar discussion here in #clojure some time ago.

j-po01:04:20

Prewalk fixes one of two test cases 😕

j-po01:04:21

Printing confirms that it's seeing map entries as vectors

noisesmith01:04:11

yes, you can only detect a map entry in a prewalk

seancorfield01:04:04

In postwalk, the MapEntry has already been rewritten as a vector, unfortunately. That’s what CLJ-2031 is about.

j-po02:04:57

(Prewalk also won't work for our case because it seems like the Java types aren't walkable.)

seancorfield02:04:09

Sounds like you’d be better off writing your own specialized expression walker / converter then…

nathanmarz02:04:06

@j-po with specter it's:

(defn java-convert [data]
  (let [node-walker (recursive-path [] p
                      (cond-path
                        map? (continue-then-stay MAP-VALS p)
                        vector? (continue-then-stay ALL p)
                        ))]
    (transform node-walker
      (fn [e]
        (if (map? e)
          (java.util.HashMap. e)
          (into-array Object e)
          ))
      data
      )))

nathanmarz02:04:08

should be higher performance than using postwalk as well

nathanmarz02:04:48

I find it's almost always better to be precise about what you're traversing, avoids the pitfalls of doing a brute force postwalk

qqq07:04:54

is there something similar to clojure.spec/assert, but it runs even when s/ehceck-aseerts is false ?

qqq07:04:00

(assert (s/valid ...))

slipset08:04:33

Around our code base I find we do quite a bit of code like the following:

slipset08:04:15

The anonymous function really annoys me, but I haven’t found a better solution to this “idiom”

lmergen08:04:34

my conclusion is that everybody uses spec differently 🙂

lmergen08:04:42

i’ve seen some interesting approaches

lmergen08:04:24

i personally like the (when-let [errors (s/explain-data ::spec v)] (throw (ex-info ...))) approach

lmergen08:04:55

other than that, i suggest writing your own assert function

neurogoo09:04:03

What is the story of Clojure on Android nowadays? I have been searching the net and it seems that Clojurescript + React Native side seems to be the only active one. Unfortunately that is not really ideal for many things for example for games

qqq10:04:10

iirc, the best way to use clojure on addroid is via cljs

scriptor12:04:30

@slipset (filter (comp #{"baz"} :foo) foos) should also work

slipset12:04:43

@scriptor nice! I tend prefer comp/partial over anon fns

matan15:04:32

Can any HoneySQL advocates out here possibly comment on any advantages compared to good old korma? I implemented something with HoneySQL, but looking at korma in the back mirror, it looks, well, simpler (even if not easier, to borrow from simple v.s. easy).

noisesmith15:04:34

For debugging, having a function that generates a query that you run, rather than a macro that generates and also runs a query, seems a bonus. Maybe I just don't know enough about korma to know where it exposes that, if it offers it.

bradford19:04:40

Not strictly clj, but any Postgres nerds here? I’m wondering if this is an efficient way to select a random row from a large (5M+) query result set. I’m thinking it’s not because you have to get the resultset first. But I’m not sure if there’s a better way:

WITH q as (SELECT (ip), row_number() OVER (PARTITION BY country) FROM PROXIES
            WHERE country = '%s' AND active = true),
            c AS (SELECT COUNT (ip) FROM q),
            r AS  (SELECT  ceiling( (count + 0) * random()) FROM c)
            SELECT * from q WHERE row_number IN (SELECT ceiling FROM r)

bradford19:04:40

ugh actually I don’t think btrees on disk are even the right solution, I just don’t wanna give up database-y things

danp19:04:51

Hi all, I'm doing some Java interop stuff that requires me to set thread authentication every time I want to run something. Pretty much the equivalent of this process: http://docs.oracle.com/cd/E28280_01/apirefs.1111/e17060/oracle/odi/core/security/SecurityManager.html

danp19:04:24

I'm currently executing this code

danp19:04:58

(.setCurrentThreadAuthentication odi-security-manager odi-authentication)

danp19:04:08

I've got the option of creating a set of functions that wrap the above, and other functionality in a do, but is there an obvious alternative I'm missing?

tagore19:04:06

Hmm... I'm not a very good postgres geek, but one thing to be aware of is that CTEs are optimization boundaries in Postgres.

tagore19:04:15

I don't know if it matter much for your particular query, but I've found that eliminating them an lead to a ten-fold (or greater) increase in performance in some cases. If you are about the performance of your queries you should always at least look at CTEs with a jaundiced eye.

tagore19:04:16

@bradford That said, there are probably better approaches to this problem.

tagore19:04:14

That assumes numeric ids...

tagore19:04:53

I'm generally inclined to think that if I can push an operation onto the database, properly, it is likely to beat any simple ad-hoc solution I'm likely to come up with easily. So I think your instinct is correct.

tagore19:04:35

But this of course depends a great deal on your individual circumstances.

clojer20:04:39

My Luminus app compiles but when I submit a form I get “java.lang.RuntimeException: EOF while reading string, compiling:(null:3:1)”

clojer20:04:18

Syntax of the queries.sql seems to be ok

clojer20:04:25

(defn searchresults [params] (let [form-schema {:country [st/required] :commodity1 [st/required] :commodity2 [st/required] :commodity3 [st/required] :devel_status [st/required]} [errors _] (st/validate params form-schema)] (if (nil? errors) (l/render “searchresults.html” {:results (db/sql-search {:country (Integer/parseInt (params :country)) :commodity1 (Integer/parseInt (params :commodity1)) :commodity2 (Integer/parseInt (params :commodity2)) :commodity3 (Integer/parseInt (params :commodity3)) :devel-status (Integer/parseInt (params :devel_status))})}) (l/render “errors.html” {:errors errors 😛 params}))))

clojer20:04:26

sql-search ends with:

clojer20:04:05

Solved by re-running lein run which means, err, not really solved as I don’t have a clue what happened. Clojure “magic”??

ghadi20:04:26

Some rain on the Java 9 module parade ^ @tcrawley

bradford21:04:13

@tagore Thanks for the wisdom!

tagore21:04:40

@bradford Well, I hope that helps, but... don't take me too seriously. Without knowing your exact use-case, etc, having the opportunity to look at your actual schema and data, etc., anything I say is likely to be at best a hint, and at worst a rabbit-hole.

bradford21:04:11

@tagore Yeah — I basically have a list of a few million proxies, and I need to choose one at random 😛

tagore21:04:31

Hmm- the direct approach is sometimes best. Are your items sparse in their ids?

tagore21:04:12

And are their id ordered?

tagore21:04:43

I suppose for randome selection the latter doesn't matter.

tagore21:04:38

If you use incrementing integers for ids, for instance, and the performance of this query really matters, you could just keep track of the highest id you've generated and select a random number below that.

tagore21:04:07

But.. are your ids spare? Than you might have to generate random numbers until you got lucky.

tagore21:04:24

That might still be a lot faster than a search within known ids. Depends on how sparse your ids are.

colliderwriter21:04:58

anyone have an opinion on whether using clojure.spec to make sure a server is up is a good idea?

bradford22:04:30

@tagore Yeah, my IDs are over the IP address space sooooo sparse. But thanks for the good thinkin’

drewverlee22:04:46

What are middleware factories? i’m reading about boot from the link below and the term came up. I have heard the term middleware (though im not sure it has sunk in) and factories, but i sense something more specific is being referred to. Here it is in context: > Boot addresses this problem by treating tasks as middleware factories Link: http://www.flyingmachinestudios.com/programming/boot-clj/

noisesmith22:04:03

it's a function that takes some arg(s) and returns a middleware

noisesmith22:04:19

a middleware in this case being a function that takes a handler function and returns a new, wrapped, handler function

noisesmith22:04:31

it can add steps before or after the initial handler is run

noisesmith22:04:43

(defn verbose-middlware [f] (fn [& args] (println "calling f with" (pr-str args)) (let [res (apply f args) ] (println "f returned" (pr-str res)) res) - a simple middleware

noisesmith22:04:00

it does exactly what f does, plus a bunch of printing

noisesmith22:04:21

a middleware factory is one level abstraction above that

noisesmith22:04:42

the most common middlewares alter the args (by adding context or pruning or substituting some parameters), though some alter the return value, and a few rare do both

noisesmith22:04:07

oh, and some decide conditionally whether to run the handler function at all

drewverlee23:04:50

That makes sense. thanks @noisesmith

qqq23:04:34

How do I convert the following to "garden" ? (the css library that looks like hiccup)

tbody tr:nth-child(odd) {
  background-color: #ff33cc;
}

tbody tr:nth-child(even) {
  background-color: #e495e4;
}

qqq23:04:19

my main point of confusion is that I nave no idea how to convert tr:nth-child(odd)

qqq23:04:24

everything else I can already do