This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
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.
How can I check whether a sequence contains a value? Something in Python’s in?
@pupeno: http://stackoverflow.com/questions/3249334/test-whether-a-list-contains-a-specific-value-in-clojure
Thanks.
@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
@beppu @nooga When I read about Pulsar, I wondered what benefit it has compared to using core.async
@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).
Sometimes, you want to coordinate actions across different servers, and this might be a good way to accomplish that.
It has a core.async compatible API if you wanted to use pulsar's actors through that kind of API.
and, like @beppu mentioned, actors can live and communicate across processes and machines, even if they’re anonymous
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.
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.
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))))
So right now I'm getting the following, which is not what I want:
(= (cell [1 2]) [1 2])
=> false
@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;
}
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)))
and it would be nice if there was a master list of methods that need to be defined for what purpose
well, I'm comparing to a vector, which is probably an instance of IPersistentCollection
so I think I need to define equiv
on Cell
It appears, even if you extend IPersistentCollection
for your type, (= (cell 1 2) [1 2]) => true
and (= [1 2] (cell 1 2)) => false
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.
Maybe I should extend IPersistentCollection
- that would at least get me the most important part of comparing a vector to a set of cells
(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
clojure.lang.Util/equiv will call Object.equals for non numbers and non IPersistentCollections
@juhoteperi: One of the objects is a vector.
Oh, sorry didn't read the whole discussion.
@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.
@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?"
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.
@potetm: good point. I'm still getting used to the clojure way of doing these protocols.
I still keep thinking about this in OO terms, like when I did this sort of thing in Python.
Well, to be more clear, it would make your type able to play like a vector. It wouldn’t actually change the type.
@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.
@juhoteperi: I'm a total newbie at deftype with clojure so, no, I'm just faking my way through this. 😉
If using your own type is complex, I would look into alternative solutions.
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.
This is for my cellular automata library: https://github.com/decomplect/ion/blob/master/src/ion/ergo/core.cljc
@juhoteperi: I don't know of an alternative to deftype that would do what I want. defrecord is too much like a map already.
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)))
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))))
but I want to use the cell-position alone as a test for membership in the original set of cells
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
because when I add more fields to the definition of a cell the cell creation process is going to get more expensive.
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.
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.
but none of this is really all that complex - any time it starts to get complex I figure out a way to simplify it
@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.
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
would have liked to try out 0.4.0 version of duct template (https://github.com/weavejester/duct) but stars aren’t aligned right 😕
@viesti: I remember someone was getting that error not too long ago because of some dependency in ~/.lein/profiles.clj
@viesti: shot to the moon... Do you have a project.clj in your current or any of your parent directories?
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...
@rauh confusing bugs?
@rauh: interesting question. I believe I saw on the http://clojure.org site that anything can be used as the key to a map.
Yeah it's def. working with nil. I was just curious if there is some things I should watch out for.
it certainly seems to work fine as a key. I'll be curious to know if you run into any problems with it.
I mean obviously, I won't be able to use (poss-nil my-map)
to lookup a key and instead have to use get
.
@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)
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"]
@adebesin: I think space is \s in regex, but I may be wrong
@val_waeselynck: The regexp is matching, but it's as if some unescaping happens after the fact.