Permalink
Browse files

Seqable strings, implement most of clojure.string

  • Loading branch information...
1 parent cfb36d3 commit 2b5d31e5ff394c6e548bc53358c63893681a5898 @noprompt noprompt committed Dec 5, 2012
Showing with 90 additions and 7 deletions.
  1. +85 −6 lib/boot.rg
  2. +2 −0 lib/rouge/seq.rb
  3. +3 −1 spec/core_spec.rg
View
@@ -31,7 +31,7 @@
`(Rouge.Seq.Lazy. (fn [] ~@body)))
(defn reduce [f coll]
- (.inject (.to_a coll) | f))
+ (.inject (.to_a (seq coll)) | f))
(defmacro when [cond & body]
`(if ~cond
@@ -42,7 +42,8 @@
(Rouge.Seq.Cons. head tail))
(defn count [coll]
- (.count coll))
+ (let [s (seq coll)]
+ (if s (.count s) 0)))
(defn = [a b]
(.== a b))
@@ -72,10 +73,11 @@
(defn map [f coll]
(lazy-seq
- (if (empty? coll)
- nil
- (let [[hd & tl] coll]
- (cons (f hd) (map f tl))))))
+ (let [s (seq coll)]
+ (if (empty? s)
+ nil
+ (let [[hd & tl] s]
+ (cons (f hd) (map f tl)))))))
(defn str [& args]
(let [args (.to_a (map .to_s args))]
@@ -94,6 +96,9 @@
(defn class [object]
(.class object))
+(defn class? [obj class]
+ (.is_a? obj class))
+
(defn sequential? [coll]
(or (.is_a? coll Rouge.Seq.ISeq)
(.is_a? coll Array)))
@@ -363,6 +368,80 @@
.to_a
(.sort_by | keyfn)))
+(defn to-array [coll]
+ "Returns an array of (seq coll)."
+ (.to_a (seq coll)))
+
+
+(ns ^{:doc "Implemenations of functions from clojure.string."}
+ rouge.string
+ (:use rouge.core ruby))
+
+(defn blank? [s]
+ "Returns true if s is falsy, empty, or contains only whitespace."
+ (if s
+ (if (or (= (.length s) 0)
+ true
+ (if (.all? (to-array s) | #(.match #"\s" %))
+ true
+ false))
+ false)))
+
+(defn lower-case [s]
+ "Converts the characters in string s to all lower-case."
+ (.downcase s))
+
+(defn upper-case [s]
+ "Converts the string s to all upper-case."
+ (.upcase s))
+
+(defn capitalize [s]
+ "Converts a string to all lower-case with the first character capitalized"
+ (-> s .downcase .capitalize))
+
+(defn trim [s]
+ "Removes all leading and trailing whitespace characters from the string s."
+ (.strip s))
+
+(defn ltrim [s]
+ "Removes all leading whitespace characters from the string s."
+ (.lstrip s))
+
+(defn rtrim [s]
+ "Removes all trailing whitespace characters from the string s."
+ (.rstrip s))
+
+(defn trim-newline [s]
+ "Removes all trailing newline characters from the string s."
+ (.sub s #"(\n|\r)*$" ""))
+
+(defn split [s delimiter]
+ "Splits a string s in to substrings based on delimiter. The delimiter may be
+ either another string or regular expression."
+ (.split s delimiter))
+
+(defn split-lines [s]
+ "Split the string s in to substrings at new lines."
+ (.split s #"\n|\r"))
+
+(defn join [separator coll]
+ "Returns a string in which all elements in coll are joined by the separator."
+ (.join (to-array coll) separator))
+
+(defn reverse [s]
+ "Returns the string s with it's characters in reverse order."
+ (.reverse s))
+
+;; TODO
+#_(defn escape [s cmap])
+
+;; TODO
+#_(defn replace [s match replacement])
+
+;; TODO
+#_(defn replace-first [s match replacement])
+
+
(ns rouge.test
(:use rouge.core ruby))
View
@@ -240,6 +240,8 @@ def self.seq(form)
end
when Enumerator
seq(form.to_a)
+ when String
+ seq(form.chars)
else
raise UnknownSeqError, form.inspect
end
View
@@ -37,7 +37,8 @@
(testing "seq"
(is (.nil? (seq ())))
(is (.nil? (seq nil)))
- (is (.nil? (seq []))))
+ (is (.nil? (seq [])))
+ (is (.nil? (seq ""))))
(testing "first"
(is (.nil? (first nil)))
@@ -90,6 +91,7 @@
(testing "map"
(is (= Rouge.Seq.Lazy (class (map inc [1 2 3]))))
(is (= '(2 3 4) (map inc [1 2 3])))
+ (is (= '("A" "B" "C") (map .upcase "abc")))
(is (= 1
(let [q (atom 0)
lazy (map #(do (swap! q inc) (inc %)) [1 2 3])]

0 comments on commit 2b5d31e

Please sign in to comment.