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

Intercalate or concat for [[a]]. #1093

Open
amano-kenji opened this issue Apr 7, 2023 · 4 comments
Open

Intercalate or concat for [[a]]. #1093

amano-kenji opened this issue Apr 7, 2023 · 4 comments

Comments

@amano-kenji
Copy link
Contributor

amano-kenji commented Apr 7, 2023

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)))
@CosmicToast
Copy link
Contributor

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

@amano-kenji
Copy link
Contributor Author

amano-kenji commented Apr 9, 2023

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

@CosmicToast
Copy link
Contributor

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.

@amano-kenji
Copy link
Contributor Author

amano-kenji commented Apr 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants