Skip to content

Commit 71882b5

Browse files
authored
A few more seq utility functions (#618)
1 parent 382fa61 commit 71882b5

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/basilisp/core.lpy

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,15 @@
30473047
(concat (flatten e) (flatten r))
30483048
(cons e (flatten r)))))))
30493049

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+
30503059
(defn min-key
30513060
"Return the arg for which (k arg) is the smallest number.
30523061
If multiple values return the same number, return the last."
@@ -3102,6 +3111,24 @@
31023111
(recur (ret))
31033112
ret)))
31043113

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+
31053132
(import* multiprocessing)
31063133

31073134
(def ^:dynamic *pmap-cpu-count*
@@ -3745,6 +3772,20 @@
37453772
(throw (python/IndexError "Start index must be less than or equal to end index"))))
37463773
(operator/getitem s (python/slice start end))))
37473774

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+
37483789
;;;;;;;;;;;;;;;;;;;;;;
37493790
;; Output Utilities ;;
37503791
;;;;;;;;;;;;;;;;;;;;;;

tests/basilisp/test_core_fns.lpy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,15 @@
934934
'(1) [[[1]]]
935935
'(1 2 3) [[[1]] 2 [3]]))
936936

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+
937946
(deftest min-key-test
938947
(is (= "dsd" (max-key count "asd" "bsd" "dsd")))
939948
(is (= "long word" (max-key count "asd" "bsd" "dsd" "long word")))
@@ -976,6 +985,11 @@
976985
"idiot")
977986
(is (= [:a :b] @a))))
978987

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+
979993
;;;;;;;;;;;;;;;;;;;;;;;;;;;
980994
;; Associative Functions ;;
981995
;;;;;;;;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)