Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit flatten #108

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ New Features
* New macro `pun`.
* New macro `map-hyseq`.
* `loop` allows more kinds of parameters.
* `flatten`, given a non-collection, returns it as a singleton list,
instead of raising an error.

Bug Fixes
------------------------------
Expand Down
28 changes: 19 additions & 9 deletions hyrule/iterables.hy
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,30 @@


(defn flatten [coll]
#[=[Recurisvely collect all the elements and subelements of ``coll`` into a
single list. :hy:func:`coll?` is used to decide whether objects should be
descended into. ::
#[=[Recurisvely collect all the elements and subelements of ``coll``,
depth-first, and return them in a single list. :hy:func:`coll?` is used to
decide whether objects should be descended into. ::


(flatten ["foo" #(1 2) [1 [2 3] 4] "bar"])
; => ["foo" 1 2 1 2 3 4 "bar"]]=]
(if (coll? coll)
(_flatten coll [])
(raise (TypeError (.format "{0!r} is not a collection" coll)))))
; => ["foo" 1 2 1 2 3 4 "bar"]

Since iteration is used to collect the elements of ``coll``, dictionaries
are reduced to lists of keys::

(flatten [{"a" 1 "b" 2} {"c" 3 "d" 4}])
; => ["a" "b" "c" "d"]

If ``coll`` isn't a collection at all, it's returned in a singleton list::

(flatten "hello")
; => ["hello"]]=]
(_flatten coll []))

(defn _flatten [coll result]
(if (coll? coll)
(do (for [b coll]
(_flatten b result)))
(for [x coll]
(_flatten x result))
(.append result coll))
result)

Expand Down
30 changes: 13 additions & 17 deletions tests/test_iterables.hy
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,19 @@


(defn test-flatten []
(setv res (flatten [1 2 [3 4] 5]))
(assert (= res [1 2 3 4 5]))
(setv res (flatten ["foo" #(1 2) [1 [2 3] 4] "bar"]))
(assert (= res ["foo" 1 2 1 2 3 4 "bar"]))
(setv res (flatten [1]))
(assert (= res [1]))
(setv res (flatten []))
(assert (= res []))
(setv res (flatten #(1)))
(assert (= res [1]))
;; test with None
(setv res (flatten #(1 #(None 3))))
(assert (= res [1 None 3]))
(try (flatten "foo")
(except [e [TypeError]] (assert (in "not a collection" (str e)))))
(try (flatten 12.34)
(except [e [TypeError]] (assert (in "not a collection" (str e))))))
(assert (=
(flatten [1 2 [3 4] 5])
[1 2 3 4 5]))
(assert (=
(flatten ["foo" #(1 2) [1 [2 3] 4] "bar"])
["foo" 1 2 1 2 3 4 "bar"]))
(assert (= (flatten "foo") ["foo"]))
(assert (= (flatten 12.34) [12.34]))
(assert (= (flatten [1]) [1]))
(assert (= (flatten []) []))
(assert (= (flatten #(1)) [1]))
(assert (= (flatten #(1 #(None 3))) [1 None 3]))
(assert (= (flatten {"a" 1 "b" 2}) ["a" "b"])))


(defn test-rest []
Expand Down
Loading