Skip to content
Browse files

more tests, functions, etc.

  • Loading branch information...
1 parent f324aad commit 942980f313184752226a79b1fbb659892472be36 @halgari committed Apr 26, 2012
View
4 clojure/core-deftype.clj
@@ -73,14 +73,14 @@
~@'([self & args]
(throw (AbstractMethodCall self))))) sigs))
methods (assoc methods "__doc__" docstr)]
- `(do (def ~name (py/type ~(clojure.core/name name)
+ (debug `(do (def ~name (py/type ~(clojure.core/name name)
(py/tuple [py/object])
(.toDict ~methods)))
(clojure.lang.protocol/protocolFromType ~'__name__ ~name)
~@(for [s sigs :when (string? (last s))]
`(py/setattr (resolve ~(list 'quote (first s)))
"__doc__"
- ~(last s))))))
+ ~(last s)))))))
(defmacro reify
View
54 clojure/core.clj
@@ -3303,6 +3303,60 @@
(.isExtendedBy p atype))
(py/issubclass protocol atype)))
+(defn satisfies?
+ "Returns true if x satisfies the protocol"
+ {:added "1.2"}
+ [protocol x]
+ (extends? (class x) protocol))
+
+(defn sequential?
+ "Returns true if coll implements Sequential"
+ {:added "1.0"
+ :static true}
+ [coll] (instance? clojure.lang/Sequential coll))
+
+(defmacro letfn
+ "fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
+
+ Takes a vector of function specs and a body, and generates a set of
+ bindings of functions to their names. All of the names are available
+ in all of the definitions of the functions, as well as the body."
+ {:added "1.0", :forms '[(letfn [fnspecs*] exprs*)],
+ :special-form true, :url nil}
+ [fnspecs & body]
+ `(let ~(vec (interleave (map first fnspecs)
+ (map #(cons `fn %) fnspecs)))
+ ~@body))
+
+(defn rseq
+ "Returns, in constant time, a seq of the items in rev (which
+ can be a vector or sorted-map), in reverse order. If rev is empty returns nil"
+ {:added "1.0"
+ :static true}
+ [rev]
+ (. rev (rseq)))
+
+(defn map-indexed
+ "Returns a lazy sequence consisting of the result of applying f to 0
+ and the first item of coll, followed by applying f to 1 and the second
+ item in coll, etc, until coll is exhausted. Thus function f should
+ accept 2 arguments, index and item."
+ {:added "1.2"
+ :static true}
+ [f coll]
+ (letfn [(mapi [idx coll]
+ (lazy-seq
+ (when-let [s (seq coll)]
+ (if (chunked-seq? s)
+ (let [c (chunk-first s)
+ size (int (count c))
+ b (chunk-buffer size)]
+ (dotimes [i size]
+ (chunk-append b (f (+ idx i) (.nth c i))))
+ (chunk-cons (chunk b) (mapi (+ idx size) (chunk-rest s))))
+ (cons (f idx (first s)) (mapi (inc idx) (rest s)))))))]
+ (mapi 0 coll)))
+
(defn parents
"Returns the immediate parents of tag, either via a Java type
inheritance relationship or a relationship established via derive. h
View
4 clojure/lang/__init__.py
@@ -1,2 +1,4 @@
from ilookup import ILookup
-from ifn import IFn
+from ifn import IFn
+from sequential import Sequential
+from iobj import IObj
View
1 clojure/lang/compiler.py
@@ -743,7 +743,6 @@ def compileThrow(comp, form):
@register_builtin("applyTo")
def compileApply(comp, form):
- print "foo", form
s = form.next()
code = []
while s is not None:
View
6 clojure/lang/protocol.py
@@ -202,7 +202,11 @@ def extendProtocolForClass(proto, tp):
pfn = proto.protofns[fn]
if hasattr(tp, fn):
- pfn.extend(tp, getattr(tp, fn))
+ try:
+ pfn.extend(tp, getattr(tp, fn))
+ except AttributeError as e:
+ print "Can't extend, got ", str(pfn), type(pfn)
+ raise
proto.markImplementor(tp)
View
1 clojure/standardimports.py
@@ -8,6 +8,7 @@
from clojure.lang.ideref import IDeref
from clojure.lang.seqable import Seqable
from clojure.lang.atom import Atom
+from clojure.lang.iobj import IObj
from clojure.lang.iseq import ISeq
from clojure.lang.var import Var
View
50 core/match.clj
@@ -673,7 +673,7 @@
(declare default-specialize-matrix)
(deftype PatternMatrix [rows ocrs _meta]
- clojure.lang.IObj
+ clojure.lang/IObj
(meta [_] _meta)
(withMeta [_ new-meta]
@@ -836,11 +836,10 @@
(deftype WildcardPattern [sym _meta]
IWildcardPattern
(sym [_] sym)
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(WildcardPattern. sym new-meta))
- Object
(toString [_]
(str sym)))
@@ -872,7 +871,7 @@
;; It "literally" matches a given occurrence.
(deftype LiteralPattern [l _meta]
- clojure.lang.IObj
+ clojure.lang/IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(LiteralPattern. l new-meta))
@@ -882,7 +881,7 @@
(= l ()) `(empty? ~ocr)
(and (symbol? l) (not (-> l meta :local))) `(= ~ocr '~l)
:else `(= ~ocr ~l)))
- Object
+ py/object
(toString [_]
(if (nil? l)
"nil"
@@ -910,14 +909,14 @@
(declare seq-pattern)
(deftype SeqPattern [s _meta]
- clojure.lang.IObj
+ clojure.lang/IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(SeqPattern. s new-meta))
IPatternCompile
(to-source* [this ocr]
`(or (seq? ~ocr) (sequential? ~ocr)))
- Object
+ py/object
(toString [_]
(str s))
ISpecializeMatrix
@@ -975,11 +974,11 @@
(deftype RestPattern [p _meta]
IPatternContainer
(pattern [_] p)
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(RestPattern. p new-meta))
- Object
+ py/object
(toString [_]
p))
@@ -1001,18 +1000,17 @@
(declare guard-pattern)
(deftype MapPattern [m _meta]
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(MapPattern. m new-meta))
+ (toString [_]
+ (str m " :only " (or (:only _meta) [])))
IPatternCompile
(to-source* [this ocr]
(if *clojurescript*
`(or (satisfies? cljs.core.ILookup ~ocr))
`(or (instance? clojure.lang.ILookup ~ocr) (satisfies? IMatchLookup ~ocr))))
- Object
- (toString [_]
- (str m " :only " (or (:only _meta) [])))
ISpecializeMatrix
(specialize-matrix [this rows ocrs]
(let [focr (first ocrs)
@@ -1107,18 +1105,17 @@
(into [])))
(deftype VectorPattern [v t size offset rest? _meta]
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(VectorPattern. v t size offset rest? new-meta))
+ (toString [_]
+ (str v " " t))
IPatternCompile
(to-source* [this ocr]
(if (and (touched? this) (not rest?) size (check-size? t))
(test-with-size-inline t ocr size)
(test-inline t ocr)))
- Object
- (toString [_]
- (str v " " t))
IContainsRestPattern
(contains-rest-pattern? [_] rest?)
IVectorPattern
@@ -1213,11 +1210,10 @@
;; Or Patterns
(deftype OrPattern [ps _meta]
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(OrPattern. ps new-meta))
- Object
(toString [this]
(str ps))
ISpecializeMatrix
@@ -1268,18 +1264,17 @@
(declare guard-pattern?)
(deftype GuardPattern [p gs _meta]
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(GuardPattern. p gs new-meta))
+ (toString [this]
+ (str p " :guard " gs))
IPatternCompile
(to-source* [this ocr]
`(and ~@(map (fn [expr ocr]
(list expr ocr))
gs (repeat ocr))))
- Object
- (toString [this]
- (str p " :guard " gs))
ISpecializeMatrix
(specialize-matrix [this rows ocrs]
(let [nrows (->> rows
@@ -1330,18 +1325,17 @@
(declare predicate-pattern?)
(deftype PredicatePattern [p gs _meta]
- clojure.lang.IObj
+ IObj
(meta [_] _meta)
(withMeta [_ new-meta]
(PredicatePattern. p gs new-meta))
+ (toString [this]
+ (str p " :when " gs))
IPatternCompile
(to-source* [this ocr]
`(and ~@(map (fn [expr ocr]
(list expr ocr))
gs (repeat ocr))))
- Object
- (toString [this]
- (str p " :when " gs))
ISpecializeMatrix
(specialize-matrix [this rows ocrs]
(let [nrows (->> rows
@@ -1568,7 +1562,7 @@
(keys (.getMethodTable ^clojure.lang.MultiFn emit-pattern-for-syntax))))))))
-(let [void (Object.)
+(let [void (py/object.)
void? #(identical? void %)
infix-keyword? #(#{:when :as :guard} %)]
;; void is a unique placeholder for nothing -- we can't use nil
View
12 tests/core-tests.clj
@@ -827,3 +827,15 @@
(deftest extends?-tests
(a/assert-true (extends? (type '()) ISeq))
(a/assert-false (extends? (type 1) ISeq)))
+
+(deftest satisfies?-tests
+ (a/assert-true (satisfies? ISeq '()))
+ (a/assert-false (satisfies? ISeq 1)))
+
+(deftest letfn-tests
+ (letfn [(twice [x]
+ (* x 2))
+ (six-times [y]
+ (* (twice y) 3))]
+ (a/assert-equal (twice 15) 30)
+ (a/assert-equal (six-times 15) 90)))

0 comments on commit 942980f

Please sign in to comment.
Something went wrong with that request. Please try again.