janet icon indicating copy to clipboard operation
janet copied to clipboard

Intercalate or concat for `[[a]]`.

Open amano-kenji opened this issue 1 year ago • 4 comments

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)))

amano-kenji avatar Apr 07 '23 03:04 amano-kenji

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"])

CosmicToast avatar Apr 08 '23 19:04 CosmicToast

> (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.

amano-kenji avatar Apr 09 '23 06:04 amano-kenji

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.

CosmicToast avatar Apr 10 '23 01:04 CosmicToast

Intercalate and concat unwrap exactly one level of array. Sometimes, you want precision.

amano-kenji avatar Apr 10 '23 01:04 amano-kenji