Fork me on GitHub
#clojure
<
2015-09-26
>
tom00:09:47

Quick question, I've never run into this before. I have a ring server set up running on port 8080. When I curl localhost:8080 I get connection refused. When I tried netstat it said java was listening on :::8080 so I curl :::8080 and it runs ok. Now, the next piece is getting nginx to proxy_pass to port 8080, but :::8080 of course isn't a valid url.

ecelis00:09:47

ipv6 addresses are enclosed in square brackets in nginx configuration

tom00:09:13

`location / { proxy_pass [::]:8080; }`

tom00:09:22

sudo nginx -t
nginx: [emerg] invalid URL prefix

tom00:09:38

I tried that one as well, the [::]:8080 but no luck

ecelis00:09:29

tom: I think you are missing the http:// part of the URL

tom00:09:02

I think you're right

tom00:09:34

looks like that does it, thanks ecelis!

beppu02:09:50

@nooga: I don't know enough about pulsar or the jvm to fix it. It's weird.

Pablo Fernandez08:09:51

How can I check whether a sequence contains a value? Something in Python’s in?

thheller10:09:08

@pupeno: if you do that check a lot and the order of your list is not important you should consider using a set instead of a list

borkdude10:09:15

@beppu @nooga When I read about Pulsar, I wondered what benefit it has compared to using core.async

ian10:09:34

@borkdude: it's a different approach to concurrency, no?

borkdude10:09:01

@ian it probably is, but what's the benefit

beppu10:09:21

@borkdude: I don't have a whole lot of hands-on experience with Pulsar yet, but the part that I found most intriguing was its ability to be used in a cluster (meaning actors don't necessarily have to live on the same server).

beppu10:09:17

Sometimes, you want to coordinate actions across different servers, and this might be a good way to accomplish that.

ian10:09:09

pulsar also covers more concurrency models than core.async does, it seems.

beppu10:09:19

It has a core.async compatible API if you wanted to use pulsar's actors through that kind of API.

nooga13:09:08

actors, behaviors and supervisors

nooga13:09:24

with core.async or manifold, you have to care about the pipes

nooga13:09:35

here, you care about the computation

nooga13:09:09

and, like @beppu mentioned, actors can live and communicate across processes and machines, even if they’re anonymous

meow13:09:52

I need some help with a deftype. It mostly works. It basically defines a Cell that behaves like an [x y] vector/point/position kind of thing.

meow13:09:44

The problem I have is I have a set of these Cells and I want a normal [x y] vector to be able to be used as an index into that set. Specifically I want (cells [x y]) to be true for some set of cells where one of the cells has an x and y value equal to the plain [x y] vector value.

meow14:09:03

Here is my current deftype, which isn't working:

#?(:clj
   (deftype Cell [x-coord y-coord]
     Positioned
     (position [_] [x-coord y-coord])
     (x [_] x-coord)
     (y [_] y-coord)
     Object
     (equals [_ that]
       (or (and (instance? Cell that) (and (= x-coord (.-x-coord ^Cell that))
                                           (= y-coord (.-y-coord ^Cell that))))
           (= [x-coord y-coord] that)))
     (hashCode [_] (hash [x-coord y-coord]))
     clojure.lang.Indexed
     (nth [_ i]
       (case i
         0 x-coord
         1 y-coord
         (throw (IllegalArgumentException.))))
     (nth [this i _] (nth this i))))

meow14:09:46

So right now I'm getting the following, which is not what I want:

(= (cell [1 2]) [1 2])
=> false

meow14:09:33

That's using a little constructor:

(defn cell [[x y]]
  (->Cell x y))

potetm14:09:36

@meow: So, this is way outside my expertise, but if you take a look at clojure.lang.Util.equiv, you find this little guy:

static public boolean equiv(Object k1, Object k2){
	if(k1 == k2)
		return true;
	if(k1 != null)
		{
		if(k1 instanceof Number && k2 instanceof Number)
			return Numbers.equal((Number)k1, (Number)k2);
		else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection)
			return pcequiv(k1,k2);
		return k1.equals(k2);
		}
	return false;
}

potetm14:09:30

It looks like it behaves differently if one of the args is a persistent collection.

meow14:09:59

@potetm: interesting

meow14:09:19

and I'm not clear on the difference between equals and equiv

potetm14:09:47

From clojure.core:

(defn =
  "Equality. Returns true if x equals y, false if not. Same as
  Java x.equals(y) except it also works for nil, and compares
  numbers and collections in a type-independent manner.  Clojure's immutable data
  structures define equals() (and thus =) as a value, not an identity,
  comparison."
  {:inline (fn [x y] `(. clojure.lang.Util equiv ~x ~y))
   :inline-arities #{2}
   :added "1.0"}
  ([x] true)
  ([x y] (clojure.lang.Util/equiv x y))
  ([x y & more]
   (if (clojure.lang.Util/equiv x y)
     (if (next more)
       (recur y (first more) (next more))
       (clojure.lang.Util/equiv y (first more)))
     false)))

potetm14:09:59

They forward to equiv.

meow14:09:23

and it would be nice if there was a master list of methods that need to be defined for what purpose

potetm14:09:48

@meow: I don’t think you can override equiv.

potetm14:09:32

Wait maybe. Let’s see here.

meow14:09:34

I've seen it done in cljs

meow14:09:40

IEquiv
     (-equiv [_ that]

meow14:09:47

not sure if its the same in clj

potetm14:09:22

I believe not. The equiv they use is on IPersistentCollection.

meow14:09:01

well, I'm comparing to a vector, which is probably an instance of IPersistentCollection so I think I need to define equiv on Cell

potetm14:09:04

It appears, even if you extend IPersistentCollection for your type, (= (cell 1 2) [1 2]) => true and (= [1 2] (cell 1 2)) => false

potetm14:09:12

So that’s pretty bad.

meow14:09:44

Well, that's the other aspect I don't have a good handle on.

meow14:09:10

How do I figure out where equiv is coming from on the clj side of things? I don't think its part of IEquiv like it is in cljs.

potetm14:09:13

I usually just jump into the functions/methods with cursive and poke around.

potetm14:09:59

Yeah clj doesn’t have nearly as many protocol hooks as cljs does.

meow15:09:29

Maybe I should extend IPersistentCollection - that would at least get me the most important part of comparing a vector to a set of cells

meow15:09:45

did you actually do that, or just think it through?

potetm15:09:35

I actually did it. I’m not so smart that I can guess that kind of thing simple_smile

meow15:09:20

can I see the code?

potetm15:09:17

Sure. One sec.

potetm15:09:25

(deftype Cell [x-coord y-coord]
  Object
  (equals [_ that]
    (or (and (instance? Cell that)
             (and (= x-coord (.-x-coord ^Cell that))
                  (= y-coord (.-y-coord ^Cell that))))
        (= [x-coord y-coord] that)))
  (hashCode [_] (hash [x-coord y-coord]))
  IPersistentCollection
  (equiv [this that]
    (.equals this that)))
=> user.Cell
(= (->Cell 1 2) [1 2])
=> true
(= [1 2] (->Cell 1 2))
=> false

juhoteperi15:09:31

clojure.lang.Util/equiv will call Object.equals for non numbers and non IPersistentCollections

potetm15:09:20

@juhoteperi: One of the objects is a vector.

juhoteperi15:09:09

Oh, sorry didn't read the whole discussion.

meow15:09:09

@juhoteperi: I want (cells [x y]) to be true for some set of cells where one of the cells has an x and y value equal to the plain [x y] vector value.

potetm15:09:17

@meow: So I started down the road of extending IPersistentVector so that it will play nice with other vectors in general. Brings up all kinds of interesting questions though. Such as “how will it behave as an associative structure? Will it behave like a vector or a map?"

meow15:09:14

I'm not sure that changing vector is the way to go.

potetm15:09:28

Well, to be clear, that wouldn’t change vector. It would make your type a vector.

meow15:09:31

I have a collection of cells, like a set. I want to test the membership of a cell in that set. Only, instead of using an actual cell, which is going to be expensive for me to calculate and unnecessary, I just want to test membership using a simple [x y] vector.

meow15:09:18

@potetm: good point. I'm still getting used to the clojure way of doing these protocols.

meow15:09:48

I still keep thinking about this in OO terms, like when I did this sort of thing in Python.

potetm15:09:11

Well, to be more clear, it would make your type able to play like a vector. It wouldn’t actually change the type.

potetm15:09:23

Still thinking on it though.

meow15:09:57

and it does that a bit already like with the nth method

meow15:09:26

so I can pass a cell to a function that destructures its args into [x y]

juhoteperi15:09:48

@meow: Do you have some reason why you need = to work with your type? It would be much easier to create new function which works like = but works like you want.

meow15:09:40

@juhoteperi: I'm a total newbie at deftype with clojure so, no, I'm just faking my way through this. 😉

meow15:09:32

except I need to be able to do (cells [x y]) to test if a cell is in a set of cells

juhoteperi15:09:38

If using your own type is complex, I would look into alternative solutions.

meow15:09:08

the code already works with vectors. But the idea is to deftype a Cell with just an x and y field, get that working, then have other types of Cells that have additional fields, like age, color, etc.

meow15:09:46

@juhoteperi: I don't know of an alternative to deftype that would do what I want. defrecord is too much like a map already.

meow15:09:25

And this needs to perform well.

meow15:09:27

I'm using the classic approach of taking a set of cells and turning it into a map of candidate cells with neighbor frequencies:

(defn neighbor-freq-8
  "Returns a map of [x y] neighbor-count pairs."
  [cells]
  (frequencies (mapcat neighborhood-8 cells)))

meow15:09:45

this results in a bunch of vectors, not cells

meow15:09:34

then the existence function turns them back into cells:

(defn exist
  "Returns a cell if destiny will allow, or mother nature brings it to life."
  [survive? birth? cell-maker-f cells [cell-position neighbor-count]]
  (if (cells cell-position)
    (when (survive? neighbor-count) (cell-maker-f cell-position))
    (when (birth? neighbor-count) (cell-maker-f cell-position))))

meow15:09:10

but I want to use the cell-position alone as a test for membership in the original set of cells

potetm15:09:43

@meow: So you’ve nerd sniped me.

potetm15:09:58

I’ve got this:

(deftype Cell [x-coord y-coord]
  java.util.Collection
  (size [this] 2)
  (iterator [this] (.iterator [x-coord y-coord]))
  IPersistentCollection
  (seq [this] (seq [x-coord y-coord]))
  (empty [this] (throw (ex-info "")))
  (equiv [this o] (.equals this o))
  ISeq
  (first [this] (first [x-coord y-coord]))
  (next [this] (next [x-coord y-coord]))
  (more [this] (rest [x-coord y-coord]))
  IPersistentVector
  (length [_] 2)
  (assocN [_ i val] "handl this")
  (cons [_ o]
    (throw
      (ex-info "no no no")))
  Object
  (equals [_ that]
    (or (and (instance? Cell that)
             (and (= x-coord (.-x-coord ^Cell that))
                  (= y-coord (.-y-coord ^Cell that))))
        (= [x-coord y-coord] that)))
  (hashCode [_] (hash [x-coord y-coord])))
=> user.Cell
(= [1 2] (->Cell 1 2))
=> true
(= (->Cell 1 2) [1 2])
=> true
(= (->Cell 1 2) (->Cell 1 2))
=> true
(= (->Cell 1 2) {:x-coord 1 :y-coord 2})
=> false
(= {:x-coord 1 :y-coord 2} (->Cell 1 2))
=> false

meow15:09:03

because when I add more fields to the definition of a cell the cell creation process is going to get more expensive.

potetm15:09:16

Definitely not what you want out of the box. Especially for perf. (One obvious thing is it’s constantly creating [x-coord y-coord] on the fly. Probably a better way to handle that.) But it works as a proof of concept.

potetm15:09:57

If you really want that syntax and need speed, you’ll be best of doing tons of performance testing. Playing with equals/hashcode on a structure can definitely affect that.

meow15:09:05

All I have is some basic criterium tests

meow15:09:39

but none of this is really all that complex - any time it starts to get complex I figure out a way to simplify it

meow15:09:42

@potem: that looks like it has most of the behavior I needed. Should work for set membership testing. I need to be away for a bit but when I get back I'll give that code a try.

meow15:09:08

@potetm: thanks for all your help on this. Much appreciated. simple_smile

potetm15:09:45

@meow: My pleasure simple_smile Let me know how it goes!

viesti18:09:27

hmm, getting "Could not locate clojure/data/priority_map__init.class” when trying out “lein new duct app”, with leiningen 2.5.3 and without ~/.lein/profiles.clj even

viesti18:09:09

would have liked to try out 0.4.0 version of duct template (https://github.com/weavejester/duct) but stars aren’t aligned right 😕

ian18:09:35

@viesti: you get that error when you run lein new duct app?

nberger18:09:24

@viesti: I remember someone was getting that error not too long ago because of some dependency in ~/.lein/profiles.clj

viesti18:09:52

yep, so I nuked it but still getting that error 😕

nberger19:09:12

@viesti: shot to the moon... Do you have a project.clj in your current or any of your parent directories?

nberger19:09:31

(I think that was also happening to someone)

viesti19:09:24

yes I do actually simple_smile

viesti19:09:23

started out a new project, then saw the 0.4.0 template and grabbed the project.clj that I had tweaked out into a parent directory and ran lein new there...

viesti19:09:15

@nberger: thanks for the tip, that was it simple_smile

rauh19:09:34

Any reasons I shouldn't use nil as a map key?

meow20:09:51

@rauh: interesting question. I believe I saw on the http://clojure.org site that anything can be used as the key to a map.

rauh20:09:55

Yeah it's def. working with nil. I was just curious if there is some things I should watch out for.

meow20:09:03

it certainly seems to work fine as a key. I'll be curious to know if you run into any problems with it.

rauh20:09:29

I mean obviously, I won't be able to use (poss-nil my-map) to lookup a key and instead have to use get.

danielcompton21:09:00

@rauh you can do that, but because nil is used in so many places as a missing value, I’d suspect you may end up with some weird bugs. If you want to return a default value when nil is passed, then do (get {} :key :value-not-found)

kazuwal21:09:09

Can anybody see why I am getting such strange results from this code

(mapv #(clojure.string/replace % #" " "\\ ") ["/some/path/file 1" "/some/path/file 2"])
I would expect
["/some/path/file\ 1" "/some/path/file\ 2"]
but am getting
["/some/path/file 1" "/some/path/file 2"]

val_waeselynck22:09:16

@adebesin: I think space is \s in regex, but I may be wrong

beppu22:09:49

@adebesin: That is weird. To get the desired result, I ended up having to use "\\\\ ".

beppu22:09:56

@val_waeselynck: The regexp is matching, but it's as if some unescaping happens after the fact.

beppu22:09:29

(What you said about \s is true though.)

kazuwal22:09:57

@beppu using "\\\\ " gives me ["/some/path/file\\ 1" "/some/path/file\\ 2"] im using Clojure 1.7.. Java's String replace method appears to handle this fine, using Java not Clojure.