Fork me on GitHub
#clojure
<
2016-08-08
>
Alex Miller (Clojure team)00:08:05

There are cases where it is important to load one piece of code before another (for example installing multi method or protocol impls before use) but generally most code is not that sensitive

danielsz01:08:41

system 0.3.1 is out and here is the official announcement. https://groups.google.com/forum/#!topic/clojure/Td4-b8gDL58

seylerius06:08:25

Why is this failing with Map literal must contain an even number of forms? http://sprunge.us/jWCT

seylerius06:08:32

Oops. Accidentally wrapped the let in {} rather than ()

tianshu06:08:31

How can I transform

{:a [1 2 3] :b [3 4]}
to
[{:k :a :v 1} {:k :a :v 2} {:k :a :v 3} {:k :b :v 3} {:k :b :v 4}]
simply?

levitanong06:08:12

(->> {:a [1 2 3] :b [3 4]}
  (mapv 
    (fn [[key vals]]
      (mapv #(hash-map :k key :v %) vals)))
  (flatten)
  (vec))

niwinz06:08:23

Something like this:

(reduce (fn [acc [k v]]
          (reduce (fn [acc' v]
                    (conj acc' {:k k :v v}))
                  acc
                  v))
        []
        {:a [1 2 3] :b [3 4]})

tianshu07:08:34

šŸ‘ Good

tianshu07:08:26

I prefer @levitanong 's version.

levitanong07:08:01

@niwinz ā€™s version is likely more efficient though.

niwinz07:08:53

A little benchmark:

user=> (time (dotimes [i 1000] (levitanong)))
"Elapsed time: 7.857729 msecs"
nil
user=> (time (dotimes [i 1000] (niwinz)))
"Elapsed time: 0.712184 msecs"

syk0saje07:08:33

i'd use this but probably slow

syk0saje07:08:55

#(vec (for [k (keys %)
            v (% k)]
        {:k k :v v}))

tianshu07:08:01

Why @niwinz 'a version is more efficient?

levitanong07:08:07

@doglooksgood: flatten is an inherently slow operation. My code goes through the vector at least thrice. @niwinz ā€™s builds a new vector as he proceeds through the reduce.

levitanong07:08:21

so he only goes through the input data once

levitanong07:08:26

@syk0saje ā€™s code is the most elegant though, imho. I completely forgot about for.

kauko07:08:30

I like it too. It's pretty readable. Only part that can give the reader pause is v (% k)

kauko07:08:37

if you don't know how for works that is

niwinz07:08:05

user=> (time (dotimes [i 1000] (syk0saje)))
"Elapsed time: 1.97847 msecs"

syk0saje07:08:05

yeah, maybe better to use (get % k)

levitanong08:08:56

Someone should make a slack bot that can evaluate the efficiency of pasted code like what @niwinz is doing. šŸ˜›

kauko08:08:56

btw, if you really care about benchmarks you should use https://github.com/hugoduncan/criterium

yonatanel09:08:34

Is it possible to require multiple namespaces into the same alias? e.g both com.rpl.specter and com.rpl.specter.macros as specter

sander09:08:27

also asked this in #C0A5GSC6T, but maybe someone here knows: in a StreamableResponseBody (ring 1.6.0-beta4), can I somehow detect that the client has closed the http connection, and then stop writing a response?

sander09:08:05

thanks, that might be it. I didn't notice an exception when continuing to write a response for an already closed connection, so maybe the outputstream isn't properly closed by ring yet

mpenet10:08:10

it's a good question for @weavejester

mpenet10:08:48

also what happens (and how to detect) if a client "disconnects" before the response is sent in async response

mpenet10:08:01

there's a error handler actually, maybe you can use this detect it and stop writing to the responsebody

sander10:08:31

now that I'm doing better async handling in my StreamableResponseBody, I do get an IOException when trying to write. is there a way to trigger it without actually writing something?

mpenet10:08:00

I doubt it's possible no

agi_underground10:08:13

Someone can help me in off-topic channel? its about postgres triggers

mpenet10:08:56

@sander (I had the same issue in "jet", which is also based on jetty 9.3.x)

gnejs11:08:26

Havenā€™t played with clojure.spec as much as Iā€™d like yetā€¦ but Iā€™ve got a feeling that it could be used to solve this: https://medium.com/@jamis/weekly-programming-challenge-2-33ef134b39cd#.2sx7h4cme ā€” would you agree?

sander11:08:24

another problem with ring 1.6.0-beta4: it seems that my handler with a StreamableResponseBody gets called over and over again with the same request (with maybe 20 seconds in between), even though the original client has already quit

sander11:08:33

@mpenet, did you notice something like this as well?

mpenet11:08:51

I didn't use the latest ring yet

bcbradley12:08:44

i'm at a crossroads in my architecture of a solution to a problem, would it be off topic to ask for guidance about how to proceed here?

pesterhazy12:08:21

not off topic IMO

bcbradley13:08:15

I'm making a game in clojure. I'm trying at all costs to avoid having "types" because I think they create complexity, but I'd like to test that by making a game without types to see

bcbradley13:08:36

Right now I have every entity as just a "bag" (a map), and these are stored in arbitrary order in an array

bcbradley13:08:52

This array is traversed with a transducer each tick

bcbradley13:08:04

the transducer is composed of maps

bcbradley13:08:29

for instance, (comp (map gravitated) (map scaled) (map located) (map positioned) and so forth

bcbradley13:08:54

i've even got a map for collision detection

bcbradley13:08:13

but now I'm thinking about how to handle collision handling with a map and I feel like i've hit a roadblock

bcbradley13:08:24

how would you do it-- remember I want to avoid "type" information

bcbradley13:08:34

take for instance a rubber ball bouncing on the ground-- the map would have to "handle" the collision for both the ball and the ground. The ground would just be mapped to the ground again. The ball would be mapped to a new ball that is relocated as it would need to be given the fact that it is bouncing

bcbradley13:08:41

but how does the function know that the ball should be bouncing?

bcbradley13:08:52

does the ball have a "bouncy" status?

bcbradley13:08:00

it feels like I can't avoid type information.

sander13:08:30

@bcbradley: if the ball has properties that, in your model, determine that the ball is bouncy, e.g. {:mygame.physics/bounding-box [ ...coordinates... ]}, you could write a predicate (defn bouncy? [entity] (entity :mygame.physics/bounding-box))

bcbradley13:08:17

I see, so type info would be inferrred from abstract properties?

bcbradley13:08:54

i feel like that would end up creating scenarios in which adding a property might cause unintended side effects

bcbradley13:08:00

and then you'd have the type problem all over again

bcbradley13:08:24

I tried dispatching to entity specific 'handler' code, but that isn't working out all that well

bcbradley13:08:45

because the collision handling itself seems to be a problem who's semantics depends on atleast two entities, not just one

sander13:08:08

you could prevent unintended side effects from handling by using namespaced properties and having a clear policy per namespace

sander13:08:31

i.e. never change the meaning of a namespaced property

bcbradley13:08:21

i've heard that somewhere

bcbradley13:08:25

is that from spec?

sander13:08:57

no, it's a mindset that is encouraged by spec

sander13:08:25

you could use spec to structure your predicates, but it may impact performance

bcbradley13:08:53

for curiosity sake, lets suppose I rephrase the question: if you had to develop a function that would serve a map over the entities in an array and who's purpose was to take an entity as input, and return either the same entity or slightly different one as output so as to "handle" collisions for that entity (collisions being a key associated with a set of other entities), how would you write such a function?

bcbradley13:08:11

I'm asking for your opinion here-- you could do it the most straightforward way possible

bcbradley13:08:19

you could do it with namespaced keys

bcbradley13:08:25

you could do it by deferring

bcbradley13:08:45

but these approaches each have strengths and weaknesses-- i'm asking what you FEEL would be the best approach

sander13:08:50

wouldn't map over the entities one-by-one, since one collision probably impacts two entities

sander13:08:50

I'd probably call (swap! state (assoc :entities #(-> % handle-collisions handle-health handle-points))) 15 times per second

sander13:08:00

with (defn handle-collisions [entities] (let [all (filter collides? entities)] ...handle collisions...)) etc

bcbradley13:08:20

how would you determine what the functionality should be for the "handle collisions"?

bcbradley13:08:41

do you just have one enormous "handle collisions" function that knows how to interpret entities?

bcbradley13:08:53

do you defer to the entities themselves to provide "pieces" of the functionality, and then compose them?

bcbradley13:08:28

actually I think I answered my own question

bcbradley13:08:21

you can't compose functionality from each entity separately because the semantics of collision handling is such that it depends on at least two entities in the general case, and thats not the same as the composition of a function on one entity with another function on another entity

bcbradley13:08:29

thanks for your help!

sander13:08:38

good luck šŸ™‚

lapple13:08:33

Hi everyone -- I'm the Open Source Evangelist at Zalando(.com), and we've got a few open-source projects to offer the community: Friboo (https://github.com/zalando/friboo), a utility library to write microservices in Clojure, with support for Swagger and OAuth; and cats.match (https://github.com/zalando/cats.match), pattern matching for the monads in the cats Clojure library. Let me know if you have any feedback/suggestions for the projects, and I'll pass it along to the devs who created them ...

lvh13:08:26

How can a Clojure macro build a correctly type hinted Java method call?

lvh13:08:51

The "obvious" ^Type looks like it attaches to the wrong symbol.

lvh13:08:03

with-meta :tag?

lvh13:08:18

With a symbol and not a type I guess -- I'll go try that :)

mpenet13:08:30

symbol or even string if I recall

novakboskov14:08:44

Is it possible/desirable to modify atoms new value in its watcher function?

gfredericks14:08:36

unless you want to just swap! it again, which feels silly

gfredericks14:08:57

I was just thinking this morning about the idea of wrapping reference types so you can tweak the writes/reads

gfredericks14:08:04

I'm curious how easy that is

lambeta16:08:29

a question for juxt

(sort-by  (juxt second first)  [[4 2] [3 2] [2 3]]) ;;; ([3 2] [4 2] [2 3]) but I want ([4 2] [3 2] [2 3])
if I want second func keep in increment order, but the first func in descrement order. how can i do that?

tap17:08:56

what about

(sort-by  (juxt second (comp - first))  [[4 2] [3 2] [2 3]])

seancorfield17:08:06

Sorting descending on numbers is sorting ascending on negā€¦ yeah, what @tap said

lambeta17:08:14

great idea!!!

ghadi17:08:41

so the "official" way to do this is to reverse the comparator:

(sort-by key-function (fn [a b] (compare b a)) collection)

ghadi17:08:20

the negative trick only works for numbers

andy.fingerhut20:08:01

The negative trick also fails in the very rare scenario of Long/MIN_VALUE, whose negation is itself if the result is also a Long

andy.fingerhut20:08:25

reversing order of args in the comparator is far preferable to avoid subtle bugs

seancorfield20:08:47

@slipset: We have a flip in our "util" namespace at work for exactly that reason šŸ˜ø

dg20:08:41

flip is one of those functions that you write once for a specific thing and then realize you can use it all over the place

kenny21:08:20

Using clojure-1.9.0-alpha10, is is possible to print without the reader macro? For example:

;; what currently prints
(println {'myns/foo 1})
#:myns{foo 1}

;; what i want to print
(println {'myns/foo 1})
{myns/foo 1}

kenny21:08:10

The use-case is I am spiting a map that has a fully qualified symbol to a .edn file which will be read later. When reading the .edn file, the symbols are no longer symbols due to the conversion into a reader macro.

gfredericks21:08:21

I don't understand that last sentence

gfredericks21:08:31

what are the symbols if they aren't symbols?

hiredman22:08:02

my guess is, he is reading it in will something that doesn't support the new namespaced map syntax

hiredman22:08:50

I saw a jira issue to provide some way to turn namespaced maps off, but I am not sure what the status of that is

kenny22:08:09

@gfredericks: Sorry for the confusion. @hiredman's explanation is much better šŸ˜›

lambeta23:08:53

@ghadi but Iā€™m still confused how I can differentiate the order rules of functions in juxt if using ā€œofficialā€ way.

nilrecurring23:08:45

@gfredericks: yep it went in the collection of "broken stuff", as I can't make make it work for some reason. It could be my fault too. I'll probably fork it and try to get to a well polished tool :)

gfredericks23:08:28

@nilrecurring I'd try upgrading tools.analyzer first

gfredericks23:08:41

I would love to see it more polished

gfredericks23:08:49

I get mad at it myself for not working :)

nilrecurring23:08:48

I have a fairly large project at the moment and I would love to see graphically if I should refactor better