-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7390d4f
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pom.xml | ||
*jar | ||
lib | ||
classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# pretzel | ||
|
||
General predicate functions. | ||
Combine predicates into new ones, plus a bunch of predicates on strings. | ||
|
||
## Usage | ||
|
||
(looks-like-email? "foo@example.com") | ||
(natural? "1234") | ||
(integer? "-123213") | ||
(web-url "http://foo.bar/somewhere") | ||
(hex? "deadb33f") | ||
(length? "bla" 3) | ||
(length? "bla" 2 4) | ||
|
||
((every-p? | ||
natural | ||
#(length % 2 3) | ||
"123") | ||
|
||
## License | ||
|
||
Copyright (C) 2010 Joost Diepenmaat, Zeekat Softwareontwikkeling | ||
joost@zeekat.nl - http://joost.zeekat.nl/ | ||
|
||
Distributed under the Eclipse Public License, the same as Clojure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(defproject pretzel "1.0.0-SNAPSHOT" | ||
:description "FIXME: write" | ||
:dependencies [[org.clojure/clojure "1.2.0"] | ||
[org.clojure/clojure-contrib "1.2.0"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns pretzel.combine | ||
{:doc "Combine predicates"}) | ||
|
||
(defn combine-p | ||
"Combine predicates using collection function f. | ||
Returns a new predicate that tests the collection of predicates | ||
against the given value(s) using f." | ||
[f predicates] | ||
(fn [& values] | ||
(f #(apply % values) predicates))) | ||
|
||
(defn every-p? | ||
"Create a new predicate which is true if every predicate is true" | ||
[& predicates] | ||
(combine-p every? predicates)) | ||
|
||
(defn some-p | ||
"Create a new predicate returning the first true value of predicates" | ||
[& predicates] | ||
(combine-p some predicates)) | ||
|
||
(defn not-any-p? | ||
"Create a new predicate returning false if all predicates return true" | ||
[& predicates] | ||
(combine-p not-any? predicates)) | ||
|
||
(defn not-every-p? | ||
"Create a new predicate returning true if any predicate returns false" | ||
[& predicates] | ||
(combine-p not-every? predicates)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns pretzel.strings | ||
{:doc "Predicates on strings"} | ||
(:import java.net.URL | ||
java.net.MalformedURLException)) | ||
|
||
(defn length? | ||
"true if length of string s is len or within the range [min ... max]" | ||
([^String s len] | ||
(= (.length s) len)) | ||
([^String s min max] | ||
(<= min (.length s) max))) | ||
|
||
(defn natural? | ||
"s represents a natural number (0 - 9999...)" | ||
[^String s] | ||
(re-matches #"\A[0-9]+\z")) | ||
|
||
(defn integer? | ||
"s represents an integer (- 999... to 999...)" | ||
[^String s] | ||
(re-matches #"\A-?[0-9]+\z")) | ||
|
||
(defn hex? | ||
"s is a hexadecimal string (case-insensitive)" | ||
[^String s] | ||
(re-matches #"\A[0-9A-Fa-f]+\z" s)) | ||
|
||
(defn blank? | ||
"s contains only whitespace" | ||
[^String s] | ||
(every? #(Character/isWhitespace %) s)) | ||
|
||
(defn url | ||
"return a java.net.URL from string or nil if s is malformed. | ||
Note that this will mean any URL valid according to java.net.URL" | ||
[^String s] | ||
(try | ||
(URL. s) | ||
(catch MalformedURLException e | ||
nil))) | ||
|
||
(defn web-url | ||
"returns a java.net.URL if that is an http or https URL" | ||
[^String s] | ||
(if-let [u (url s)] | ||
(if-let [p (.getProtocol s)] | ||
(or (= p "http") | ||
(= p "https"))))) | ||
|
||
(defn looks-like-email? | ||
"a fairly permissive check; s should contain no whitespace, a @ | ||
character between the name and the domain, and a dot in the domain. | ||
People with email at a TLD probably have other mail addresses too" | ||
[^String s] | ||
(re-matches #"\A[^\s@]+@[^\s@]+\.[^\s@]+\z" s)) |