A few String functions we could implement in Clojure?

231 views
Skip to first unread message

Pierre Masci

unread,
Jul 19, 2014, 6:58:31 AM7/19/14
to clo...@googlegroups.com
Hi all, just nit picking about Clojure's String API.

I've been comparing it with Java's, and I noticed that (not surprisingly) they are very similar.
There are just 2-3 functions that exist in Java but don't have an equivalent in Clojure. I was wondering if they could be worth adding to clojure.string :

(.indexOf s "c")   and   (.lastIndexOf "c")

(.startsWith s "danc")   and   (.endsWith s "ing")

(.charAt s 5)
    same as (get s 5) but expresses a clearer intent. It's less general than (get) though as it only applies to Strings, so that might be unnecessary sugar.




.indexOf and .lastIndexOf are indicated in the Clojure Cheatsheet, maybe .startsWith and .endsWith also deserve to be mentioned there?

I've been wondering why some functions have been ported, like (lower-case) for (.toLowerCase), but not the ones mentioned above.

I told you it was nit picking (^c^) Clojure's API is awesome as it is.

Andy Fingerhut

unread,
Jul 19, 2014, 11:18:18 AM7/19/14
to clo...@googlegroups.com
Pierre:

I maintain the cheatsheet, and I put .indexOf and .lastIndexOf on there since they are probably the most common thing I saw asked about that is in the Java API but not the Clojure API, for strings.  There are also links to whole Java classes and their entire API, e.g. for file I/O, for which there is no Clojure equivalent, since file I/O is a common need.  Clojure is meant to be a hosted language, not hiding its host platform, but making it easily callable.

If there are entire Java classes that meet very common needs that aren't mentioned on the cheatsheet, I would consider adding links to their documentation pages.  I don't want to fill up the cheatsheet with many individual Java methods, though.

As for why there are not Clojure equivalents of particular Java API methods, I think the reasoning might be similar (it has likely been discussed publicly, but I don't have a link handy) -- don't create a large number of Clojure functions that do nothing more than what the equivalent Java APIs do.

Andy


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruce Durling

unread,
Jul 19, 2014, 11:24:23 AM7/19/14
to Clojure
Andy,

How much of this reasoning do you think changes when we starting
thinking about being hosted on multiple platforms (I'm thinking
specifically clojure/clojurescript and cljx)?

cheers,
Bruce
--
@otfrom | CTO & co-founder @MastodonC | mastodonc.com
See recent coverage of us in the Economist http://econ.st/WeTd2i and
the Financial Times http://on.ft.com/T154BA

Andy Fingerhut

unread,
Jul 19, 2014, 11:49:35 AM7/19/14
to clo...@googlegroups.com
I would have to defer that question to someone who makes decisions regarding what goes into Clojure/ClojureScript, and what does not.

Of course, anyone else is free to create libraries that try to make portability between those two platforms easier.  Perhaps someone has already taken a go at creating such a thing?  I haven't used ClojureScript myself yet, so haven't looked for anything in that area.

Andy

mascip

unread,
Jul 19, 2014, 11:56:44 AM7/19/14
to clo...@googlegroups.com
Thank you for your insight Andy :-)

Interesting question Bruce.

-- Pierre Masci


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/MzsfBo7Vkjg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Bozhidar Batsov

unread,
Aug 1, 2014, 6:16:48 AM8/1/14
to clo...@googlegroups.com
I recently raised a similar point regarding `starts-with?` and `ends-with?` (link - http://dev.clojure.org/jira/browse/CLJ-1449) and it seems that Clojure's team acknowledges that this is valid reasoning.

I think you should open a ticket as well as the case you present is pretty much the same. With ClojureScript and cljx it makes much more sense now to create
portable interfaces that it used to before (in the era of Java-only Clojure).

mascip

unread,
Aug 1, 2014, 6:30:24 AM8/1/14
to clo...@googlegroups.com
I have voted your issue up, and added a comment. Thanks for the link :-)

-- Pierre Masci

Paul Butcher

unread,
Aug 1, 2014, 6:40:10 AM8/1/14
to clo...@googlegroups.com, Andy Fingerhut
I recently hit exactly this question in a ClojureScript app I’m writing. It just so happens that Javascript provides a .indexOf method which is, as near as dammit, the same as the one provided by Java. So in this instance, portability isn’t an issue.

But having said that, I would still prefer to see this supported natively in Clojure, even if it’s just a wrapper around the interop. Using interop to do things that are as generic as simple string manipulation just feels messy.

--
paul.butcher->msgCount++

Silverstone, Brands Hatch, Donington Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
Skype: paulrabutcher

Author of Seven Concurrency Models in Seven Weeks: When Threads Unravel
Reply all
Reply to author
Forward
0 new messages