Well, it can't be the best, because it has a bug. Calling it with "a"
instead of "aa" gives me a NPE:

x=> (subseq (sorted-map-by string-like-coll-comparator (vec "ab") 1
(vec "n") 2) > (vec "a"))
NullPointerException   clojure.lang.AFunction.compare (AFunction.java:
59)

The following comparator should do the trick:

(defn string-like-coll-comparator
  [coll1 coll2]
  (or (first (drop-while zero? (map compare coll1 coll2)))
      (- (count coll1) (count coll2))))

x=> (subseq (sorted-map-by string-like-coll-comparator (vec "ab") 1
(vec "n") 2) > (vec "a"))
([[\a \b] 1] [[\n] 2])

So is this then the best way to get the sorting behavior I need?

On Jul 10, 6:09 pm, Eugen Dück <eu...@dueck.org> wrote:
> Say I have this sorted map, using strings as keys:
>
> x=> (sorted-map "ab" 1 "n" 2)
> {"ab" 1, "n" 2}
>
> When I do a subseq
>
> x=> (subseq (sorted-map "ab" 1 "n" 2) > "aa")
> (["ab" 1] ["n" 2])
>
> I get back both entries.
>
> Now if I do the same subseq on the same map, except that I turn all
> strings into character collections, I only get the "ab" entry:
>
> x=> (subseq (sorted-map (vec "ab") 1 (vec "n") 2) > (vec "aa"))
> ([[\a \b] 1])
>
> I guess that's fine, the length of collections is probably used first
> and only if it differs would the actual entries be compared.
>
> Using my own comparator to simulate the string comparison I can get
> the string-like behavior:
>
> (defn string-like-coll-comparator
>     [coll1 coll2]
>     (first (drop-while zero? (map compare coll1 coll2))))
>
> x=> (subseq (sorted-map-by string-like-coll-comparator (vec "ab") 1
> (vec "n") 2) > (vec "aa"))
> ([[\a \b] 1] [[\n] 2])
>
> Here's my question - is this the best way to get what I want?

-- 
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

Reply via email to