janet
janet copied to clipboard
Intercalate or concat for `[[a]]`.
https://hackage.haskell.org/package/base/docs/Data-List.html#v:intercalate
intercalate :: [a] -> [[a]] -> [a]
This is equivalent to
(concat (intersperse xs xss))
This is similar to
(mapcat (fn [x] x) (interpose xs xss))
in janet. I would like either intercalate
or concat
that accepts [[a]]
.
Currently, I have this function in my code.
(defn intercalate
"(intercalate xs xss) is (mapcat (fn [x] x) (interpose xs xss))"
[xs xss]
(mapcat (fn [x] x) (interpose xs xss)))
Can you give a usage example?
For instance, why do you use mapcat
and not array/concat
?
With (array/concat (interpose xs xss))
I get the same output on the example from haskell (", "
and ["Lorem" "ipsum" "dolor"]
)
> (array/concat (interpose [""] [["1" "2" "3"] ["4" "5" "6"]]))
@[("1" "2" "3") ("") ("4" "5" "6")]
> (mapcat (fn [x] x) (interpose [""] [["1" "2" "3"] ["4" "5" "6"]]))
@["1" "2" "3" "" "4" "5" "6"]
They are not the same. Haskell's String
type is actually [Char]
. Janet's string type is not [Char]
. That's a crucial difference.
In this case, flatten
in place of array/concat
seems to do the trick.
Or do you want exactly one level of [[a]]
?
This does seem rather specific.
Intercalate and concat unwrap exactly one level of array. Sometimes, you want precision.