Dave
--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
I think that's a good point. I did notice in the style guidelines that we should avoid doing that specifically.
On further thought, I think inc and dec would be better names for the functions currently called prev and next. The only thing is, I'm using clojure.core/inc and dec all over the place, so I guess I would have to fully qualify those functions wherever I'm using them, which could get a little messy... Thoughts on that?
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/CVT5nqCz9XI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
Hi everyone,
I came to Clojure already being familiar with Haskell, so it was natural to compare the two. One thing that I noticed is that Clojure lacks a library of character utility functions. Haskell's Data.Char (http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Char.html) is a good example of the kind of thing I'm talking about. I also think it would be nice for Clojure to have a more intuitive way to represent ranges of characters, such as all lowercase letters, all (Latin-1) digits, etc.
As it is, it's easy enough to use Java inter-op to utilize the methods in Java's Character class, and using (map char (range (int \a) (inc (int \z)))) to represent all lowercase letters isn't TOO terribly obnoxious, but I think that having a more intuitive way of working with characters in Clojure would be an asset to the language's expressivity and friendliness to newcomers.
With that in mind, I've taken the liberty of putting together this rough draft of a possible character utility library for Clojure: https://github.com/daveyarwood/djy
I could see this being namespaced as clojure.char (on analogy with clojure.string, clojure.set, etc.), or perhaps as a contrib library if that is more appropriate. Ultimately, if Clojure/core is not interested, I could just make it available as a Clojar for whomever may find it useful.
Some key things that I've put into this library:
- API functions are polymorphic -- most of them can accept characters, integers representing Unicode code points, or strings (allowing you to work with supplementary characters that cannot be represented as Java/Clojure character literals).
- There are wrappers for Java methods (Character/isISOControl, Character/toUpperCase, etc.), as well as other useful utility functions.
- char' (on analogy with +', *', etc.) -- an extended version of the char function that will return a string containing a supplementary character if you provide the code point, e.g. (char' 135641) => 𡇙
- next and prev functions, which will return the character one code point before or after a given character/code point
- char-seq -- like seq (when used on a string), but each of the resulting seq's items can be either a character or a string containing a supplemental character. You could use this function to get an accurate character count in a body of text that contains supplementary characters.
- char-range -- returns the range of chars from %1 to %2.. This would make it significantly easier to represent things like all lowercase letters -- (char-range \a \z)
This is still a work in progress -- I'd be happy to hear any constructive feedback / criticism you guys might have, as well as any suggestions for character-related functions that could be useful for a library like this. Let me know what you think.
Thanks!
Dave
In general, I think this is a good idea for a library (contrib or no). Few specific comments below.- There are wrappers for Java methods (Character/isISOControl, Character/toUpperCase, etc.), as well as other useful utility functions.
Mere wrappers are meh. However, one reason such a thing might be useful is if they define a portable API implementable across hosts (JVM/JS/CLR).
- char' (on analogy with +', *', etc.) -- an extended version of the char function that will return a string containing a supplementary character if you provide the code point, e.g. (char' 135641) => 𡇙
+1 (not sure of char' name but idea seems good)
- next and prev functions, which will return the character one code point before or after a given character/code point+1 but wouldn't name them next/prev. succ/pred would be better (or possibly some play on inc/dec).
- char-seq -- like seq (when used on a string), but each of the resulting seq's items can be either a character or a string containing a supplemental character. You could use this function to get an accurate character count in a body of text that contains supplementary characters.It seems like some (all?) of this already exists via String/Char but I haven't used supplemental characters enough to know where the gaps are.
--
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/CVT5nqCz9XI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
Thanks for all the great feedback! I've written some comments below:
On Monday, October 13, 2014 1:13:44 PM UTC-4, Alex Miller wrote:In general, I think this is a good idea for a library (contrib or no). Few specific comments below.- next and prev functions, which will return the character one code point before or after a given character/code point+1 but wouldn't name them next/prev. succ/pred would be better (or possibly some play on inc/dec).I'm starting to lean towards char/inc and char/dec as the names for these functions, on analogy with clojure.core's inc and dec for integers.I guess the big question here is whether this library will be intended to be "used" or "required." Right now I'm taking inspiration from clojure.string, which has some functions that would conflict with clojure.core functions, and so the intended usage is to require it ":as str" or s or whatever so that the conflicting functions are all namespaced. If the intended usage for this library would be something like (require '[djy.char :as char]), then the functions could be called like (char/inc \a).I do like succ/pred, as an alternative. Or maybe next-char/prev-char.
I do like succ/pred, as an alternative. Or maybe next-char/prev-char.
next-char and prev-char are very good names for this, IMO.