Open
Description
into
is simultaneously under-spec'd and over-spec'd right now due to some weird edge cases that come with the function. Officially, the documentation notes that, with two arguments, into
should take two collections, but there are several cases where this is not true.
- If
to
is any value or expression, andfrom
is an empty collection,(into to from)
returns evaluation ofto
.
user=> (into :a [])
:a
babel.middleware=> (into :a [])
The first argument of (into :a []) was expected to be a sequence but is a keyword :a instead.
- If
from
is a non-empty collection,(into nil from)
performs conj on(list (first from))
and the rest of the list, regardless of what the type of collectionfrom
is.
user=> (into nil [:a :b])
(:b :a)
Same result in Babel.
- If
to
is a string, andfrom
is a non-empty string,(into to from)
throws an exception.
user=> (into "" "test")
Execution error (ClassCastException) at user/eval1579 (REPL:1).
class java.lang.String cannot be cast to class clojure.lang.IPersistentCollection (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.IPersistentCollection is in unnamed module of loader 'bootstrap')
Correctly fails Babel spec, though might need to be improved, since the message is wrong (see below):
babel.middleware=> (into "" "test")
The first argument of (into "" "test") was expected to be a sequence but is a string "" instead.
- Note that this still works if
from
is an empty string:
user=> (into "a" "")
"a"
babel.middleware=> (into "a" "")
The first argument of (into "a" "") was expected to be a sequence but is a string "a" instead.
- Note also that Babel specs incorrectly pass if
to
is a collection andfrom
is not. In these cases, we get an error onseq
instead (which is also a problem. See: Need spec onseq
#63).
babel.middleware=> (into [1 2 3] 4)
Don't know how to create a sequence from a number.