Uploaded image for project: 'Clojure'
  1. CLJ-1449

Add clojure.string functions for portability to ClojureScript


    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Completed
    • Affects versions: Release 1.6
    • Fix versions: Release 1.8
    • Labels:
    • Approval:
    • Patch:
      Code and Test


      It would be useful if a few common functions from Java's String were available as Clojure functions for the purposes of increasing portability to other Clojure platforms like ClojureScript.

      The functions below also cover the vast majority of cases where Clojure users currently drop into Java interop for String calls - this tends to be an issue for discoverability and learning. While the goal of this ticket is increased portability, improving that is a nice secondary benefit.

      Proposed clojure.string fn java.lang.String method
      index-of indexOf
      last-index-of lastIndexOf
      starts-with? startsWith
      ends-with? endsWith
      includes? contains


      • clj-1449-7.patch - uses nil to indicate not-found in index-of

      Performance: Tested the following with criterium for execution mean time (all times in ns).

      Java Clojure Java Clojure (-4 patch) Clojure (-6 patch)
      (.indexOf "banana" "n") (clojure.string/index-of "banana" "n") 8.70 9.03 9.27
      (.indexOf "banana" "n" 1) (clojure.string/index-of "banana" "n" 1) 7.29 7.61 7.66
      (.indexOf "banana" (int \n)) (clojure.string/index-of "banana" \n) 5.34 6.20 6.20
      (.indexOf "banana" (int \n) 1) (clojure.string/index-of "banana" \n 1) 5.38 6.19 6.24
      (.startsWith "apple" "a") (clojure.string/starts-with? "apple" "a") 4.84 5.71 5.65
      (.endsWith "apple" "e") (clojure.string/ends-with? "apple" "e") 4.82 5.68 5.70
      (.contains "baked apple pie" "apple") (clojure.string/includes? "baked apple pie" "apple") 10.78 11.99 12.17

      Screened by: Stu, who prefers nil over -1 - add_functions_to_strings-6.patch

      Note: In both Java and JavaScript, indexOf will return -1 to indicate not-found, so this is (at least in these two cases) the status quo. The benefit of nil return in Clojure is that it can be used as a found/not-found condition. The benefit of a -1 return is that it matches Java/JavaScript and that the return type can be hinted as a primitive long, allowing you to feed it into some other arithmetic or string operation without boxing.




            • Assignee:
              rubygeek Nola Stowe
              bozhidar Bozhidar Batsov
            • Votes:
              15 Vote for this issue
              5 Start watching this issue


              • Created: