Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joodie committed Apr 5, 2011
0 parents commit 7390d4f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pom.xml
*jar
lib
classes
26 changes: 26 additions & 0 deletions README.md
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.
4 changes: 4 additions & 0 deletions project.clj
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"]])
32 changes: 32 additions & 0 deletions src/pretzel/combine.clj
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))


55 changes: 55 additions & 0 deletions src/pretzel/strings.clj
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))

0 comments on commit 7390d4f

Please sign in to comment.