Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: weaver/hello-clojure
base: f0ae3d03d7
...
head fork: weaver/hello-clojure
compare: acbcc0b2e6
  • 2 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
2  README.md
View
@@ -6,7 +6,9 @@ experience with Clojure or Java.
+ hello-world
+ use-java
++ strings
+ basic-io
++ functional-data
## Installing Clojure ##
3  hello-world/README.md
View
@@ -4,6 +4,7 @@ This example shows how to start a new project with Leiningen and
create a main program entry point.
+ project.clj
-+ src/hello_world/core.clj
+ test/hello_world/test/core.clj
++ src/hello_world/core.clj
+
6 hello-world/src/hello_world/core.clj
View
@@ -9,6 +9,12 @@
;;;; + Update `core.clj` with your main program.
;;;; + A project can be run using `lein run`.
;;;; + Create a standalone `.jar` using `lein uberjar`.
+;;;;
+;;;; Once all of this is done, run the jar from the command line like
+;;;; this:
+;;;;
+;;;; java -jar hello-world-0.1.0-standalone.jar
+;;;;
;;; Main Program
6 hello-world/test/hello_world/test/core.clj
View
@@ -19,8 +19,10 @@
;; The [`clojure.test`][0] package exports `deftest` to define tests
;; and `is` to make assertions.
-(deftest test-main
- (is (= "Hello, world.\n" (with-out-str (-main)))))
+(deftest verify-main
+ (is (= (with-out-str (-main))
+ "Hello, world.\n")
+ "The main program produces the expected output."))
;; [0] http://clojure.github.com/clojure/clojure.test-api.html
;; [1] https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md
8 strings/.gitignore
View
@@ -0,0 +1,8 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
9 strings/README.md
View
@@ -0,0 +1,9 @@
+# Strings in Clojure #
+
+This project explores how strings and regular expressions work in
+Clojure.
+
++ project.clj
++ test/strings/test/core.clj
++ src/strings/core.clj
+
3  strings/project.clj
View
@@ -0,0 +1,3 @@
+(defproject strings "0.1.0"
+ :description "Demonstrate how to use strings and regular expressions."
+ :dependencies [[org.clojure/clojure "1.3.0"]])
104 strings/src/strings/core.clj
View
@@ -0,0 +1,104 @@
+;;;; Strings and Regular Expressions
+;;;;
+;;;; Clojure offers basic string and regexp support as part of its
+;;;; core library. Additional string methods are available in
+;;;; `clojure.string`.
+;;;;
+;;;; This project demonstrates how to interact with strings by
+;;;; implementing various string utility methods.
+
+(ns strings.core
+ (:require [clojure.string :as string])
+ (:import [java.util.regex Pattern]))
+
+
+;;; Basic String Support
+
+;; Using `with-out-str` is a simple way to accumulate a string
+;; iteratively. It binds `*out*` for the scope of its body, directing
+;; print output into a string buffer. The value of the buffer is
+;; returned.
+
+(defn repeat-string
+ "Create a string by repeatedly appending repeating `str` `limit`
+ times."
+
+ [str limit]
+ (with-out-str
+ (dotimes [_ limit]
+ (print str))))
+
+;; Clojure allows strings to be treated as sequences of characters.
+
+(defn starts-with?
+ "Does `string` begin with `prefix`?"
+
+ [string prefix]
+ (if (char? prefix)
+ (= (first string) prefix)
+ (loop [[p & rest-p] prefix
+ [s & rest-s] string]
+ (cond
+ (nil? p) true
+ (not= p s) false
+ true (recur rest-p rest-s)))))
+
+
+;;; Regular Expressions
+
+;; Regular Expressions are straightforward to use. The `#"..."` reader
+;; syntax produces a compiled pattern that can be matched using
+;; `re-matches`. The result of `re-matches` is a sequence of captured
+;; groups. The first item in the sequence is the matched string.
+;;
+;; See [Pattern][0] in the Java Docs for an overview of pattern
+;; syntax.
+
+(defn split-semicolons
+ "Split semi-colons that prefix a line off from the rest of the
+ line's body. Return (semi rest) values."
+
+ [line]
+ (let [[_ semi rest] (re-matches #"^(;+)\s*(.*)\s*$" line)]
+ (list semi rest)))
+
+;; Patterns can be compiled dynamically as well. Note the use of
+;; `Pattern/quote` to escape any special regex characters in the
+;; input.
+
+(defn strips
+ "Create a stripping function that removes leading and trailing
+ characters in the remove set from a string."
+
+ [remove]
+
+ (let [char-set (str "[" (Pattern/quote remove) "]*")
+ pattern (re-pattern (str \^ char-set "(.*?)" char-set \$))]
+
+ (fn [string]
+ (let [[_ middle] (re-matches pattern string)]
+ middle))))
+
+(defn strip
+ "Strip leading and trailing characters in the `remove` set from `string`."
+
+ [string remove]
+ ((strips remove) string))
+
+
+;;; String API
+
+;; Clojure provides additional string methods though the
+;; [clojure.string][1] module. For example, the core method `str`
+;; concatenates a fixed number of strings, but `string/join` operates
+;; on a sequence of strings.
+
+(defn join-paths
+ "Forcibly join filesystem path segments."
+
+ [& paths]
+
+ (string/join "/" (map (strips "/") paths)))
+
+;; [0]: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
+;; [1]: http://clojure.github.com/clojure/clojure.string-api.html
76 strings/test/strings/test/core.clj
View
@@ -0,0 +1,76 @@
+;;;; Strings and Regular Expressions
+;;;;
+;;;; Processing and munging strings is a fundamental task in many
+;;;; programs. Clojure offers strings and regular expressions at its
+;;;; core. Additional support is available through a supplemental
+;;;; `core.string` package or by using native Java classes and
+;;;; methods.
+
+(ns strings.test.core
+ (:use [strings.core])
+ (:use [clojure.test]))
+
+
+;;; Example Methods
+
+;; The methods implemented in this package demonstrate various aspects
+;; of working with strings. Otherwise, there's no particular
+;; motivation for them :-)
+
+(deftest test-repeat
+
+ (is (= (repeat-string "ab" 3)
+ "ababab")
+ "A string can be repeated a certain number of times."))
+
+(deftest test-starts-with?
+
+ (is (starts-with? "apple" \a)
+ "A string may start with a single character.")
+
+ (is (not (starts-with? "apple" \b))
+ "But `false` is returned when it doesn't.")
+
+ (is (starts-with? "apple" "ap")
+ "A string may start with a sub-string.")
+
+ (is (not (starts-with? "apple" "pp"))
+ "An it may not.")
+
+ (is (starts-with? "apple" "apple")
+ "A string starts with itself.")
+
+ (is (not (starts-with? "apple" "applejack"))
+ "But it doesn't start with something longer than itself."))
+
+(deftest test-split-semicolons
+
+ (is (= (split-semicolons ";; hello")
+ '(";;" "hello"))
+ "When a string begins with one or more semicolons, they're split
+ off.")
+
+ (is (= (split-semicolons "just a line")
+ '(nil nil))
+ "When no semicolons are found, `nil` is returned."))
+
+(deftest test-strip
+
+ (is (= (strip "mumble quux" "mux")
+ "ble q")
+ "Characters from the given set are removed from the beginning
+ and end of the string.")
+
+ (is (= (strip "foo bar" "baz")
+ "foo bar")
+ "When no characters match, the original string is returned."))
+
+(deftest test-join-paths
+
+ (is (= (join-paths "foo" "bar")
+ "foo/bar")
+ "Paths are joined with a forward slash.")
+
+ (is (= (join-paths "a/" "/b/" "c")
+ "a/b/c")
+ "Leading and trailing slashes on segments are normalized."))
40 use-java/src/use_java/core.clj
View
@@ -1,9 +1,15 @@
;;;; Using Java from Clojure
;;;;
-;;;; This example shows how to convert Markdown to HTML with the
-;;;; native Java project [pegdown][0] from Clojure. Begin by adding
-;;;; the dependency to `project.clj` and an `:import` to the `ns`
-;;;; declaration of a Clojure module.
+;;;; The use of [pegdown][0] demonstrates how Java libraries can be
+;;;; referenced directly from Clojure programs. Functions declared
+;;;; below make the pegdown dependency transparent to consumers of
+;;;; this project. For example, to read in markdown from a file and
+;;;; print the corresponding HTML:
+;;;;
+;;;; (file-to-html "path/to/file.md")
+;;;;
+;;;; A dependency has been added to `project.clj` and an `:import` to
+;;;; the `ns` declaration of this module.
(ns use-java.core
(:import [org.pegdown PegDownProcessor Extensions]))
@@ -11,11 +17,25 @@
;;; PegDown
-;; Static properties can be accessed using either `CLASS/NAME` or
-;; `(. CLASS NAME)`. Instances are constructed with a
-;; `(CLASS. arg...)` form. Methods are invoked by passing the instance
-;; and arguments to a method: `(.methodName obj arg...)`. See [Java
-;; Interop][1] for more details.
+;; Clojure's dot special form is the basis for interacting with
+;; Java. There are also several shortcut forms for common cases. For
+;; example:
+;;
+;; + Static properties can be accessed using `(. CLASS NAME)`
+;; or `CLASS/NAME`
+;;
+;; + Instances are constructed with a `(CLASS. arg...)` form or the
+;; new macro: `(new CLASS arg...)`
+;;
+;; + Methods are invoked by passing the instance and arguments to a
+;; method: `(.methodName obj arg...)` or with the dot form: `(. obj
+;; (methodName arg...))`
+;;
+;; See [Java Interop][1] for more details.
+;;
+;; Pegdown works by creating a `PegDownProcessor` instance and calling
+;; its `markdownToHtml` method on a string or stream. A processor can
+;; accept optional flags to modify the parser's behavior.
(def DEFAULT-OPTIONS
(bit-or
@@ -39,4 +59,4 @@
(markdown-to-html (slurp path)))
;; [0]: https://github.com/sirthias/pegdown
-;; [1]: http://clojure.org/java_interop
+;; [1]: http://clojure.org/java_interop
23 use-java/test/use_java/test/core.clj
View
@@ -1,19 +1,28 @@
-;;;; Tests for Using Java
+;;;; Using Java
;;;;
-;;;; Here are a few simple tests. Because the library is just a small
-;;;; wrapper around pegdown, there's not much that needs to be
-;;;; covered.
+;;;; This project offers supports the conversion of Markdown documents
+;;;; to HTML using the native Java project [pegdown][0]. A document
+;;;; may be a file or a string in memory.
(ns use-java.test.core
(:use [use-java.core])
(:use [clojure.test]))
+;; Example markdown documents and expected HTML output are kept in the
+;; adjacent "resources" folder.
+
+(defn resource
+ "Expand a resource name to a project-relative path."
+
+ [name]
+ (str "test/use_java/resources/" name))
+
(deftest test-markdown-to-html
(is (= (markdown-to-html "# foo #\nbar")
"<h1>foo</h1><p>bar</p>")))
(deftest test-file-to-html
- (is (= (file-to-html "test/use_java/resources/example.md")
- (slurp "test/use_java/resources/example.html"))))
+ (is (= (file-to-html (resource "example.md"))
+ (slurp (resource "example.html")))))
-(run-tests)
+;; [0]: https://github.com/sirthias/pegdown

No commit comments for this range

Something went wrong with that request. Please try again.