-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
stdlib: add List.intersperse and List.intercalate #9962
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some general comments. I don't have a strong opinion on whether to include this or not in the stdlib
; let's wait for other developers to chime in.
stdlib/list.ml
Outdated
@@ -87,6 +87,10 @@ let rec flatten = function | |||
|
|||
let concat = flatten | |||
|
|||
let rec intersperse sep = function | |||
| ([] | [ _ ]) as l -> l | |||
| x :: (_ :: _ as xs) -> x :: sep :: intersperse sep xs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better to make the function tail-recursive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 🙂
stdlib/list.mli
Outdated
@@ -128,6 +128,13 @@ val flatten : 'a list list -> 'a list | |||
(length of the argument + length of the longest sub-list). | |||
*) | |||
|
|||
val intersperse : 'a -> 'a list -> 'a list | |||
(** [intersperse sep l] is the list formed by inserting [sep] between adjacent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not go further and allow the separator to be a 'a list
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 'a -> 'a list -> 'a list
form has a more descriptive type and I think more intuitively matches the name "intersperse".
Anecdotally, I've never found I needed the more general form either. YMMV.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'm not sure I had that much of a need for intersperse
(but I don't mind). I rather often missed the signature of String.concat
for List
(which would be 'a list -> 'a list list -> 'a list
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dbuenzli: Interesting. I've never found a need for that one, but I know Haskell developers like it, and they call it intercalate
. I'm happy to add that along with this PR if it's deemed worthy; it's in some sense the natural sibling to intersperse
.
42442c4
to
04f9a8e
Compare
a946fde
to
1022881
Compare
Rebased and added |
1022881
to
5d7c4cd
Compare
You should investigate the CI failures. I don't have a strong opinion on adding these functions. Maybe @gasche or @alainfrisch would like to comment? |
9e8655c
to
501710c
Compare
AppVeyor is now green. A maintainer will need to explicitly approve triggering the other CIs (thanks to GitHub's new security policy). |
09adbf7
to
24e765f
Compare
Still missing that fun. |
What's the status of this issue? I need |
The status is that the PR is stalled. Judging from the discussion, there wasn't a lot of support for the addition, nor was there strong opposition. There was some related discussion in #10583 (comment). Again judging from the discussion, there does not seem to be a lot of consensus about which signaturess are the most useful in practice. In any case, I think any addition in this direction should be done in both |
@nojb, I can make another PR that borrows code from this PR and introduces the same functions for |
No opposition in principle. |
Closed as I don't have the bandwidth to un-stall this at the moment. Best of luck with the follow-up PR @Hirrolot :-) |
Adds a new function –
List.intersperse : 'a -> 'a list -> 'a list
– to the standard library. This transforms a listl
by adding a separating element between each adjacent pair of elements inl
. For example:I re-define this function reasonably often, and it's slightly annoying to do so due to the asymmetry of the empty list case. Example use-cases I've had in the past:
.csv
/.tsv
fields;Notes:
Other standard libraries that include this function: Jane Street base, Haskell base and Elm core.
If
intersperse
is deemed useful, I think there's a strong argument for (at least)Seq.intersperse
as well. I'm happy to add that to this PR.