Clean up whitespace
This commit is contained in:
parent
308bedbebe
commit
6bc9e842e1
@ -3,53 +3,60 @@
|
||||
;; license. See the LICENSE.
|
||||
|
||||
(defclass Sequence []
|
||||
|
||||
(defn --init-- [self func]
|
||||
"initialize a new sequence with a function to compute values"
|
||||
(setv (. self func) func)
|
||||
(setv (. self cache) [])
|
||||
(setv (. self high-water) -1))
|
||||
"initialize a new sequence with a function to compute values"
|
||||
(setv (. self func) func)
|
||||
(setv (. self cache) [])
|
||||
(setv (. self high-water) -1))
|
||||
|
||||
(defn --getitem-- [self n]
|
||||
"get nth item of sequence"
|
||||
(if (hasattr n "start")
|
||||
(gfor x (range n.start n.stop (or n.step 1))
|
||||
(get self x))
|
||||
(do (when (neg? n)
|
||||
; Call (len) to force the whole
|
||||
; sequence to be evaluated.
|
||||
(len self))
|
||||
(if (<= n (. self high-water))
|
||||
(get (. self cache) n)
|
||||
(do (while (< (. self high-water) n)
|
||||
(setv (. self high-water) (inc (. self high-water)))
|
||||
(.append (. self cache) (.func self (. self high-water))))
|
||||
(get self n))))))
|
||||
"get nth item of sequence"
|
||||
(if (hasattr n "start")
|
||||
(gfor x (range n.start n.stop (or n.step 1))
|
||||
(get self x))
|
||||
(do (when (neg? n)
|
||||
; Call (len) to force the whole
|
||||
; sequence to be evaluated.
|
||||
(len self))
|
||||
(if (<= n (. self high-water))
|
||||
(get (. self cache) n)
|
||||
(do (while (< (. self high-water) n)
|
||||
(setv (. self high-water) (inc (. self high-water)))
|
||||
(.append (. self cache) (.func self (. self high-water))))
|
||||
(get self n))))))
|
||||
|
||||
(defn --iter-- [self]
|
||||
"create iterator for this sequence"
|
||||
(setv index 0)
|
||||
(try (while True
|
||||
(yield (get self index))
|
||||
(setv index (inc index)))
|
||||
(except [IndexError]
|
||||
(return))))
|
||||
"create iterator for this sequence"
|
||||
(setv index 0)
|
||||
(try (while True
|
||||
(yield (get self index))
|
||||
(setv index (inc index)))
|
||||
(except [IndexError]
|
||||
(return))))
|
||||
|
||||
(defn --len-- [self]
|
||||
"length of the sequence, dangerous for infinite sequences"
|
||||
(setv index (. self high-water))
|
||||
(try (while True
|
||||
(get self index)
|
||||
(setv index (inc index)))
|
||||
(except [IndexError]
|
||||
(len (. self cache)))))
|
||||
"length of the sequence, dangerous for infinite sequences"
|
||||
(setv index (. self high-water))
|
||||
(try (while True
|
||||
(get self index)
|
||||
(setv index (inc index)))
|
||||
(except [IndexError]
|
||||
(len (. self cache)))))
|
||||
|
||||
(setv max-items-in-repr 10)
|
||||
|
||||
(defn --str-- [self]
|
||||
"string representation of this sequence"
|
||||
(setv items (list (take (inc self.max-items-in-repr) self)))
|
||||
(.format (if (> (len items) self.max-items-in-repr)
|
||||
"[{0}, ...]"
|
||||
"[{0}]")
|
||||
(.join ", " (map str items))))
|
||||
"string representation of this sequence"
|
||||
(setv items (list (take (inc self.max-items-in-repr) self)))
|
||||
(.format (if (> (len items) self.max-items-in-repr)
|
||||
"[{0}, ...]"
|
||||
"[{0}]")
|
||||
(.join ", " (map str items))))
|
||||
|
||||
(defn --repr-- [self]
|
||||
"string representation of this sequence"
|
||||
(.--str-- self)))
|
||||
"string representation of this sequence"
|
||||
(.--str-- self)))
|
||||
|
||||
(defmacro seq [param &rest seq-code]
|
||||
`(Sequence (fn ~param (do ~@seq-code))))
|
||||
|
@ -37,7 +37,7 @@
|
||||
(defclass B []
|
||||
(setv x 42)
|
||||
(setv y (fn [self value]
|
||||
(+ self.x value))))
|
||||
(+ self.x value))))
|
||||
(assert (= B.x 42))
|
||||
(assert (= (.y (B) 5) 47))
|
||||
(setv b (B))
|
||||
|
@ -1773,20 +1773,22 @@ macros()
|
||||
|
||||
(defn test-pep-3115 []
|
||||
(defclass member-table [dict]
|
||||
(defn --init-- [self] (setv self.member-names []))
|
||||
(defn --init-- [self]
|
||||
(setv self.member-names []))
|
||||
|
||||
(defn --setitem-- [self key value]
|
||||
(if (not-in key self)
|
||||
(.append self.member-names key))
|
||||
(dict.--setitem-- self key value)))
|
||||
(if (not-in key self)
|
||||
(.append self.member-names key))
|
||||
(dict.--setitem-- self key value)))
|
||||
|
||||
(defclass OrderedClass [type]
|
||||
(setv --prepare-- (classmethod (fn [metacls name bases] (member-table))))
|
||||
(setv --prepare-- (classmethod (fn [metacls name bases]
|
||||
(member-table))))
|
||||
|
||||
(defn --new-- [cls name bases classdict]
|
||||
(setv result (type.--new-- cls name bases (dict classdict)))
|
||||
(setv result.member-names classdict.member-names)
|
||||
result))
|
||||
(setv result (type.--new-- cls name bases (dict classdict)))
|
||||
(setv result.member-names classdict.member-names)
|
||||
result))
|
||||
|
||||
(defclass MyClass [:metaclass OrderedClass]
|
||||
(defn method1 [self] (pass))
|
||||
|
@ -160,19 +160,19 @@
|
||||
|
||||
(defclass HyTestMatrix [list]
|
||||
(defn --matmul-- [self other]
|
||||
(setv n (len self)
|
||||
m (len (. other [0]))
|
||||
result [])
|
||||
(for [i (range m)]
|
||||
(setv result-row [])
|
||||
(for [j (range n)]
|
||||
(setv dot-product 0)
|
||||
(for [k (range (len (. self [0])))]
|
||||
(+= dot-product (* (. self [i] [k])
|
||||
(. other [k] [j]))))
|
||||
(.append result-row dot-product))
|
||||
(.append result result-row))
|
||||
result))
|
||||
(setv n (len self)
|
||||
m (len (. other [0]))
|
||||
result [])
|
||||
(for [i (range m)]
|
||||
(setv result-row [])
|
||||
(for [j (range n)]
|
||||
(setv dot-product 0)
|
||||
(for [k (range (len (. self [0])))]
|
||||
(+= dot-product (* (. self [i] [k])
|
||||
(. other [k] [j]))))
|
||||
(.append result-row dot-product))
|
||||
(.append result result-row))
|
||||
result))
|
||||
|
||||
(setv first-test-matrix (HyTestMatrix [[1 2 3]
|
||||
[4 5 6]
|
||||
|
@ -41,7 +41,7 @@
|
||||
(defn test-pep-487 []
|
||||
(defclass QuestBase []
|
||||
(defn --init-subclass-- [cls swallow &kwargs kwargs]
|
||||
(setv cls.swallow swallow)))
|
||||
(setv cls.swallow swallow)))
|
||||
|
||||
(defclass Quest [QuestBase :swallow "african"])
|
||||
(assert (= (. (Quest) swallow) "african")))
|
||||
|
Loading…
Reference in New Issue
Block a user