Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread atreyu
impenetrable for builtin curried functions and . operator as comp?? i
dont think so, haskell can be hard to read but not for those reasons
F. e. filter when the sum of elements are even:

Prelude> filter (even.sum) [[2,3],[3,3]]
[[3,3]]

i think is pretty readable ;-)

On Nov 16, 2:19 am, Cyrus Harmon  wrote:
> I think the minimal character count for composition and partial functions in 
> haskell are some of the reasons that haskell code is so impenetrable to 
> non-haskell hackers. Feel free to rig up crazy unicode characters to any 
> identifier you want in your own code, just don't ask me to read or debug any 
> of it.
>
> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>
> > Coming from Haskell, where composition and partial functions are cheap and 
> > free in terms of character count, it is actually pretty discouraging to 
> > have to spell it out in Clojure for the same effect.  Some of the cases 
> > where you "should" be using multiple expressions in Clojure would be 
> > perfectly clear in Haskell as one expression...
>
> > On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield  
> > wrote:
> > On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
> > > The one that bugs me is complement - such a long name for a commonly-
> > > useful function. I often wind up defining ! as an alias for
> > > complement, but maybe others will think that is poor style.
>
> > Possibly because bang functions indicate "Here be dragons" in terms of
> > mutating state? e.g., set!
>
> > Are you really using complement a lot? I guess I would define an alias
> > for the complement-ed function or use not in expressions...
> > --
> > Sean A Corfield -- (904) 302-SEAN
> > Railo Technologies, Inc. --http://getrailo.com/
> > An Architect's View --http://corfield.org/
>
> > "If you're not annoying somebody, you're not really alive."
> > -- Margaret Atwood
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with 
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with 
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Ken Wesson
On Tue, Nov 16, 2010 at 3:23 AM, atreyu  wrote:
> impenetrable for builtin curried functions and . operator as comp?? i
> dont think so, haskell can be hard to read but not for those reasons
> F. e. filter when the sum of elements are even:
>
> Prelude> filter (even.sum) [[2,3],[3,3]]
> [[3,3]]
>
> i think is pretty readable ;-)

user=> (filter (comp even? (partial apply +)) [[2 3] [3 3]])
([3 3])

is not too shabby either ;)

The major bit of ickiness is the need to use (partial apply +) to get
sum. If you (defn sum [coll] (apply + coll)) then you can use

(filter (comp even? sum) [[2 3] [3 3]])

which avoids icky infix notation ;) and can be shortened further if
you alias comp with e.g.

(def o comp)

to

(filter (o even? sum) [[2 3] [3 3]])

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Performance of seq on empty collections

2010-11-16 Thread Laurent PETIT
2010/11/16 Meikel Brandmeyer 

> Hi,
>
> On 16 Nov., 02:31, Ken Wesson  wrote:
>
> > Eh. I'd heard first and rest had replaced next. No?
>
> No. This was a misinformation. To elaborate a bit more on the
> differences pointed out by Sean:
>
> next returns nil if there are no more items in the sequence, which is
> nice to use in if and when statements to test for emptiness. However
> in order to be able to return nil the sequence has to be realised.
> Otherwise you can't know whether to return nil or not. So next is "non-
> lazy of order 1" so to say. To allow full laziness lazy-seq was
> introduced. It returns an object which doesn't know directly whether
> it's empty, but it knows how to find out, when you ask for it. When
> you know call empty? on the lazy-seq you realise the sequence and the
> seq call hidden in empty will return most likely a Cons, where first
> and rest are just field references, or nil of there are no elements in
> the sequence. But you throw this away. Then you call first on the lazy-
> seq. first itself calls seq and gets again the now cached Cons. Then
> you call rest, which calls seq, which returns the cached Cons. A lot
> of indirection going on. If you handle non-seqs like vectors or maps,
> you have to replace that with multiple creations of very short-lived
> objects. This is even more expensive.
>
> So you can see, that by capturing the return value of the seq call you
> can save quite a bit of indirection. Plus seq becomes the identity,
> which should be fast.
>
> I did some quick'n'dirty benchmarking and the improvement seems to be
> 12% for me. Not tht much, but quite a bit for such trivial
> changes.
>

Hello,

Agreed with the explanation, but ... 12% of what, exactly ?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: bimaps in clojure

2010-11-16 Thread Christophe Grand
You can implement your own, prettu easily with deftype.
However it can be tedious to track every methods, so we need a repl helper
function to scaffold the code for us.

(defn scaffold [iface]
  (doseq [[iface methods] (->> iface .getMethods
(map #(vector (.getName (.getDeclaringClass %))
(symbol (.getName %))
(count (.getParameterTypes %
(group-by first))]
(println (str "  " iface))
(doseq [[_ name argcount] methods]
  (println
(str ""
  (list name (into ['this] (take argcount (repeatedly
gensym)

user=> (scaffold clojure.lang.IPersistentMap)
  clojure.lang.IPersistentMap
(assoc [this G__441 G__442])
(without [this G__443])
(assocEx [this G__444 G__445])
  java.lang.Iterable
(iterator [this])
  clojure.lang.Associative
(containsKey [this G__446])
(assoc [this G__447 G__448])
(entryAt [this G__449])
  clojure.lang.IPersistentCollection
(count [this])
(cons [this G__450])
(empty [this])
(equiv [this G__451])
  clojure.lang.Seqable
(seq [this])
  clojure.lang.ILookup
(valAt [this G__452 G__453])
(valAt [this G__454])
  clojure.lang.Counted
(count [this])
nil

Now you need to remove the duplicates (count and assoc), wrap everything in
a deftype form and implement it:
(including a new protocol fn to reverse a bidi map)

(defprotocol ReversibleMap
  (rmap [m]))

(defn- rdissoc [d r v]
 (if-let [[_ k] (find r v)] (dissoc d k) d))

(deftype Bimap [^clojure.lang.IPersistentMap direct reverse]
  Object
  (hashCode [x]
(.hashCode direct))
  (equals [x y]
(.equals direct y))
  clojure.lang.IPersistentMap
(without [this k]
  (Bimap.
(dissoc direct k)
(rdissoc reverse direct k)))
(assocEx [this k v]
  (if (or (contains? direct k) (contains? reverse v))
(throw (Exception. "Key or value already present"))
(assoc this k v)))
  java.lang.Iterable
(iterator [this]
  (.iterator direct))
  clojure.lang.Associative
(assoc [this k v]
  (Bimap.
(assoc (rdissoc direct reverse v) k v)
(assoc (rdissoc reverse direct k) v k)))
(containsKey [this k]
  (contains? direct k))
(entryAt [this k]
  (find direct k))
  clojure.lang.IPersistentCollection
(cons [this x]
  (if-let [[k v] (and (vector? x) x)]
(assoc this k v)
(reduce (fn [m [k v]] (assoc m k v)) this x)))
(empty [this]
  (.empty direct))
(equiv [this x]
  (.equiv direct x))
  clojure.lang.Seqable
(seq [this]
  (seq direct))
  clojure.lang.ILookup
(valAt [this k else]
  (direct k else))
(valAt [this k]
  (direct k))
  clojure.lang.Counted
(count [this]
  (count direct))
  ReversibleMap
(rmap [this]
  (Bimap. reverse direct)))

(defn bimap [& kvs]
  (reduce (fn [m [k v]] (assoc m k v)) (Bimap. {} {}) (partition 2 kvs)))

Some quick tests:
user=> (assoc (bimap :a 1 :b 2) [:c 3] [:d 4] [:e 5])
{[:e 5] nil, [:c 3] [:d 4], :b 2, :a 1}
user=> (assoc (bimap :a 1 :b 2) :c 3 :d 4 :e 5)
{:e 5, :d 4, :c 3, :b 2, :a 1}
user=> (assoc (bimap :a 1 :b 2) :c 3 :d 2 :e 5)
{:e 5, :d 2, :c 3, :a 1}
user=> (dissoc *1 :d)
{:e 5, :c 3, :a 1}
user=> (rmap *1)
{5 :e, 3 :c, 1 :a}
user=> (assoc *1 2 :c)
{2 :c, 5 :e, 1 :a}

hth,

Christophe


On Tue, Nov 16, 2010 at 8:16 AM, Sunil S Nandihalli <
sunil.nandiha...@gmail.com> wrote:

> A bimap is a map where each elements of the pair can be used as key to
> access it..
> Sunil.
>
>
> On Tue, Nov 16, 2010 at 12:44 PM, Sunil S Nandihalli <
> sunil.nandiha...@gmail.com> wrote:
>
>> Hello everybody,
>>
>> Is there something like a bimap in clojure? I know I can have two regular
>> hash-maps .. but I was wondering if there is a better implementation..?
>>
>>  a similar implementation in c++ is
>>
>> http://beta.boost.org/doc/libs/1_41_0/libs/bimap/doc/html/index.html
>>
>> Thanks,
>> Sunil.
>>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>



-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Performance of seq on empty collections

2010-11-16 Thread Meikel Brandmeyer
Salut Laurent,

On 16 Nov., 09:51, Laurent PETIT  wrote:

> Agreed with the explanation, but ... 12% of what, exactly ?

6,502692ms / 7,393586ms ~ 0,88 => 12% improvement, no?

But maybe this whole microbenchmarking story is paper waste. As I
said: quick'n'dirty.

Will now drop out of perfomance discussions...

Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread atreyu
clojure is nice too for the example but if you'd add functions and
they have arity more than 1 haskell gets better (imo of course):

user> (def concat2 (partial apply concat))
#'user/concat2
user> ((comp (partial map (partial + 5)) concat2) (filter (comp even?
sum) [[2 3] [3 3] [6 6]]))
(8 8 11 11)

I added unnecessary parentheses to get closer to a lisp expression

Prelude> ((map (5+).concat) (filter (even.sum) [[2,3],[3,3],[6,6]]))
[8,8,11,11]

but dont get me wrong i really like clojure :-P and the difference
doesn't matter very much for me. With symbols would be...
((∘ (𝒫 map (𝒫 + 5)) concat2) (filter (comp even? sum) [[2 3] [3 3]
[6 6]]))


On Nov 16, 9:34 am, Ken Wesson  wrote:
> On Tue, Nov 16, 2010 at 3:23 AM, atreyu  wrote:
> > impenetrable for builtin curried functions and . operator as comp?? i
> > dont think so, haskell can be hard to read but not for those reasons
> > F. e. filter when the sum of elements are even:
>
> > Prelude> filter (even.sum) [[2,3],[3,3]]
> > [[3,3]]
>
> > i think is pretty readable ;-)
>
> user=> (filter (comp even? (partial apply +)) [[2 3] [3 3]])
> ([3 3])
>
> is not too shabby either ;)
>
> The major bit of ickiness is the need to use (partial apply +) to get
> sum. If you (defn sum [coll] (apply + coll)) then you can use
>
> (filter (comp even? sum) [[2 3] [3 3]])
>
> which avoids icky infix notation ;) and can be shortened further if
> you alias comp with e.g.
>
> (def o comp)
>
> to
>
> (filter (o even? sum) [[2 3] [3 3]])

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Meikel Brandmeyer
Hi,

On 16 Nov., 11:06, atreyu  wrote:

> clojure is nice too for the example but if you'd add functions and
> they have arity more than 1 haskell gets better (imo of course):

And less than 3 if one is honest. Haskell is spicked with rather
unmotivated "`foo` x"s which simply means #(foo % x) in a - IMHO -
rather unobvious way. Do #(foo x % y) in Haskell... I'm missing the
elegance somehow. In the end this is only superficial and does not
affect the power of the underlying language in any way.

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread atreyu
Yep, you have to use flip and it is not so elegant

Prelude> let f x y z=(x+z)*y
Prelude> map (flip (f 1) 2) [3,4,5]
[9,12,15]

OTOH in clojure we have, you guess..., macros!!!

user> (->> [[2 3][3 3][6 6]] (filter (comp even? sum)) concat2 (map
#(+ 5 %)))
(8 8 11 11)

i promise i'll not comment about (superficial) syntax in a month!!
:-P

On Nov 16, 11:35 am, Meikel Brandmeyer  wrote:
> Hi,
>
> On 16 Nov., 11:06, atreyu  wrote:
>
> > clojure is nice too for the example but if you'd add functions and
> > they have arity more than 1 haskell gets better (imo of course):
>
> And less than 3 if one is honest. Haskell is spicked with rather
> unmotivated "`foo` x"s which simply means #(foo % x) in a - IMHO -
> rather unobvious way. Do #(foo x % y) in Haskell... I'm missing the
> elegance somehow. In the end this is only superficial and does not
> affect the power of the underlying language in any way.
>
> Sincerely
> Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Generating random regular graphs

2010-11-16 Thread Tiemo
Hi,

I need to implement an algorithm to generate large random d-regular
graphs. A d-regular graph is a graph in which all vertices have degree
d. Specifically I need to generate large (1000+ vertices) 4-regular
graphs. The algorithm I need to use is as follows:

let V be a set of n vertices with d legs
while |legs| > 0
find a random leg and connect with it
done

A leg is an edge that is not yet connected. It is important that the
algorithm iterates over the free legs in order, and then connects them
to a random leg on some edge. Edges that connect a vertex with itself
(tadpoles) are allowed.

To implement this simple algorithm in Java didn't take long, but I
want to try implementing it in Clojure, the problem is that, as a
clojure noob, I end up with imperative looking code.  So how can I
tackle this problem such that the algorithm is followed precisely
(it's important because this algorithm ensures certain properties) and
I end up with efficient code?

The representation of the graph is currently a vector of vertex
structs:
(defstruct vertex :id :neighbours)

The id is there just for drawing. Neighbours is a d-sized vector. A
adjacency matrix is out of the question here because I'm concerned
with large sparsely connected graphs.

Any help is appreciated.

- Tiemo.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Eric Schulte
Hi Paul,

Thanks for sharing this.  It seems like the best compromise between the
desire to keep my code brief (at least to my eyes) without wanting to
introduce my own custom function names for global functions.

If you don't mind I'd like to add this to my fork of the Emacs Starter
Kit (will credit you as author).

Best -- Eric

Paul Hobbs  writes:

> Well, for those who use emacs, you could always make it *look* like it was
> pretty... For example:
>
> (eval-after-load 'clojure-mode
>   '(font-lock-add-keywords
> 'clojure-mode `(("\\"
>  (0 (progn (compose-region
> (match-beginning 0) (match-end 0)
> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
>nil)))
> ("\\"
>  (0
>   (progn (compose-region
>   (match-beginning 0) (match-end 0)
>   "∘ ")
>  nil)))
> ("\\"
>  (0
>   (progn (compose-region
>   (match-beginning 0) (match-end 0)
>   "𝒫 ")))
>
> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon  wrote:
>
>>
>> I think the minimal character count for composition and partial functions
>> in haskell are some of the reasons that haskell code is so impenetrable to
>> non-haskell hackers. Feel free to rig up crazy unicode characters to any
>> identifier you want in your own code, just don't ask me to read or debug any
>> of it.
>>
>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>>
>> Coming from Haskell, where composition and partial functions are cheap and
>> free in terms of character count, it is actually pretty discouraging to have
>> to spell it out in Clojure for the same effect.  Some of the cases where you
>> "should" be using multiple expressions in Clojure would be perfectly clear
>> in Haskell as one expression...
>>
>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield 
>> wrote:
>>
>>> On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
>>> > The one that bugs me is complement - such a long name for a commonly-
>>> > useful function. I often wind up defining ! as an alias for
>>> > complement, but maybe others will think that is poor style.
>>>
>>> Possibly because bang functions indicate "Here be dragons" in terms of
>>> mutating state? e.g., set!
>>>
>>> Are you really using complement a lot? I guess I would define an alias
>>> for the complement-ed function or use not in expressions...
>>> --
>>> Sean A Corfield -- (904) 302-SEAN
>>> Railo Technologies, Inc. -- http://getrailo.com/
>>> An Architect's View -- http://corfield.org/
>>>
>>> "If you're not annoying somebody, you're not really alive."
>>> -- Margaret Atwood
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>>
>>
>>  --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Eric Schulte
atreyu  writes:

> Yep, you have to use flip and it is not so elegant
>
> Prelude> let f x y z=(x+z)*y
> Prelude> map (flip (f 1) 2) [3,4,5]
> [9,12,15]
>
> OTOH in clojure we have, you guess..., macros!!!
>
> user> (->> [[2 3][3 3][6 6]] (filter (comp even? sum)) concat2 (map
> #(+ 5 %)))
> (8 8 11 11)
>

Too bad Liskell [1] is dead, or there would be a language with both
currying and macros (aside from forth/factor which probably have both
but don't count).

Personally, I'd be happy to give up multivariadic functions (they always
seem to need `apply' anyways) in exchange for a simpler curried language
in which functions can only take a single argument.  I'm not trying to
suggest that this would be good for Clojure (or for anything implemented
over the java VM) this is just a property I'd want in my perfect lisp.

>
> i promise i'll not comment about (superficial) syntax in a month!!
> :-P
>
> On Nov 16, 11:35 am, Meikel Brandmeyer  wrote:
>> Hi,
>>
>> On 16 Nov., 11:06, atreyu  wrote:
>>
>> > clojure is nice too for the example but if you'd add functions and
>> > they have arity more than 1 haskell gets better (imo of course):
>>
>> And less than 3 if one is honest. Haskell is spicked with rather
>> unmotivated "`foo` x"s which simply means #(foo % x) in a - IMHO -
>> rather unobvious way. Do #(foo x % y) in Haskell... I'm missing the
>> elegance somehow. In the end this is only superficial and does not
>> affect the power of the underlying language in any way.
>>
>> Sincerely
>> Meikel

Footnotes: 
[1]  http://www.liskell.org/
 http://blog.clemens.endorphin.org/2009/01/liskell-standalone.html

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or "&" argument?

2010-11-16 Thread Chris Maier
That makes sense... thanks, Meikel.

Maybe my example of + wasn't the best, given its mathematical nature.

Here's my situation: I'm writing some software to analyze some protein
datasets, part of which entails generating a Venn diagram of their
intersections.  Each dataset has a unique name, and my function looks
up the data by this name in a database.  Right now the function takes
a single "&" parameter of "datasets"; you give it as many dataset
names as you want, and it gives you the intersection breakdown.
Internally, this function just treats "datasets" like the sequence it
is.

So I guess a better way to state my question would be this: if you
only have a function that operates on a sequence of items, and it
could be written either as

(def foo [& stuff] ... )

or

(def foo [stuff] ... ) ;; where 'stuff' is a sequence

are there any concerns besides (for lack of a better term) the
"ergonomics" of calling such functions that should influence which
signature to use?

Does that make sense?

Thanks,
Chris

On Mon, Nov 15, 2010 at 2:08 PM, Meikel Brandmeyer  wrote:
> Hi,
>
> Am 15.11.2010 um 17:52 schrieb Chris:
>
>> If you have a function that needs to treat multiple arguments as a
>> group, what forces drive you to represent this as a single sequence
>> argument vs. an "&" argument?  To give a concrete example, why does
>> "+" work like
>>
>> (+ 1 2 3 4)
>>
>> instead of
>>
>> (+ [1 2 3 4])
>>
>> Is it performance?  Aesthetics?  Composability concerns?  Not having
>> to call "apply" all the time?
>
> Semantics?
>
> + is a binary operator. Allowing more arguments is a convenience I guess. The 
> mathematically honest definition would have arity 2.
>
> Eg. filter on the other hand acts on a sequence. That's why it is not defined 
> as (filter even? 1 2 3 4). (See also map for example where apply wouldn't 
> help.)
>
> Does this make sense?
>
> Sincerely
> Meikel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or "&" argument?

2010-11-16 Thread Christophe Grand
Hi,

On Tue, Nov 16, 2010 at 5:21 PM, Chris Maier wrote:

> That makes sense... thanks, Meikel.
>
> Maybe my example of + wasn't the best, given its mathematical nature.
>
> Here's my situation: I'm writing some software to analyze some protein
> datasets, part of which entails generating a Venn diagram of their
> intersections.  Each dataset has a unique name, and my function looks
> up the data by this name in a database.  Right now the function takes
> a single "&" parameter of "datasets"; you give it as many dataset
> names as you want, and it gives you the intersection breakdown.
> Internally, this function just treats "datasets" like the sequence it
> is.
>
> So I guess a better way to state my question would be this: if you
> only have a function that operates on a sequence of items, and it
> could be written either as
>
> (def foo [& stuff] ... )
>
> or
>
> (def foo [stuff] ... ) ;; where 'stuff' is a sequence
>
> are there any concerns besides (for lack of a better term) the
> "ergonomics" of calling such functions that should influence which
> signature to use?
>

If you really want to treat your sequence lazily pass it explicitely and not
as varargs.

You can also provide both: clojure.core has #'conj and #'into for example.

hth,

  Christophe

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Newbie question

2010-11-16 Thread Lee Hinman
On Fri, Nov 12, 2010 at 1:54 PM,   wrote:
> Which natural language processing tools have you used that worked well with
> clojure?

I've had good luck with clojure-opennlp -
http://github.com/dakrone/clojure-opennlp

However, I may be a bit biased since I wrote it :)

- Lee

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Newbie question

2010-11-16 Thread Lee Hinman
On Tue, Nov 16, 2010 at 9:34 AM, Lee Hinman  wrote:
> On Fri, Nov 12, 2010 at 1:54 PM,   wrote:
>> Which natural language processing tools have you used that worked well with
>> clojure?
>
> I've had good luck with clojure-opennlp -
> http://github.com/dakrone/clojure-opennlp
>
> However, I may be a bit biased since I wrote it :)
>

You should also check out Aria's work with NLP and Clojure:

https://github.com/aria42

- Lee

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or "&" argument?

2010-11-16 Thread Chris Maier
Ah, laziness... thanks Christophe.  For my particular application
laziness doesn't matter (so that hadn't occurred to me) but that's a
good general principle to keep in mind.

Thanks,
Chris

On Tue, Nov 16, 2010 at 11:31 AM, Christophe Grand
 wrote:
> Hi,
>
> On Tue, Nov 16, 2010 at 5:21 PM, Chris Maier 
> wrote:
>>
>> That makes sense... thanks, Meikel.
>>
>> Maybe my example of + wasn't the best, given its mathematical nature.
>>
>> Here's my situation: I'm writing some software to analyze some protein
>> datasets, part of which entails generating a Venn diagram of their
>> intersections.  Each dataset has a unique name, and my function looks
>> up the data by this name in a database.  Right now the function takes
>> a single "&" parameter of "datasets"; you give it as many dataset
>> names as you want, and it gives you the intersection breakdown.
>> Internally, this function just treats "datasets" like the sequence it
>> is.
>>
>> So I guess a better way to state my question would be this: if you
>> only have a function that operates on a sequence of items, and it
>> could be written either as
>>
>> (def foo [& stuff] ... )
>>
>> or
>>
>> (def foo [stuff] ... ) ;; where 'stuff' is a sequence
>>
>> are there any concerns besides (for lack of a better term) the
>> "ergonomics" of calling such functions that should influence which
>> signature to use?
>
> If you really want to treat your sequence lazily pass it explicitely and not
> as varargs.
>
> You can also provide both: clojure.core has #'conj and #'into for example.
>
> hth,
>
>   Christophe
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Generating random regular graphs

2010-11-16 Thread Ken Wesson
I took a stab at it:

(defn make-verts [n]
  (zipmap (range n) (repeat [])))

(defn free-legs? [m i d]
  (< (count (m i)) d))

(defn free-leg-indices [m n d]
  (filter #(free-legs? m % d) (range n)))

(defn first-free-leg [m n d]
  (first (free-leg-indices m n d)))

(defn random-free-leg [m n d]
  (let [fli (free-leg-indices m n d)
fl-counts (zipmap fli (map #(- d (count (m %))) fli))
fli2 (mapcat #(repeat (fl-counts %) %) fli)]
(nth fli2 (rand-int (count fli2)

(defn add-random-edge [m n d]
  (let [i1 (first-free-leg m n d)
i2 (random-free-leg (update-in m [i1] conj 'dummy) n d)]
(update-in (update-in m [i1] conj i2) [i2] conj i1)))

(defn d-regular-graph-edges [n d]
  (/ (* n d) 2))

(defn random-d-regular-graph [n d]
  (nth
(iterate #(add-random-edge % n d) (make-verts n))
(d-regular-graph-edges n d)))


It seems to work and looks pretty functional to me.

user=> (random-d-regular-graph 5 2)
{4 [0 0],
 3 [1 1],
 2 [2 2],
 1 [3 3],
 0 [4 4]}
user=> (random-d-regular-graph 5 2)
{4 [1 3],
 3 [1 4],
 2 [2 2],
 1 [3 4],
 0 [0 0]}
user=> (random-d-regular-graph 5 2)
{4 [0 2],
 3 [0 1],
 2 [1 4],
 1 [2 3],
 0 [3 4]}
user=> (random-d-regular-graph 4 3)
{3 [0 1 2],
 2 [0 1 3],
 1 [0 2 3],
 0 [1 3 2]}
user=> (random-d-regular-graph 4 3)
{3 [0 0 1],
 2 [1 2 2],
 1 [0 3 2],
 0 [1 3 3]}

Obviously it b0rks if the product of the number of vertices and the
degree isn't even:

user=> (random-d-regular-graph 3 3)
{2 [1 1],
 1 [0 2 2],
 0 [1 0 0]}

As you can see there's one free leg left over.

The only slightly tricky bit in the code is the (update-in ...
'dummy). It's there to avoid this:

user=> (random-d-regular-graph 5 2)
{4 [0 0],
 3 [1],
 2 [1 2 2],
 1 [3 2],
 0 [4 4]}

In this example, for the final random edge it took a free leg on 2 and
saw that 2 and 3 still had free legs and randomly picked 2. So, when
it goes to pick the other leg to join it has to view the leg already
chosen as already taken.

Now, this picks one leg for each new edge to be the first in a fixed
traversal order of verts-then-legs and the other randomly and
uniformly from all of the other free legs remaining. If that's not
what you wanted, say if in particular having two edges from the same
node to the same other node is verboten, I hope at least that the
outline of the code shows you how this kind of thing can be done
without mutables or even loop/recur and that you can adapt your own
algorithm to the same basic structure -- that you can see where the
random selection of the second leg of a new edge occurs and where you
might make it pick in a different way or avoid parallel pairs of
edges, etc.

If you want global properties such as connectedness, that's more
complicated; my first inclination would be to wrap the whole thing in
a (first (remove is-bad-in-some-way? (repeatedly
#(random-d-regular-graph n d but I expect that would slow to a
crawl if is-bad-in-some-way? is usually true for a fully general
random graph and either n or d is at all large.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Eric Schulte
Just to follow up, I'm now using the following to pretty up Clojure code
in Emacs

#+begin_src emacs-lisp
  ;; symbols for some overlong function names
  (eval-after-load 'clojure-mode
'(font-lock-add-keywords
  'clojure-mode
  (mapcar
   (lambda (pair)
 `(,(car pair)
   (0 (progn (compose-region
  (match-beginning 0) (match-end 0)
  ,(cadr pair))
 nil
   `(("\\" ,(make-char 'greek-iso8859-7 107))
 ("\\" ?∘)
 ("\\" ?þ)
 ("\\" ?¬)
#+end_src

I think the results look quite nice, a small example is attached

Best -- Eric

"Eric Schulte"  writes:

> Hi Paul,
>
> Thanks for sharing this.  It seems like the best compromise between the
> desire to keep my code brief (at least to my eyes) without wanting to
> introduce my own custom function names for global functions.
>
> If you don't mind I'd like to add this to my fork of the Emacs Starter
> Kit (will credit you as author).
>
> Best -- Eric
>
> Paul Hobbs  writes:
>
>> Well, for those who use emacs, you could always make it *look* like it was
>> pretty... For example:
>>
>> (eval-after-load 'clojure-mode
>>   '(font-lock-add-keywords
>> 'clojure-mode `(("\\"
>>  (0 (progn (compose-region
>> (match-beginning 0) (match-end 0)
>> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
>>nil)))
>> ("\\"
>>  (0
>>   (progn (compose-region
>>   (match-beginning 0) (match-end 0)
>>   "∘ ")
>>  nil)))
>> ("\\"
>>  (0
>>   (progn (compose-region
>>   (match-beginning 0) (match-end 0)
>>   "𝒫 ")))
>>
>> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon  wrote:
>>
>>>
>>> I think the minimal character count for composition and partial functions
>>> in haskell are some of the reasons that haskell code is so impenetrable to
>>> non-haskell hackers. Feel free to rig up crazy unicode characters to any
>>> identifier you want in your own code, just don't ask me to read or debug any
>>> of it.
>>>
>>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>>>
>>> Coming from Haskell, where composition and partial functions are cheap and
>>> free in terms of character count, it is actually pretty discouraging to have
>>> to spell it out in Clojure for the same effect.  Some of the cases where you
>>> "should" be using multiple expressions in Clojure would be perfectly clear
>>> in Haskell as one expression...
>>>
>>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield 
>>> wrote:
>>>
 On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
 > The one that bugs me is complement - such a long name for a commonly-
 > useful function. I often wind up defining ! as an alias for
 > complement, but maybe others will think that is poor style.

 Possibly because bang functions indicate "Here be dragons" in terms of
 mutating state? e.g., set!

 Are you really using complement a lot? I guess I would define an alias
 for the complement-ed function or use not in expressions...
 --
 Sean A Corfield -- (904) 302-SEAN
 Railo Technologies, Inc. -- http://getrailo.com/
 An Architect's View -- http://corfield.org/

 "If you're not annoying somebody, you're not really alive."
 -- Margaret Atwood

 --
 You received this message because you are subscribed to the Google
 Groups "Clojure" group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>>
<>-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@goog

Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Alan
Ask on #clojure about this. Someone (hiredman, I think?) has a macro
that rewrites code using some funky unicode characters. I can't find
it at the moment, but it might be what you're looking for.

On Nov 16, 9:51 am, "Eric Schulte"  wrote:
> Just to follow up, I'm now using the following to pretty up Clojure code
> in Emacs
>
> #+begin_src emacs-lisp
>   ;; symbols for some overlong function names
>   (eval-after-load 'clojure-mode
>     '(font-lock-add-keywords
>       'clojure-mode
>       (mapcar
>        (lambda (pair)
>          `(,(car pair)
>            (0 (progn (compose-region
>                       (match-beginning 0) (match-end 0)
>                       ,(cadr pair))
>                      nil
>        `(("\\" ,(make-char 'greek-iso8859-7 107))
>          ("\\" ?∘)
>          ("\\" ?þ)
>          ("\\" ?¬)
> #+end_src
>
> I think the results look quite nice, a small example is attached
>
>  abbrev-function-names.png
> 5KViewDownload
>
>
>
> Best -- Eric
>
> "Eric Schulte"  writes:
> > Hi Paul,
>
> > Thanks for sharing this.  It seems like the best compromise between the
> > desire to keep my code brief (at least to my eyes) without wanting to
> > introduce my own custom function names for global functions.
>
> > If you don't mind I'd like to add this to my fork of the Emacs Starter
> > Kit (will credit you as author).
>
> > Best -- Eric
>
> > Paul Hobbs  writes:
>
> >> Well, for those who use emacs, you could always make it *look* like it was
> >> pretty... For example:
>
> >> (eval-after-load 'clojure-mode
> >>   '(font-lock-add-keywords
> >>     'clojure-mode `(("\\"
> >>      (0 (progn (compose-region
> >> (match-beginning 0) (match-end 0)
> >> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
> >>        nil)))
> >>     ("\\"
> >>      (0
> >>       (progn (compose-region
> >>       (match-beginning 0) (match-end 0)
> >>       "∘ ")
> >>      nil)))
> >>     ("\\"
> >>      (0
> >>       (progn (compose-region
> >>       (match-beginning 0) (match-end 0)
> >>       "𝒫 ")))
>
> >> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon  
> >> wrote:
>
> >>> I think the minimal character count for composition and partial functions
> >>> in haskell are some of the reasons that haskell code is so impenetrable to
> >>> non-haskell hackers. Feel free to rig up crazy unicode characters to any
> >>> identifier you want in your own code, just don't ask me to read or debug 
> >>> any
> >>> of it.
>
> >>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>
> >>> Coming from Haskell, where composition and partial functions are cheap and
> >>> free in terms of character count, it is actually pretty discouraging to 
> >>> have
> >>> to spell it out in Clojure for the same effect.  Some of the cases where 
> >>> you
> >>> "should" be using multiple expressions in Clojure would be perfectly clear
> >>> in Haskell as one expression...
>
> >>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield 
> >>> wrote:
>
>  On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
>  > The one that bugs me is complement - such a long name for a commonly-
>  > useful function. I often wind up defining ! as an alias for
>  > complement, but maybe others will think that is poor style.
>
>  Possibly because bang functions indicate "Here be dragons" in terms of
>  mutating state? e.g., set!
>
>  Are you really using complement a lot? I guess I would define an alias
>  for the complement-ed function or use not in expressions...
>  --
>  Sean A Corfield -- (904) 302-SEAN
>  Railo Technologies, Inc. --http://getrailo.com/
>  An Architect's View --http://corfield.org/
>
>  "If you're not annoying somebody, you're not really alive."
>  -- Margaret Atwood
>
>  --
>  You received this message because you are subscribed to the Google
>  Groups "Clojure" group.
>  To post to this group, send email to clojure@googlegroups.com
>  Note that posts from new members are moderated - please be patient with
>  your first post.
>  To unsubscribe from this group, send email to
>  clojure+unsubscr...@googlegroups.com
>  For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "Clojure" group.
> >>> To post to this group, send email to clojure@googlegroups.com
> >>> Note that posts from new members are moderated - please be patient with
> >>> your first post.
> >>> To unsubscribe from this group, send email to
> >>> clojure+unsubscr...@googlegroups.com
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/clojure?hl=en
>
> >>>  --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "Clojure" group.
> >>> To post to this group, send email to clojure@googlegroups.com
> >>> Note that posts from new members are moderated - please be patient with
> >>> your first post.
> >>> To unsubscribe f

Re: Function Design: sequence or "&" argument?

2010-11-16 Thread Vagif Verdi
On Nov 15, 8:52 am, Chris  wrote:
> If you have a function that needs to treat multiple arguments as a
> group, what forces drive you to represent this as a single sequence
> argument vs. an "&" argument?  To give a concrete example, why does
> "+" work like
>
> (+ 1 2 3 4)
>
> instead of
>
> (+ [1 2 3 4])
>
> Is it performance?  Aesthetics?  Composability concerns?  Not having
> to call "apply" all the time?
>
> Thanks,
> Chris

The "&" form is a syntax helper. In other words it is intended to
change the source code, not to change the way you program. Thus you
have to look at the intended use of the function.
If you are going to type different number arguments to a function
right in the code yourself, then use &. If you are going to get the
arguments for that function at runtime, then use a single argument.
Examples: map, filter, reduce.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Generating random regular graphs

2010-11-16 Thread Mark Engelberg
I think the simplest and most efficient way to simulate the random
connection process in a functional manner is to start by generating a
shuffled list of d copies of each vertices (this represents the d legs for
each vertex), and then pair them up using partition.  At this point you have
a list of all the undirected edges, so you just add them to your graph.
 This process should work with any graph representation.

So if your vertices are numbered 0 through 3, and you want 3 edges coming
out from each vertex, you begin by shuffling the list:
[0 1 2 3 0 1 2 3 0 1 2 3]
into something like:
[2 1 3 0 3 0 2 2 0 3 1 1]
Then partition this into:
[[2 1] [3 0] [3 0] [2 2] [0 3] [1 1]]
These are your 6 undirected edges which you store in your graph however you
like.

For demonstration purposes, consider a graph representation where an
undirected edge is stored as two directed edges, vertex ids are numbers in
(range number-of-nodes), and the graph is just a vector that associates
vertex ids with vectors of ids you can get to via outgoing directed edges.

A straightforward implementation of graph-building functions:

(defn empty-graph [number-of-nodes]
 (vec (repeat number-of-nodes [])))

(defn add-directed-edge [graph node1 node2]
 (let [node1-neighbors (graph node1)]
   (assoc graph node1 (conj node1-neighbors node2

(defn add-edge [graph [node1 node2]]
 (-> graph
 (add-directed-edge node1 node2)
 (add-directed-edge node2 node1)))

And the random generation is easy:

(defn random-graph [number-of-nodes number-of-edges]
 (reduce add-edge (empty-graph number-of-nodes)
 (partition 2 (shuffle (take (* number-of-nodes number-of-edges)
 (cycle (range number-of-nodes)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Paul Hobbs
Hi Eric,

Looks good; nice job with complement too.  Of course you can add it to the
Emacs Starter Kit ;-)

--
Paul Hobbs

On Tue, Nov 16, 2010 at 9:51 AM, Eric Schulte wrote:

> Just to follow up, I'm now using the following to pretty up Clojure code
> in Emacs
>
> #+begin_src emacs-lisp
>  ;; symbols for some overlong function names
>   (eval-after-load 'clojure-mode
>'(font-lock-add-keywords
>  'clojure-mode
>   (mapcar
>   (lambda (pair)
> `(,(car pair)
>(0 (progn (compose-region
>  (match-beginning 0) (match-end 0)
>   ,(cadr pair))
> nil
>   `(("\\" ,(make-char 'greek-iso8859-7 107))
> ("\\" ?∘)
> ("\\" ?þ)
> ("\\" ?¬)
> #+end_src
>
> I think the results look quite nice, a small example is attached
>
>
> Best -- Eric
>
> "Eric Schulte"  writes:
>
> > Hi Paul,
> >
> > Thanks for sharing this.  It seems like the best compromise between the
> > desire to keep my code brief (at least to my eyes) without wanting to
> > introduce my own custom function names for global functions.
> >
> > If you don't mind I'd like to add this to my fork of the Emacs Starter
> > Kit (will credit you as author).
> >
> > Best -- Eric
> >
> > Paul Hobbs  writes:
> >
> >> Well, for those who use emacs, you could always make it *look* like it
> was
> >> pretty... For example:
> >>
> >> (eval-after-load 'clojure-mode
> >>   '(font-lock-add-keywords
> >> 'clojure-mode `(("\\"
> >>  (0 (progn (compose-region
> >> (match-beginning 0) (match-end 0)
> >> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
> >>nil)))
> >> ("\\"
> >>  (0
> >>   (progn (compose-region
> >>   (match-beginning 0) (match-end 0)
> >>   "∘ ")
> >>  nil)))
> >> ("\\"
> >>  (0
> >>   (progn (compose-region
> >>   (match-beginning 0) (match-end 0)
> >>   "𝒫 ")))
> >>
> >> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon 
> wrote:
> >>
> >>>
> >>> I think the minimal character count for composition and partial
> functions
> >>> in haskell are some of the reasons that haskell code is so impenetrable
> to
> >>> non-haskell hackers. Feel free to rig up crazy unicode characters to
> any
> >>> identifier you want in your own code, just don't ask me to read or
> debug any
> >>> of it.
> >>>
> >>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
> >>>
> >>> Coming from Haskell, where composition and partial functions are cheap
> and
> >>> free in terms of character count, it is actually pretty discouraging to
> have
> >>> to spell it out in Clojure for the same effect.  Some of the cases
> where you
> >>> "should" be using multiple expressions in Clojure would be perfectly
> clear
> >>> in Haskell as one expression...
> >>>
> >>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield <
> seancorfi...@gmail.com>wrote:
> >>>
>  On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
>  > The one that bugs me is complement - such a long name for a
> commonly-
>  > useful function. I often wind up defining ! as an alias for
>  > complement, but maybe others will think that is poor style.
> 
>  Possibly because bang functions indicate "Here be dragons" in terms of
>  mutating state? e.g., set!
> 
>  Are you really using complement a lot? I guess I would define an alias
>  for the complement-ed function or use not in expressions...
>  --
>  Sean A Corfield -- (904) 302-SEAN
>  Railo Technologies, Inc. -- http://getrailo.com/
>  An Architect's View -- http://corfield.org/
> 
>  "If you're not annoying somebody, you're not really alive."
>  -- Margaret Atwood
> 
>  --
>  You received this message because you are subscribed to the Google
>  Groups "Clojure" group.
>  To post to this group, send email to clojure@googlegroups.com
>  Note that posts from new members are moderated - please be patient
> with
>  your first post.
>  To unsubscribe from this group, send email to
>  clojure+unsubscr...@googlegroups.com
> 
> >
>  For more options, visit this group at
>  http://groups.google.com/group/clojure?hl=en
> 
> >>>
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "Clojure" group.
> >>> To post to this group, send email to clojure@googlegroups.com
> >>> Note that posts from new members are moderated - please be patient with
> >>> your first post.
> >>> To unsubscribe from this group, send email to
> >>> clojure+unsubscr...@googlegroups.com
> >>> For more options, visit this group at
> >>> http://groups.google.com/group/clojure?hl=en
> >>>
> >>>
> >>>  --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "Clojure" group.
> >>> To post to this group, send email to clojure@googlegroups.com
> >>> Note that posts from new members are moderated - please be patient with
> >>> your first post.
> >>> To unsubscribe from thi

Re: Generating random regular graphs

2010-11-16 Thread Tiemo
That is really nice thank you. I'd have to take a real hard look at
it, to see whether or not it's the exact algorithm I need, but
nonetheless, you pointed me in the right direction. The code I
produced was a mess of mutable state and loops. Thank you very much!

On Nov 16, 6:29 pm, Ken Wesson  wrote:
> I took a stab at it:
>
> (defn make-verts [n]
>   (zipmap (range n) (repeat [])))
>
> (defn free-legs? [m i d]
>   (< (count (m i)) d))
>
> (defn free-leg-indices [m n d]
>   (filter #(free-legs? m % d) (range n)))
>
> (defn first-free-leg [m n d]
>   (first (free-leg-indices m n d)))
>
> (defn random-free-leg [m n d]
>   (let [fli (free-leg-indices m n d)
>         fl-counts (zipmap fli (map #(- d (count (m %))) fli))
>         fli2 (mapcat #(repeat (fl-counts %) %) fli)]
>     (nth fli2 (rand-int (count fli2)
>
> (defn add-random-edge [m n d]
>   (let [i1 (first-free-leg m n d)
>         i2 (random-free-leg (update-in m [i1] conj 'dummy) n d)]
>     (update-in (update-in m [i1] conj i2) [i2] conj i1)))
>
> (defn d-regular-graph-edges [n d]
>   (/ (* n d) 2))
>
> (defn random-d-regular-graph [n d]
>   (nth
>     (iterate #(add-random-edge % n d) (make-verts n))
>     (d-regular-graph-edges n d)))
>
> It seems to work and looks pretty functional to me.
>
> user=> (random-d-regular-graph 5 2)
> {4 [0 0],
>  3 [1 1],
>  2 [2 2],
>  1 [3 3],
>  0 [4 4]}
> user=> (random-d-regular-graph 5 2)
> {4 [1 3],
>  3 [1 4],
>  2 [2 2],
>  1 [3 4],
>  0 [0 0]}
> user=> (random-d-regular-graph 5 2)
> {4 [0 2],
>  3 [0 1],
>  2 [1 4],
>  1 [2 3],
>  0 [3 4]}
> user=> (random-d-regular-graph 4 3)
> {3 [0 1 2],
>  2 [0 1 3],
>  1 [0 2 3],
>  0 [1 3 2]}
> user=> (random-d-regular-graph 4 3)
> {3 [0 0 1],
>  2 [1 2 2],
>  1 [0 3 2],
>  0 [1 3 3]}
>
> Obviously it b0rks if the product of the number of vertices and the
> degree isn't even:
>
> user=> (random-d-regular-graph 3 3)
> {2 [1 1],
>  1 [0 2 2],
>  0 [1 0 0]}
>
> As you can see there's one free leg left over.
>
> The only slightly tricky bit in the code is the (update-in ...
> 'dummy). It's there to avoid this:
>
> user=> (random-d-regular-graph 5 2)
> {4 [0 0],
>  3 [1],
>  2 [1 2 2],
>  1 [3 2],
>  0 [4 4]}
>
> In this example, for the final random edge it took a free leg on 2 and
> saw that 2 and 3 still had free legs and randomly picked 2. So, when
> it goes to pick the other leg to join it has to view the leg already
> chosen as already taken.
>
> Now, this picks one leg for each new edge to be the first in a fixed
> traversal order of verts-then-legs and the other randomly and
> uniformly from all of the other free legs remaining. If that's not
> what you wanted, say if in particular having two edges from the same
> node to the same other node is verboten, I hope at least that the
> outline of the code shows you how this kind of thing can be done
> without mutables or even loop/recur and that you can adapt your own
> algorithm to the same basic structure -- that you can see where the
> random selection of the second leg of a new edge occurs and where you
> might make it pick in a different way or avoid parallel pairs of
> edges, etc.
>
> If you want global properties such as connectedness, that's more
> complicated; my first inclination would be to wrap the whole thing in
> a (first (remove is-bad-in-some-way? (repeatedly
> #(random-d-regular-graph n d but I expect that would slow to a
> crawl if is-bad-in-some-way? is usually true for a fully general
> random graph and either n or d is at all large.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Generating random regular graphs

2010-11-16 Thread Tiemo
This is also a nice idea. I will take a look at both implementations.

On Nov 16, 8:16 pm, Mark Engelberg  wrote:
> I think the simplest and most efficient way to simulate the random
> connection process in a functional manner is to start by generating a
> shuffled list of d copies of each vertices (this represents the d legs for
> each vertex), and then pair them up using partition.  At this point you have
> a list of all the undirected edges, so you just add them to your graph.
>  This process should work with any graph representation.
>
> So if your vertices are numbered 0 through 3, and you want 3 edges coming
> out from each vertex, you begin by shuffling the list:
> [0 1 2 3 0 1 2 3 0 1 2 3]
> into something like:
> [2 1 3 0 3 0 2 2 0 3 1 1]
> Then partition this into:
> [[2 1] [3 0] [3 0] [2 2] [0 3] [1 1]]
> These are your 6 undirected edges which you store in your graph however you
> like.
>
> For demonstration purposes, consider a graph representation where an
> undirected edge is stored as two directed edges, vertex ids are numbers in
> (range number-of-nodes), and the graph is just a vector that associates
> vertex ids with vectors of ids you can get to via outgoing directed edges.
>
> A straightforward implementation of graph-building functions:
>
> (defn empty-graph [number-of-nodes]
>  (vec (repeat number-of-nodes [])))
>
> (defn add-directed-edge [graph node1 node2]
>  (let [node1-neighbors (graph node1)]
>    (assoc graph node1 (conj node1-neighbors node2
>
> (defn add-edge [graph [node1 node2]]
>  (-> graph
>      (add-directed-edge node1 node2)
>      (add-directed-edge node2 node1)))
>
> And the random generation is easy:
>
> (defn random-graph [number-of-nodes number-of-edges]
>  (reduce add-edge (empty-graph number-of-nodes)
>          (partition 2 (shuffle (take (* number-of-nodes number-of-edges)
>                                      (cycle (range number-of-nodes)))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Alan
Found it. See 
https://github.com/hiredman/odds-and-ends/blob/8a84e6ddbad9d71f714ba16c3e1239633228a7eb/functional.clj#L94

On Nov 16, 10:06 am, Alan  wrote:
> Ask on #clojure about this. Someone (hiredman, I think?) has a macro
> that rewrites code using some funky unicode characters. I can't find
> it at the moment, but it might be what you're looking for.
>
> On Nov 16, 9:51 am, "Eric Schulte"  wrote:
>
> > Just to follow up, I'm now using the following to pretty up Clojure code
> > in Emacs
>
> > #+begin_src emacs-lisp
> >   ;; symbols for some overlong function names
> >   (eval-after-load 'clojure-mode
> >     '(font-lock-add-keywords
> >       'clojure-mode
> >       (mapcar
> >        (lambda (pair)
> >          `(,(car pair)
> >            (0 (progn (compose-region
> >                       (match-beginning 0) (match-end 0)
> >                       ,(cadr pair))
> >                      nil
> >        `(("\\" ,(make-char 'greek-iso8859-7 107))
> >          ("\\" ?∘)
> >          ("\\" ?þ)
> >          ("\\" ?¬)
> > #+end_src
>
> > I think the results look quite nice, a small example is attached
>
> >  abbrev-function-names.png
> > 5KViewDownload
>
> > Best -- Eric
>
> > "Eric Schulte"  writes:
> > > Hi Paul,
>
> > > Thanks for sharing this.  It seems like the best compromise between the
> > > desire to keep my code brief (at least to my eyes) without wanting to
> > > introduce my own custom function names for global functions.
>
> > > If you don't mind I'd like to add this to my fork of the Emacs Starter
> > > Kit (will credit you as author).
>
> > > Best -- Eric
>
> > > Paul Hobbs  writes:
>
> > >> Well, for those who use emacs, you could always make it *look* like it 
> > >> was
> > >> pretty... For example:
>
> > >> (eval-after-load 'clojure-mode
> > >>   '(font-lock-add-keywords
> > >>     'clojure-mode `(("\\"
> > >>      (0 (progn (compose-region
> > >> (match-beginning 0) (match-end 0)
> > >> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
> > >>        nil)))
> > >>     ("\\"
> > >>      (0
> > >>       (progn (compose-region
> > >>       (match-beginning 0) (match-end 0)
> > >>       "∘ ")
> > >>      nil)))
> > >>     ("\\"
> > >>      (0
> > >>       (progn (compose-region
> > >>       (match-beginning 0) (match-end 0)
> > >>       "𝒫 ")))
>
> > >> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon  
> > >> wrote:
>
> > >>> I think the minimal character count for composition and partial 
> > >>> functions
> > >>> in haskell are some of the reasons that haskell code is so impenetrable 
> > >>> to
> > >>> non-haskell hackers. Feel free to rig up crazy unicode characters to any
> > >>> identifier you want in your own code, just don't ask me to read or 
> > >>> debug any
> > >>> of it.
>
> > >>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>
> > >>> Coming from Haskell, where composition and partial functions are cheap 
> > >>> and
> > >>> free in terms of character count, it is actually pretty discouraging to 
> > >>> have
> > >>> to spell it out in Clojure for the same effect.  Some of the cases 
> > >>> where you
> > >>> "should" be using multiple expressions in Clojure would be perfectly 
> > >>> clear
> > >>> in Haskell as one expression...
>
> > >>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield 
> > >>> wrote:
>
> >  On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
> >  > The one that bugs me is complement - such a long name for a commonly-
> >  > useful function. I often wind up defining ! as an alias for
> >  > complement, but maybe others will think that is poor style.
>
> >  Possibly because bang functions indicate "Here be dragons" in terms of
> >  mutating state? e.g., set!
>
> >  Are you really using complement a lot? I guess I would define an alias
> >  for the complement-ed function or use not in expressions...
> >  --
> >  Sean A Corfield -- (904) 302-SEAN
> >  Railo Technologies, Inc. --http://getrailo.com/
> >  An Architect's View --http://corfield.org/
>
> >  "If you're not annoying somebody, you're not really alive."
> >  -- Margaret Atwood
>
> >  --
> >  You received this message because you are subscribed to the Google
> >  Groups "Clojure" group.
> >  To post to this group, send email to clojure@googlegroups.com
> >  Note that posts from new members are moderated - please be patient with
> >  your first post.
> >  To unsubscribe from this group, send email to
> >  clojure+unsubscr...@googlegroups.com
> >  For more options, visit this group at
> > http://groups.google.com/group/clojure?hl=en
>
> > >>> --
> > >>> You received this message because you are subscribed to the Google
> > >>> Groups "Clojure" group.
> > >>> To post to this group, send email to clojure@googlegroups.com
> > >>> Note that posts from new members are moderated - please be patient with
> > >>> your first post.
> > >>> To unsubscribe from this group, send email to
> > >>> clojure+unsubscr...@googl

Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Paul Hobbs
Huh.  Isn't a unicode composition symbol harder to type than comp?  If it's
for readability, I'd rather go with the emacs hacks.

On Tue, Nov 16, 2010 at 11:54 AM, Alan  wrote:

> Found it. See
> https://github.com/hiredman/odds-and-ends/blob/8a84e6ddbad9d71f714ba16c3e1239633228a7eb/functional.clj#L94
>
> On Nov 16, 10:06 am, Alan  wrote:
> > Ask on #clojure about this. Someone (hiredman, I think?) has a macro
> > that rewrites code using some funky unicode characters. I can't find
> > it at the moment, but it might be what you're looking for.
> >
> > On Nov 16, 9:51 am, "Eric Schulte"  wrote:
> >
> > > Just to follow up, I'm now using the following to pretty up Clojure
> code
> > > in Emacs
> >
> > > #+begin_src emacs-lisp
> > >   ;; symbols for some overlong function names
> > >   (eval-after-load 'clojure-mode
> > > '(font-lock-add-keywords
> > >   'clojure-mode
> > >   (mapcar
> > >(lambda (pair)
> > >  `(,(car pair)
> > >(0 (progn (compose-region
> > >   (match-beginning 0) (match-end 0)
> > >   ,(cadr pair))
> > >  nil
> > >`(("\\" ,(make-char 'greek-iso8859-7 107))
> > >  ("\\" ?∘)
> > >  ("\\" ?þ)
> > >  ("\\" ?¬)
> > > #+end_src
> >
> > > I think the results look quite nice, a small example is attached
> >
> > >  abbrev-function-names.png
> > > 5KViewDownload
> >
> > > Best -- Eric
> >
> > > "Eric Schulte"  writes:
> > > > Hi Paul,
> >
> > > > Thanks for sharing this.  It seems like the best compromise between
> the
> > > > desire to keep my code brief (at least to my eyes) without wanting to
> > > > introduce my own custom function names for global functions.
> >
> > > > If you don't mind I'd like to add this to my fork of the Emacs
> Starter
> > > > Kit (will credit you as author).
> >
> > > > Best -- Eric
> >
> > > > Paul Hobbs  writes:
> >
> > > >> Well, for those who use emacs, you could always make it *look* like
> it was
> > > >> pretty... For example:
> >
> > > >> (eval-after-load 'clojure-mode
> > > >>   '(font-lock-add-keywords
> > > >> 'clojure-mode `(("\\"
> > > >>  (0 (progn (compose-region
> > > >> (match-beginning 0) (match-end 0)
> > > >> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
> > > >>nil)))
> > > >> ("\\"
> > > >>  (0
> > > >>   (progn (compose-region
> > > >>   (match-beginning 0) (match-end 0)
> > > >>   "∘ ")
> > > >>  nil)))
> > > >> ("\\"
> > > >>  (0
> > > >>   (progn (compose-region
> > > >>   (match-beginning 0) (match-end 0)
> > > >>   "𝒫 ")))
> >
> > > >> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon <
> cyrushar...@gmail.com> wrote:
> >
> > > >>> I think the minimal character count for composition and partial
> functions
> > > >>> in haskell are some of the reasons that haskell code is so
> impenetrable to
> > > >>> non-haskell hackers. Feel free to rig up crazy unicode characters
> to any
> > > >>> identifier you want in your own code, just don't ask me to read or
> debug any
> > > >>> of it.
> >
> > > >>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
> >
> > > >>> Coming from Haskell, where composition and partial functions are
> cheap and
> > > >>> free in terms of character count, it is actually pretty
> discouraging to have
> > > >>> to spell it out in Clojure for the same effect.  Some of the cases
> where you
> > > >>> "should" be using multiple expressions in Clojure would be
> perfectly clear
> > > >>> in Haskell as one expression...
> >
> > > >>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield <
> seancorfi...@gmail.com>wrote:
> >
> > >  On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
> > >  > The one that bugs me is complement - such a long name for a
> commonly-
> > >  > useful function. I often wind up defining ! as an alias for
> > >  > complement, but maybe others will think that is poor style.
> >
> > >  Possibly because bang functions indicate "Here be dragons" in
> terms of
> > >  mutating state? e.g., set!
> >
> > >  Are you really using complement a lot? I guess I would define an
> alias
> > >  for the complement-ed function or use not in expressions...
> > >  --
> > >  Sean A Corfield -- (904) 302-SEAN
> > >  Railo Technologies, Inc. --http://getrailo.com/
> > >  An Architect's View --http://corfield.org/
> >
> > >  "If you're not annoying somebody, you're not really alive."
> > >  -- Margaret Atwood
> >
> > >  --
> > >  You received this message because you are subscribed to the Google
> > >  Groups "Clojure" group.
> > >  To post to this group, send email to clojure@googlegroups.com
> > >  Note that posts from new members are moderated - please be patient
> with
> > >  your first post.
> > >  To unsubscribe from this group, send email to
> > >  clojure+unsubscr...@googlegroups.com
> 
> >
> > >  For more options, visit this group at
> > > http://

Re: shorter alternatives for `comp' and `partial'

2010-11-16 Thread Alan
Well, it would be harder for *me* to type. I imagine Hiredman has
these symbols bound to some short but little-used key sequences. If
you're not using your C-c  bindings for anything else, you can
pick four of them and use them for this.

On Nov 16, 12:13 pm, Paul Hobbs  wrote:
> Huh.  Isn't a unicode composition symbol harder to type than comp?  If it's
> for readability, I'd rather go with the emacs hacks.
>
> On Tue, Nov 16, 2010 at 11:54 AM, Alan  wrote:
> > Found it. See
> >https://github.com/hiredman/odds-and-ends/blob/8a84e6ddbad9d71f714ba1...
>
> > On Nov 16, 10:06 am, Alan  wrote:
> > > Ask on #clojure about this. Someone (hiredman, I think?) has a macro
> > > that rewrites code using some funky unicode characters. I can't find
> > > it at the moment, but it might be what you're looking for.
>
> > > On Nov 16, 9:51 am, "Eric Schulte"  wrote:
>
> > > > Just to follow up, I'm now using the following to pretty up Clojure
> > code
> > > > in Emacs
>
> > > > #+begin_src emacs-lisp
> > > >   ;; symbols for some overlong function names
> > > >   (eval-after-load 'clojure-mode
> > > >     '(font-lock-add-keywords
> > > >       'clojure-mode
> > > >       (mapcar
> > > >        (lambda (pair)
> > > >          `(,(car pair)
> > > >            (0 (progn (compose-region
> > > >                       (match-beginning 0) (match-end 0)
> > > >                       ,(cadr pair))
> > > >                      nil
> > > >        `(("\\" ,(make-char 'greek-iso8859-7 107))
> > > >          ("\\" ?∘)
> > > >          ("\\" ?þ)
> > > >          ("\\" ?¬)
> > > > #+end_src
>
> > > > I think the results look quite nice, a small example is attached
>
> > > >  abbrev-function-names.png
> > > > 5KViewDownload
>
> > > > Best -- Eric
>
> > > > "Eric Schulte"  writes:
> > > > > Hi Paul,
>
> > > > > Thanks for sharing this.  It seems like the best compromise between
> > the
> > > > > desire to keep my code brief (at least to my eyes) without wanting to
> > > > > introduce my own custom function names for global functions.
>
> > > > > If you don't mind I'd like to add this to my fork of the Emacs
> > Starter
> > > > > Kit (will credit you as author).
>
> > > > > Best -- Eric
>
> > > > > Paul Hobbs  writes:
>
> > > > >> Well, for those who use emacs, you could always make it *look* like
> > it was
> > > > >> pretty... For example:
>
> > > > >> (eval-after-load 'clojure-mode
> > > > >>   '(font-lock-add-keywords
> > > > >>     'clojure-mode `(("\\"
> > > > >>      (0 (progn (compose-region
> > > > >> (match-beginning 0) (match-end 0)
> > > > >> ,(make-char 'greek-iso8859-7 107)) ;; a lambda
> > > > >>        nil)))
> > > > >>     ("\\"
> > > > >>      (0
> > > > >>       (progn (compose-region
> > > > >>       (match-beginning 0) (match-end 0)
> > > > >>       "∘ ")
> > > > >>      nil)))
> > > > >>     ("\\"
> > > > >>      (0
> > > > >>       (progn (compose-region
> > > > >>       (match-beginning 0) (match-end 0)
> > > > >>       "𝒫 ")))
>
> > > > >> On Mon, Nov 15, 2010 at 5:19 PM, Cyrus Harmon <
> > cyrushar...@gmail.com> wrote:
>
> > > > >>> I think the minimal character count for composition and partial
> > functions
> > > > >>> in haskell are some of the reasons that haskell code is so
> > impenetrable to
> > > > >>> non-haskell hackers. Feel free to rig up crazy unicode characters
> > to any
> > > > >>> identifier you want in your own code, just don't ask me to read or
> > debug any
> > > > >>> of it.
>
> > > > >>> On Nov 15, 2010, at 2:12 PM, Paul Hobbs wrote:
>
> > > > >>> Coming from Haskell, where composition and partial functions are
> > cheap and
> > > > >>> free in terms of character count, it is actually pretty
> > discouraging to have
> > > > >>> to spell it out in Clojure for the same effect.  Some of the cases
> > where you
> > > > >>> "should" be using multiple expressions in Clojure would be
> > perfectly clear
> > > > >>> in Haskell as one expression...
>
> > > > >>> On Mon, Nov 15, 2010 at 11:37 AM, Sean Corfield <
> > seancorfi...@gmail.com>wrote:
>
> > > >  On Mon, Nov 15, 2010 at 10:26 AM, Alan  wrote:
> > > >  > The one that bugs me is complement - such a long name for a
> > commonly-
> > > >  > useful function. I often wind up defining ! as an alias for
> > > >  > complement, but maybe others will think that is poor style.
>
> > > >  Possibly because bang functions indicate "Here be dragons" in
> > terms of
> > > >  mutating state? e.g., set!
>
> > > >  Are you really using complement a lot? I guess I would define an
> > alias
> > > >  for the complement-ed function or use not in expressions...
> > > >  --
> > > >  Sean A Corfield -- (904) 302-SEAN
> > > >  Railo Technologies, Inc. --http://getrailo.com/
> > > >  An Architect's View --http://corfield.org/
>
> > > >  "If you're not annoying somebody, you're not really alive."
> > > >  -- Margaret Atwood
>
> > > >  --
> > > >  You received this message because you are subscribed

Re: Being "not Lisp" is a feature?

2010-11-16 Thread Daniel Gagnon
I spoke to the guys on reddit. They said it is tongue-in-cheek. They have no
knowledge of functional programming but strongly feel it isn't suited to
their field.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Being "not Lisp" is a feature?

2010-11-16 Thread lprefontaine
Daniel Gagnon  wrote ..
> I spoke to the guys on reddit. They said it is tongue-in-cheek.

> They have no knowledge of functional programming

???

> but strongly feel it isn't suited to their field.

???

Isn't this a bit contradictory ? It's ok to follow your own gut feeling but
it needs to be supported somehow by tangible facts. 

At least we laughed about it.

Luc P.

> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first
> post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Luc P.


The rabid Muppet

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread Alyssa Kwan
I ran into this while working on making functions durable.  Here's a
contrived example:

=> (defn a
 ([x] x)
 ([x y] (+ (a x) (a y
#'user/a

=> (a 1 2)
3

=> (def b a)
#'user/b

=> (b 1 2)
3

=> (defn a [x]
 (- x))
#'user/a

=> (b 1 2)
-3

Is this what people expect?  I would think that the original
definition of a, which is self-referencing, should point to itself no
matter what it's named, not get resolved at invoke-time to see what
the var is currently resolving to.

Thanks,
Alyssa Kwan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread Lee Spector


On Nov 16, 2010, at 5:26 PM, Alyssa Kwan wrote:
> 
> Is this what people expect?  I would think that the original
> definition of a, which is self-referencing, should point to itself no
> matter what it's named, not get resolved at invoke-time to see what
> the var is currently resolving to.


I'm still new around here but that is indeed what I would expect and want.

 -Lee


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread David Nolen
But that would destroy one of the most useful features Lisp has to offer,
interactive coding.

Live coding would be impossible w/o this behavior as you would need to find
and update all callers. Yuk.

David

On Tue, Nov 16, 2010 at 5:26 PM, Alyssa Kwan wrote:

> I ran into this while working on making functions durable.  Here's a
> contrived example:
>
> => (defn a
> ([x] x)
> ([x y] (+ (a x) (a y
> #'user/a
>
> => (a 1 2)
> 3
>
> => (def b a)
> #'user/b
>
> => (b 1 2)
> 3
>
> => (defn a [x]
> (- x))
> #'user/a
>
> => (b 1 2)
> -3
>
> Is this what people expect?  I would think that the original
> definition of a, which is self-referencing, should point to itself no
> matter what it's named, not get resolved at invoke-time to see what
> the var is currently resolving to.
>
> Thanks,
> Alyssa Kwan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Passing arguments from clojure to java

2010-11-16 Thread unst...@gmail.com
This seems like such an obvious question, but I can't seem to find the
answer anywhere. I don't understand why this would not be included in
the java_interop section of the clojure documentation.

Is it possible to pass a clojure vector to a java function that
requires a java vector as an argument? Apparently not since:

(javax.swing.table.DefaultTableModel. ["I" "B"] 0) fails.

What is the idiomatic way to do this?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread David Sletten

On Nov 16, 2010, at 5:26 PM, Alyssa Kwan wrote:

> I ran into this while working on making functions durable.  Here's a
> contrived example:
> 
> => (defn a
> ([x] x)
> ([x y] (+ (a x) (a y
> #'user/a
> 
> => (a 1 2)
> 3
> 
> => (def b a)
> #'user/b
> 
> => (b 1 2)
> 3
> 
> => (defn a [x]
> (- x))
> #'user/a
> 
> => (b 1 2)
> -3
> 

I'm confused here. Are you saying that this is what _you_ expect to see? 
Because that's not how Clojure actually behaves. 

You assign the function 'a' to 'b', and 'b' continues to refer to the original 
function after you redefine 'a'. So your second call to (b 1 2) still returns 3.

That's interesting since the function assigned to 'b' (the original version of 
'a') retains the self-reference after 'a' is redefined. The call to 'a' in 'b' 
still refers to the original 'a'. In other words, Clojure is not looking up the 
current value of 'a' when 'b' is invoked.

But that is what happens with non-self-referencing functions (There must be a 
clearer way to say that...a function which references a function other than 
itself):
(defn d [x y] (* x y))
(defn c [x] (d x 2))
(def f d)
(defn e [x] (f x 2))
user=> (c 5)
10
user=> (e 5)
10

(defn d [x y] (< x y))
user=> (c 5)
false
user=> (e 5)
10

Here the value of (c 5) changes depending on how 'd' is defined, whereas 'f' 
continues to refer to the original definition.

I suppose the answer comes from expanding 'defn':
(macroexpand '(defn a
  ([x] x)
  ([x y] (+ (a x) (a y )
=> (def a (.withMeta (clojure.core/fn a ([x] x) ([x y] (+ (a x) (a y (.meta 
(var a

(macroexpand '(defn c [x] (d x 2)))
=> (def c (.withMeta (clojure.core/fn c ([x] (d x 2))) (.meta (var c

And the documentation (http://clojure.org/special_forms#fn) says:
If a name symbol is provided, it is bound within the function definition to the 
function object itself, allowing for self-calling, even in anonymous functions.

So I guess technically the self-referential 'a' in the function definition 
actually refers to the name inside the 'fn' form not the variable that is 
getting def'd?
> 

Have all good days,
David Sletten




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Passing arguments from clojure to java

2010-11-16 Thread Alan
This is because java vectors are disgusting and not part of the
Collections framework. Clojure vectors do implement Collection, and
indeed List, but that is different from Vector. However, Vector was
hacked up a bit after Collections were released to make it a little
more "forward compatible":

(javax.swing.table.DefaultTableModel. (java.util.Vector. ["I" "B"]) 0)
;==>#


On Nov 16, 2:49 pm, "unst...@gmail.com"  wrote:
> This seems like such an obvious question, but I can't seem to find the
> answer anywhere. I don't understand why this would not be included in
> the java_interop section of the clojure documentation.
>
> Is it possible to pass a clojure vector to a java function that
> requires a java vector as an argument? Apparently not since:
>
> (javax.swing.table.DefaultTableModel. ["I" "B"] 0) fails.
>
> What is the idiomatic way to do this?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Passing arguments from clojure to java

2010-11-16 Thread David Sletten

On Nov 16, 2010, at 5:49 PM, unst...@gmail.com wrote:

> This seems like such an obvious question, but I can't seem to find the
> answer anywhere. I don't understand why this would not be included in
> the java_interop section of the clojure documentation.
> 
> Is it possible to pass a clojure vector to a java function that
> requires a java vector as an argument? Apparently not since:
> 
> (javax.swing.table.DefaultTableModel. ["I" "B"] 0) fails.
> 
> What is the idiomatic way to do this?
> 

It looks as though your constructor takes either a java.util.Vector or an array 
of Object[].

To make a String[] array out of a Clojure vector 
(clojure.lang.PersistentVector):
(into-array String ["I" "B"])

So you wind up with this:
(javax.swing.table.DefaultTableModel. (into-array String ["I" "B"]) 0)
#

Have all good days,
David Sletten




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


fastest way to remove nils

2010-11-16 Thread Glen Rubin
What is the fastest way to remove nils from a sequence?

I usually use the filter function, but I see there are other functions
like remove that should also do the trick.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: fastest way to remove nils

2010-11-16 Thread lprefontaine
user=> (time (filter identity [ nil 1 2 nil 4]))
"Elapsed time: 0.053219 msecs"
(1 2 4)

user=> (time (remove nil? [ nil 1 2 nil 4]))
"Elapsed time: 0.065092 msecs"
(1 2 4)

Choose the flavor your prefer...

Luc P.

Glen Rubin  wrote ..
> What is the fastest way to remove nils from a sequence?
> 
> I usually use the filter function, but I see there are other functions
> like remove that should also do the trick.
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first
> post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Luc P.


The rabid Muppet

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: fastest way to remove nils

2010-11-16 Thread Miki
user=> (time (remove nil? (repeat 100 nil)))
"Elapsed time: 0.079312 msecs"
()
user=> (time (filter identity (repeat 100 nil)))
"Elapsed time: 0.070249 msecs"
()

Seems like filter is a bit faster, however YMMV

On Nov 16, 4:48 pm, Glen Rubin  wrote:
> What is the fastest way to remove nils from a sequence?
>
> I usually use the filter function, but I see there are other functions
> like remove that should also do the trick.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: fastest way to remove nils

2010-11-16 Thread Stuart Campbell
On 17 November 2010 12:37,  wrote:

> user=> (time (filter identity [ nil 1 2 nil 4]))
> "Elapsed time: 0.053219 msecs"
> (1 2 4)
>
> user=> (time (remove nil? [ nil 1 2 nil 4]))
> "Elapsed time: 0.065092 msecs"
> (1 2 4)
>
> Choose the flavor your prefer...
>
> Luc P.
>

Those aren't exactly equivalent:

user> (filter identity [1 2 false 3 nil 4])
(1 2 3 4)
user> (remove nil? [1 2 false 3 nil 4])
(1 2 false 3 4)

Regards,
Stuart

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: fastest way to remove nils

2010-11-16 Thread Mark Engelberg
(keep identity l) is preferable to (filter identity l)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: bimaps in clojure

2010-11-16 Thread Sunil S Nandihalli
Thanks Christopher Grand. I really like your scaffold .. would be extremely
handy .. Like david suggested .. it would be a perfect candidate for
inclusion in the clojure.repl

Regarding your bimap implementation, in terms of complexity, I feel, it will
be linear in the number of elements, when accessing the pair via the value
.. Is that true?

Sunil

On Tue, Nov 16, 2010 at 2:36 PM, Christophe Grand wrote:

> You can implement your own, prettu easily with deftype.
> However it can be tedious to track every methods, so we need a repl helper
> function to scaffold the code for us.
>
> (defn scaffold [iface]
>   (doseq [[iface methods] (->> iface .getMethods
> (map #(vector (.getName (.getDeclaringClass %))
>
> (symbol (.getName %))
> (count (.getParameterTypes %
> (group-by first))]
> (println (str "  " iface))
> (doseq [[_ name argcount] methods]
>   (println
> (str ""
>   (list name (into ['this] (take argcount (repeatedly
> gensym)
>
> user=> (scaffold clojure.lang.IPersistentMap)
>   clojure.lang.IPersistentMap
> (assoc [this G__441 G__442])
> (without [this G__443])
> (assocEx [this G__444 G__445])
>   java.lang.Iterable
> (iterator [this])
>   clojure.lang.Associative
> (containsKey [this G__446])
> (assoc [this G__447 G__448])
> (entryAt [this G__449])
>   clojure.lang.IPersistentCollection
> (count [this])
> (cons [this G__450])
> (empty [this])
> (equiv [this G__451])
>   clojure.lang.Seqable
> (seq [this])
>   clojure.lang.ILookup
> (valAt [this G__452 G__453])
> (valAt [this G__454])
>   clojure.lang.Counted
> (count [this])
> nil
>
> Now you need to remove the duplicates (count and assoc), wrap everything in
> a deftype form and implement it:
> (including a new protocol fn to reverse a bidi map)
>
> (defprotocol ReversibleMap
>   (rmap [m]))
>
> (defn- rdissoc [d r v]
>  (if-let [[_ k] (find r v)] (dissoc d k) d))
>
> (deftype Bimap [^clojure.lang.IPersistentMap direct reverse]
>   Object
>   (hashCode [x]
> (.hashCode direct))
>   (equals [x y]
> (.equals direct y))
>   clojure.lang.IPersistentMap
> (without [this k]
>   (Bimap.
> (dissoc direct k)
> (rdissoc reverse direct k)))
> (assocEx [this k v]
>   (if (or (contains? direct k) (contains? reverse v))
> (throw (Exception. "Key or value already present"))
> (assoc this k v)))
>   java.lang.Iterable
> (iterator [this]
>   (.iterator direct))
>   clojure.lang.Associative
> (assoc [this k v]
>   (Bimap.
> (assoc (rdissoc direct reverse v) k v)
> (assoc (rdissoc reverse direct k) v k)))
> (containsKey [this k]
>   (contains? direct k))
> (entryAt [this k]
>   (find direct k))
>   clojure.lang.IPersistentCollection
> (cons [this x]
>   (if-let [[k v] (and (vector? x) x)]
> (assoc this k v)
> (reduce (fn [m [k v]] (assoc m k v)) this x)))
> (empty [this]
>   (.empty direct))
> (equiv [this x]
>   (.equiv direct x))
>   clojure.lang.Seqable
> (seq [this]
>   (seq direct))
>   clojure.lang.ILookup
> (valAt [this k else]
>   (direct k else))
> (valAt [this k]
>   (direct k))
>   clojure.lang.Counted
> (count [this]
>   (count direct))
>   ReversibleMap
> (rmap [this]
>   (Bimap. reverse direct)))
>
> (defn bimap [& kvs]
>   (reduce (fn [m [k v]] (assoc m k v)) (Bimap. {} {}) (partition 2 kvs)))
>
> Some quick tests:
> user=> (assoc (bimap :a 1 :b 2) [:c 3] [:d 4] [:e 5])
> {[:e 5] nil, [:c 3] [:d 4], :b 2, :a 1}
> user=> (assoc (bimap :a 1 :b 2) :c 3 :d 4 :e 5)
> {:e 5, :d 4, :c 3, :b 2, :a 1}
> user=> (assoc (bimap :a 1 :b 2) :c 3 :d 2 :e 5)
> {:e 5, :d 2, :c 3, :a 1}
> user=> (dissoc *1 :d)
> {:e 5, :c 3, :a 1}
> user=> (rmap *1)
> {5 :e, 3 :c, 1 :a}
> user=> (assoc *1 2 :c)
> {2 :c, 5 :e, 1 :a}
>
> hth,
>
> Christophe
>
>
>  On Tue, Nov 16, 2010 at 8:16 AM, Sunil S Nandihalli <
> sunil.nandiha...@gmail.com> wrote:
>
>>  A bimap is a map where each elements of the pair can be used as key to
>> access it..
>> Sunil.
>>
>>
>> On Tue, Nov 16, 2010 at 12:44 PM, Sunil S Nandihalli <
>> sunil.nandiha...@gmail.com> wrote:
>>
>>> Hello everybody,
>>>
>>> Is there something like a bimap in clojure? I know I can have two regular
>>> hash-maps .. but I was wondering if there is a better implementation..?
>>>
>>>  a similar implementation in c++ is
>>>
>>> http://beta.boost.org/doc/libs/1_41_0/libs/bimap/doc/html/index.html
>>>
>>> Thanks,
>>> Sunil.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, se

Re: fastest way to remove nils

2010-11-16 Thread lprefontaine
Oups.. :)

Stuart Campbell  wrote ..
> On 17 November 2010 12:37,  wrote:
> 
> > user=> (time (filter identity [ nil 1 2 nil 4]))
> > "Elapsed time: 0.053219 msecs"
> > (1 2 4)
> >
> > user=> (time (remove nil? [ nil 1 2 nil 4]))
> > "Elapsed time: 0.065092 msecs"
> > (1 2 4)
> >
> > Choose the flavor your prefer...
> >
> > Luc P.
> >
> 
> Those aren't exactly equivalent:
> 
> user> (filter identity [1 2 false 3 nil 4])
> (1 2 3 4)
> user> (remove nil? [1 2 false 3 nil 4])
> (1 2 false 3 4)
> 
> Regards,
> Stuart
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first
> post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Luc P.


The rabid Muppet

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: fastest way to remove nils

2010-11-16 Thread lprefontaine
Re-oups, Clojure 1.0 to 1.2 upgrade glitch :)

Mark Engelberg  wrote ..
> (keep identity l) is preferable to (filter identity l)
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first
> post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Luc P.


The rabid Muppet

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: bimaps in clojure

2010-11-16 Thread Mark Engelberg
On Tue, Nov 16, 2010 at 7:18 PM, Sunil S Nandihalli <
sunil.nandiha...@gmail.com> wrote:

> Thanks Christopher Grand. I really like your scaffold .. would be extremely
> handy .. Like david suggested .. it would be a perfect candidate for
> inclusion in the clojure.repl
>
> Regarding your bimap implementation, in terms of complexity, I feel, it
> will be linear in the number of elements, when accessing the pair via the
> value .. Is that true?
>
> Sunil
>
>
You could use the same technique he described to implement a bimap that's
stored internally as two hash maps (keys->vals, vals->keys).

Alternatively, you could use some sort of simple relational database and
build an index on both columns to support queries from either keys or
values.  In addition to the many off-the-shelf databases that you can access
from Clojure (SQLite, MySQL, MongoDB, etc.), you could take a look at
clojure.contrib.datalog and FleetDB which provide in-memory relational
databases.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread Alyssa Kwan
Notice that when I redefined a, I only included one arity.  If b were
updated with the fn that a was redefined to, then (b 1 2) should have
thrown an exception.  Instead, it used the old definition of a but
within that definition pointed to the new definition of a.  This is
internally inconsistent.

I'm not proposing making all function definitions lexically bound.
Yes, that would destroy interactive coding.

But to be internally consistent, self-references should be lexical.

In any case, I am using Github master and I thought I was using 1.2.
1.2 has self-references lexically bound, as David Sletten points out,
which I agree is the correct behavior.  But something has happened on
1.3 alpha that has changed that.  I don't know if it's intentional or
not.

Thanks,
Alyssa Kwan

On Nov 16, 6:01 pm, David Nolen  wrote:
> But that would destroy one of the most useful features Lisp has to offer,
> interactive coding.
>
> Live coding would be impossible w/o this behavior as you would need to find
> and update all callers. Yuk.
>
> David
>
> On Tue, Nov 16, 2010 at 5:26 PM, Alyssa Kwan wrote:
>
>
>
> > I ran into this while working on making functions durable.  Here's a
> > contrived example:
>
> > => (defn a
> >     ([x] x)
> >     ([x y] (+ (a x) (a y
> > #'user/a
>
> > => (a 1 2)
> > 3
>
> > => (def b a)
> > #'user/b
>
> > => (b 1 2)
> > 3
>
> > => (defn a [x]
> >     (- x))
> > #'user/a
>
> > => (b 1 2)
> > -3
>
> > Is this what people expect?  I would think that the original
> > definition of a, which is self-referencing, should point to itself no
> > matter what it's named, not get resolved at invoke-time to see what
> > the var is currently resolving to.
>
> > Thanks,
> > Alyssa Kwan
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com > >
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: bimaps in clojure

2010-11-16 Thread Ken Wesson
On Tue, Nov 16, 2010 at 10:18 PM, Sunil S Nandihalli
 wrote:
> Thanks Christopher Grand. I really like your scaffold .. would be extremely
> handy .. Like david suggested .. it would be a perfect candidate for
> inclusion in the clojure.repl
> Regarding your bimap implementation, in terms of complexity, I feel, it will
> be linear in the number of elements, when accessing the pair via the value
> .. Is that true?

Doesn't look like it. The implementation posted seems to use two Clojure maps.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Ghost Vars?

2010-11-16 Thread Alyssa Kwan
ns-unmap isn't typically used.  But for durability, we can think of
JVM shutdown/startup as unmapping everything and starting fresh.
Therefore, expected behavior for ns-unmap should be the same as
behavior for deserializing and loading an object after a new JVM
startup.  Here's another issue I ran into:

=> (def a 1)
#'user/a

=> (defn b [] a)
#'user/b

=> (b)
1

=> (def a 2)
#'user/a

=> (b)
2

=> (binding [a 3]
 (b))
3

So far so good...

=> (ns-unmap *ns* 'a)
nil

=> a
java.lang.Exception: Unable to resolve symbol: a in this context

=> (b)
2

=> (binding [a 3]
 (b))
java.lang.Exception: Unable to resolve var: a in this context

So what's the expected behavior here?  I would think that after a is
unmapped, b should no longer work.  Instead, it's hanging onto a with
the last value.  And the binding form doesn't work anymore, so there's
no way to dynamically bind over it.  It's like a weird hybrid of
lexical and dynamic binding...

Thanks,
Alyssa Kwan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread David Nolen
On Tue, Nov 16, 2010 at 10:52 PM, Alyssa Kwan wrote:

>
> In any case, I am using Github master and I thought I was using 1.2.
> 1.2 has self-references lexically bound, as David Sletten points out,
> which I agree is the correct behavior.  But something has happened on
> 1.3 alpha that has changed that.  I don't know if it's intentional or
> not.
>
> Thanks,
> Alyssa Kwan


Binding changed in 1.3.0. I see your point, I'm not sure if the behavior in
1.3.0 is considered a bug.

David

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Dynamic Binding of Self-Referencing Functions Expected Behavior?

2010-11-16 Thread David Sletten
Ok, now I get it. The results you included weren't a hypothetical example, you 
were simply using a different version of Clojure. So in your 1.3 version, the 
second (b 1 2) does return -3?

Are the macro expansions of 'defn' different?


Have all good days,
David Sletten

On Nov 16, 2010, at 10:52 PM, Alyssa Kwan wrote:

> Notice that when I redefined a, I only included one arity.  If b were
> updated with the fn that a was redefined to, then (b 1 2) should have
> thrown an exception.  Instead, it used the old definition of a but
> within that definition pointed to the new definition of a.  This is
> internally inconsistent.
> 
> I'm not proposing making all function definitions lexically bound.
> Yes, that would destroy interactive coding.
> 
> But to be internally consistent, self-references should be lexical.
> 
> In any case, I am using Github master and I thought I was using 1.2.
> 1.2 has self-references lexically bound, as David Sletten points out,
> which I agree is the correct behavior.  But something has happened on
> 1.3 alpha that has changed that.  I don't know if it's intentional or
> not.
> 
> Thanks,
> Alyssa Kwan
> 
> On Nov 16, 6:01 pm, David Nolen  wrote:
>> But that would destroy one of the most useful features Lisp has to offer,
>> interactive coding.
>> 
>> Live coding would be impossible w/o this behavior as you would need to find
>> and update all callers. Yuk.
>> 
>> David
>> 
>> On Tue, Nov 16, 2010 at 5:26 PM, Alyssa Kwan wrote:
>> 
>> 
>> 
>>> I ran into this while working on making functions durable.  Here's a
>>> contrived example:
>> 
>>> => (defn a
>>> ([x] x)
>>> ([x y] (+ (a x) (a y
>>> #'user/a
>> 
>>> => (a 1 2)
>>> 3
>> 
>>> => (def b a)
>>> #'user/b
>> 
>>> => (b 1 2)
>>> 3
>> 
>>> => (defn a [x]
>>> (- x))
>>> #'user/a
>> 
>>> => (b 1 2)
>>> -3
>> 
>>> Is this what people expect?  I would think that the original
>>> definition of a, which is self-referencing, should point to itself no
>>> matter what it's named, not get resolved at invoke-time to see what
>>> the var is currently resolving to.
>> 
>>> Thanks,
>>> Alyssa Kwan
>> 
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com>> >
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Add method implementations to proxy

2010-11-16 Thread Mark Rathwell
So I get an instance of AbstractComponent (
http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/component/AbstractComponent.html)
with the function below, overriding the necessary methods:

(defn get-component
  "returns a new component instance with the specified values
name:  the name of the component, e.g. the subdomain name
domain:  the domain of the xmpp server
description:  a description of the component
message-handler:  a handler function for incoming messages
   (takes one param - the message instance)"
  [name domain description message-handler]
  (proxy [AbstractComponent] []
(handleMessage [message] (message-handler message))
(getName [] (str name))
(getDomain [] (str domain))
(getDescription [] (str description

Now, say I later want to override additional methods on that instance, as
in:

(let [comp (get-component "foo" "domain.com" "foo comp" message-handler)]
  (-> comp (add-presence-handler presence-handler) (add-iq-handler
iq-handler)))

First, is this possible?  And if so, what does add-presence-handler look
like?

(defn add-presence-handler
  "add the specified function as the presence packet handler for the
specified component"
  [component presence-handler]
  (doto component something))

Or would gen-class work here?

Thanks.

 - Mark

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Add method implementations to proxy

2010-11-16 Thread Liam
Would this help?

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/update-proxy

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Add method implementations to proxy

2010-11-16 Thread Liam
More information from Rich himself about "update-proxy" when he first
introduced it. Could not find examples for you except from the Joy of
Clojure book on page 273, but it is very trivial... just like the doc
string sounds.

http://groups.google.com/group/clojure/browse_thread/thread/ed1652132dfa623c/a78eac7875bd2866?#a78eac7875bd2866

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: bimaps in clojure

2010-11-16 Thread Christophe Grand
On Wednesday, November 17, 2010, Sunil S Nandihalli
 wrote:
> Regarding your bimap implementation, in terms of complexity, I feel, it will 
> be linear in the number of elements, when accessing the pair via the value .. 
> Is that true?

No I use two maps and rmap is O(1) so rmap+get is O(log32 n) like a
direct get. What may confuse you is that only the direct map is
hinted. The reverse map doesn't need to: no interop calls on it while
it is in reverse position.

> On Tue, Nov 16, 2010 at 2:36 PM, Christophe Grand  
> wrote:
>
>
> You can implement your own, prettu easily with deftype.
> However it can be tedious to track every methods, so we need a repl helper 
> function to scaffold the code for us.
>
> (defn scaffold [iface]
>   (doseq [[iface methods] (->> iface .getMethods
>     (map #(vector (.getName (.getDeclaringClass %))
>     (symbol (.getName %))
>     (count (.getParameterTypes %
>     (group-by first))]
>     (println (str "  " iface))
>     (doseq [[_ name argcount] methods]
>   (println
>     (str "    "
>   (list name (into ['this] (take argcount (repeatedly gensym)
>
> user=> (scaffold clojure.lang.IPersistentMap)
>   clojure.lang.IPersistentMap
>     (assoc [this G__441 G__442])
>     (without [this G__443])
>     (assocEx [this G__444 G__445])
>   java.lang.Iterable
>     (iterator [this])
>   clojure.lang.Associative
>     (containsKey [this G__446])
>     (assoc [this G__447 G__448])
>     (entryAt [this G__449])
>   clojure.lang.IPersistentCollection
>     (count [this])
>     (cons [this G__450])
>     (empty [this])
>     (equiv [this G__451])
>   clojure.lang.Seqable
>     (seq [this])
>   clojure.lang.ILookup
>     (valAt [this G__452 G__453])
>     (valAt [this G__454])
>   clojure.lang.Counted
>     (count [this])
> nil
>
> Now you need to remove the duplicates (count and assoc), wrap everything in a 
> deftype form and implement it:
> (including a new protocol fn to reverse a bidi map)
>
> (defprotocol ReversibleMap
>   (rmap [m]))
>
> (defn- rdissoc [d r v]
>  (if-let [[_ k] (find r v)] (dissoc d k) d))
>
> (deftype Bimap [^clojure.lang.IPersistentMap direct reverse]
>   Object
>   (hashCode [x]
>     (.hashCode direct))
>   (equals [x y]
>     (.equals direct y))
>   clojure.lang.IPersistentMap
>     (without [this k]
>   (Bimap.
>     (dissoc direct k)
>     (rdissoc reverse direct k)))
>     (assocEx [this k v]
>   (if (or (contains? direct k) (contains? reverse v))
>     (throw (Exception. "Key or value already present"))
>     (assoc this k v)))
>   java.lang.Iterable
>     (iterator [this]
>   (.iterator direct))
>   clojure.lang.Associative
>     (assoc [this k v]
>   (Bimap.
>     (assoc (rdissoc direct reverse v) k v)
>     (assoc (rdissoc reverse direct k) v k)))
>     (containsKey [this k]
>   (contains? direct k))
>     (entryAt [this k]
>   (find direct k))
>   clojure.lang.IPersistentCollection
>     (cons [this x]
>   (if-let [[k v] (and (vector? x) x)]
>     (assoc this k v)
>     (reduce (fn [m [k v]] (assoc m k v)) this x)))
>     (empty [this]
>   (.empty direct))
>     (equiv [this x]
>   (.equiv direct x))
>   clojure.lang.Seqable
>     (seq [this]
>   (seq direct))
>   clojure.lang.ILookup
>     (valAt [this k else]
>   (direct k else))
>     (valAt [this k]
>   (direct k))
>   clojure.lang.Counted
>     (count [this]
>   (count direct))
>   ReversibleMap
>     (rmap [this]
>   (Bimap. reverse direct)))
>
> (defn bimap [& kvs]
>   (reduce (fn [m [k v]] (assoc m k v)) (Bimap. {} {}) (partition 2 kvs)))
>
> Some quick tests:
> user=> (assoc (bimap :a 1 :b 2) [:c 3] [:d 4] [:e 5])
> {[:e 5] nil, [:c 3] [:d 4], :b 2, :a 1}
> user=> (assoc (bimap :a 1 :b 2) :c 3 :d 4 :e 5)
> {:e 5, :d 4, :c 3, :b 2, :a 1}
> user=> (assoc (bimap :a 1 :b 2) :c 3 :d 2 :e 5)
> {:e 5, :d 2, :c 3, :a 1}
> user=> (dissoc *1 :d)
> {:e 5, :c 3, :a 1}
> user=> (rmap *1)
> {5 :e, 3 :c, 1 :a}
> user=> (assoc *1 2 :c)
> {2 :c, 5 :e, 1 :a}
>
> hth,
>
> Christophe
>
>
>
>
>
> On Tue, Nov 16, 2010 at 8:16 AM, Sunil S Nandihalli 
>  wrote:
>
>
>
> A bimap is a map where each elements of the pair can be used as key to access 
> it..
> Sunil.
>
> On Tue, Nov 16, 2010 at 12:44 PM, Sunil S Nandihalli 
>  wrote:

-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en