Clean up whitespace

This commit is contained in:
Kodi Arfer 2019-07-02 12:10:01 -04:00
parent 308bedbebe
commit 6bc9e842e1
5 changed files with 72 additions and 63 deletions

View File

@ -3,53 +3,60 @@
;; license. See the LICENSE. ;; license. See the LICENSE.
(defclass Sequence [] (defclass Sequence []
(defn --init-- [self func] (defn --init-- [self func]
"initialize a new sequence with a function to compute values" "initialize a new sequence with a function to compute values"
(setv (. self func) func) (setv (. self func) func)
(setv (. self cache) []) (setv (. self cache) [])
(setv (. self high-water) -1)) (setv (. self high-water) -1))
(defn --getitem-- [self n] (defn --getitem-- [self n]
"get nth item of sequence" "get nth item of sequence"
(if (hasattr n "start") (if (hasattr n "start")
(gfor x (range n.start n.stop (or n.step 1)) (gfor x (range n.start n.stop (or n.step 1))
(get self x)) (get self x))
(do (when (neg? n) (do (when (neg? n)
; Call (len) to force the whole ; Call (len) to force the whole
; sequence to be evaluated. ; sequence to be evaluated.
(len self)) (len self))
(if (<= n (. self high-water)) (if (<= n (. self high-water))
(get (. self cache) n) (get (. self cache) n)
(do (while (< (. self high-water) n) (do (while (< (. self high-water) n)
(setv (. self high-water) (inc (. self high-water))) (setv (. self high-water) (inc (. self high-water)))
(.append (. self cache) (.func self (. self high-water)))) (.append (. self cache) (.func self (. self high-water))))
(get self n)))))) (get self n))))))
(defn --iter-- [self] (defn --iter-- [self]
"create iterator for this sequence" "create iterator for this sequence"
(setv index 0) (setv index 0)
(try (while True (try (while True
(yield (get self index)) (yield (get self index))
(setv index (inc index))) (setv index (inc index)))
(except [IndexError] (except [IndexError]
(return)))) (return))))
(defn --len-- [self] (defn --len-- [self]
"length of the sequence, dangerous for infinite sequences" "length of the sequence, dangerous for infinite sequences"
(setv index (. self high-water)) (setv index (. self high-water))
(try (while True (try (while True
(get self index) (get self index)
(setv index (inc index))) (setv index (inc index)))
(except [IndexError] (except [IndexError]
(len (. self cache))))) (len (. self cache)))))
(setv max-items-in-repr 10) (setv max-items-in-repr 10)
(defn --str-- [self] (defn --str-- [self]
"string representation of this sequence" "string representation of this sequence"
(setv items (list (take (inc self.max-items-in-repr) self))) (setv items (list (take (inc self.max-items-in-repr) self)))
(.format (if (> (len items) self.max-items-in-repr) (.format (if (> (len items) self.max-items-in-repr)
"[{0}, ...]" "[{0}, ...]"
"[{0}]") "[{0}]")
(.join ", " (map str items)))) (.join ", " (map str items))))
(defn --repr-- [self] (defn --repr-- [self]
"string representation of this sequence" "string representation of this sequence"
(.--str-- self))) (.--str-- self)))
(defmacro seq [param &rest seq-code] (defmacro seq [param &rest seq-code]
`(Sequence (fn ~param (do ~@seq-code)))) `(Sequence (fn ~param (do ~@seq-code))))

View File

@ -37,7 +37,7 @@
(defclass B [] (defclass B []
(setv x 42) (setv x 42)
(setv y (fn [self value] (setv y (fn [self value]
(+ self.x value)))) (+ self.x value))))
(assert (= B.x 42)) (assert (= B.x 42))
(assert (= (.y (B) 5) 47)) (assert (= (.y (B) 5) 47))
(setv b (B)) (setv b (B))

View File

@ -1773,20 +1773,22 @@ macros()
(defn test-pep-3115 [] (defn test-pep-3115 []
(defclass member-table [dict] (defclass member-table [dict]
(defn --init-- [self] (setv self.member-names [])) (defn --init-- [self]
(setv self.member-names []))
(defn --setitem-- [self key value] (defn --setitem-- [self key value]
(if (not-in key self) (if (not-in key self)
(.append self.member-names key)) (.append self.member-names key))
(dict.--setitem-- self key value))) (dict.--setitem-- self key value)))
(defclass OrderedClass [type] (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] (defn --new-- [cls name bases classdict]
(setv result (type.--new-- cls name bases (dict classdict))) (setv result (type.--new-- cls name bases (dict classdict)))
(setv result.member-names classdict.member-names) (setv result.member-names classdict.member-names)
result)) result))
(defclass MyClass [:metaclass OrderedClass] (defclass MyClass [:metaclass OrderedClass]
(defn method1 [self] (pass)) (defn method1 [self] (pass))

View File

@ -160,19 +160,19 @@
(defclass HyTestMatrix [list] (defclass HyTestMatrix [list]
(defn --matmul-- [self other] (defn --matmul-- [self other]
(setv n (len self) (setv n (len self)
m (len (. other [0])) m (len (. other [0]))
result []) result [])
(for [i (range m)] (for [i (range m)]
(setv result-row []) (setv result-row [])
(for [j (range n)] (for [j (range n)]
(setv dot-product 0) (setv dot-product 0)
(for [k (range (len (. self [0])))] (for [k (range (len (. self [0])))]
(+= dot-product (* (. self [i] [k]) (+= dot-product (* (. self [i] [k])
(. other [k] [j])))) (. other [k] [j]))))
(.append result-row dot-product)) (.append result-row dot-product))
(.append result result-row)) (.append result result-row))
result)) result))
(setv first-test-matrix (HyTestMatrix [[1 2 3] (setv first-test-matrix (HyTestMatrix [[1 2 3]
[4 5 6] [4 5 6]

View File

@ -41,7 +41,7 @@
(defn test-pep-487 [] (defn test-pep-487 []
(defclass QuestBase [] (defclass QuestBase []
(defn --init-subclass-- [cls swallow &kwargs kwargs] (defn --init-subclass-- [cls swallow &kwargs kwargs]
(setv cls.swallow swallow))) (setv cls.swallow swallow)))
(defclass Quest [QuestBase :swallow "african"]) (defclass Quest [QuestBase :swallow "african"])
(assert (= (. (Quest) swallow) "african"))) (assert (= (. (Quest) swallow) "african")))