File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 3047
3047
(concat (flatten e) (flatten r))
3048
3048
(cons e (flatten r)))))))
3049
3049
3050
+ (defn take-last
3051
+ "Return the last n items in coll in linear time."
3052
+ [n coll]
3053
+ (loop [c (seq coll)
3054
+ rem (seq (drop n coll))]
3055
+ (if rem
3056
+ (recur (next c) (next rem))
3057
+ c)))
3058
+
3050
3059
(defn min-key
3051
3060
"Return the arg for which (k arg) is the smallest number.
3052
3061
If multiple values return the same number, return the last."
3102
3111
(recur (ret))
3103
3112
ret)))
3104
3113
3114
+ (defn tree-seq
3115
+ "Return a lazy seq on the nodes of the tree-like data structure `root`.
3116
+
3117
+ `branch?` should be a function of a single argument which should return
3118
+ true if a node might contain children (though it need not).
3119
+
3120
+ `children` should be a function of one argument which should return a
3121
+ sequence of children of a node. `children` will only be called on a node
3122
+ if `branch?` returns true for that node."
3123
+ [branch? children root]
3124
+ (let [walk (fn walk
3125
+ [node]
3126
+ (lazy-seq
3127
+ (cons node
3128
+ (when (branch? node)
3129
+ (mapcat walk (children node))))))]
3130
+ (walk root)))
3131
+
3105
3132
(import* multiprocessing)
3106
3133
3107
3134
(def ^:dynamic *pmap-cpu-count*
3745
3772
(throw (python/IndexError "Start index must be less than or equal to end index"))))
3746
3773
(operator/getitem s (python/slice start end))))
3747
3774
3775
+ ;;;;;;;;;;;;;;;;;;;;
3776
+ ;; File Functions ;;
3777
+ ;;;;;;;;;;;;;;;;;;;;
3778
+
3779
+ (import* pathlib)
3780
+
3781
+ (defn file-seq
3782
+ "Return a seq of `pathlib.Path` objects for all files and subdirectories of `dir`."
3783
+ [dir]
3784
+ (-> (pathlib/Path dir)
3785
+ (.resolve)
3786
+ (.rglob "*")
3787
+ (seq)))
3788
+
3748
3789
;;;;;;;;;;;;;;;;;;;;;;
3749
3790
;; Output Utilities ;;
3750
3791
;;;;;;;;;;;;;;;;;;;;;;
Original file line number Diff line number Diff line change 934
934
'(1) [[[1]]]
935
935
'(1 2 3) [[[1]] 2 [3]]))
936
936
937
+ (deftest take-last-test
938
+ (are [res n coll] (= res (take-last n coll))
939
+ [:c :d] 2 [:a :b :c :d]
940
+ [:a] 2 [:a]
941
+ nil 2 []
942
+ nil 2 nil
943
+ nil 0 [:a]
944
+ nil -1 [:a]))
945
+
937
946
(deftest min-key-test
938
947
(is (= "dsd" (max-key count "asd" "bsd" "dsd")))
939
948
(is (= "long word" (max-key count "asd" "bsd" "dsd" "long word")))
976
985
"idiot")
977
986
(is (= [:a :b] @a))))
978
987
988
+ (deftest tree-seq-test
989
+ (are [res branch? children root] (= res (tree-seq branch? children root))
990
+ '(((1 2 (3)) (4)) (1 2 (3)) 1 2 (3) 3 (4) 4) seq? identity '((1 2 (3)) (4))
991
+ '((:A (:B (:D) (:E)) (:C (:F))) (:B (:D) (:E)) (:D) (:E) (:C (:F)) (:F)) next rest '(:A (:B (:D) (:E)) (:C (:F)))))
992
+
979
993
;;;;;;;;;;;;;;;;;;;;;;;;;;;
980
994
;; Associative Functions ;;
981
995
;;;;;;;;;;;;;;;;;;;;;;;;;;;
You can’t perform that action at this time.
0 commit comments