Skip to content

Commit

Permalink
Merge pull request #108 from Kodiologist/research-flat-earth
Browse files Browse the repository at this point in the history
Edit `flatten`
  • Loading branch information
Kodiologist authored Dec 18, 2024
2 parents 9e408fd + 6988e4a commit ffbd27d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
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

0 comments on commit ffbd27d

Please sign in to comment.