Proposal: add lists:intersperse/2 and lists:intercalate/2

classic Classic list List threaded Threaded
45 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Proposal: add lists:intersperse/2 and lists:intercalate/2

Jesper Louis Andersen-2
Hi Erlangers,

I'd really like to add two functions to the lists module from Haskell:

intersperse(List, Seperator) produces a list where each element is separated by separator, i.e.

X = [1,2,3]
[1, x, 2, x, 3] = lists:intersperse(X, x),

and it's cousin, intercalate(ListOfLists, Separator) is append(intersperse(ListOfLists, Seperator)), i.e,

Y = ["a", "b", "c"]
"a, b, c" = lists:intercalate(Y, ", "),

The implementations are straightforward and easy to write tests for, even property based tests if needed.

The rationale for this proposal is that I find myself implementing this function again and again in every project I write, and it is highly generic. It belongs in a typical list module. OCaml libraries add it. Haskell's Data.List has it. I believe Erlang, being a practical language, should have it as well.

Thoughts?

--
J.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Pierre Fenoll-2
I find myself reimplementing the first one a lot, for use with iolists.
The second one is string:join/2, right?


> On 02 Mar 2016, at 06:47, Jesper Louis Andersen <[hidden email]> wrote:
>
> Hi Erlangers,
>
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator) produces a list where each element is separated by separator, i.e.
>
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
>
> and it's cousin, intercalate(ListOfLists, Separator) is append(intersperse(ListOfLists, Seperator)), i.e,
>
> Y = ["a", "b", "c"]
> "a, b, c" = lists:intercalate(Y, ", "),
>
> The implementations are straightforward and easy to write tests for, even property based tests if needed.
>
> The rationale for this proposal is that I find myself implementing this function again and again in every project I write, and it is highly generic. It belongs in a typical list module. OCaml libraries add it. Haskell's Data.List has it. I believe Erlang, being a practical language, should have it as well.
>
> Thoughts?
>
> --
> J.
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Robert Wilkinson
In reply to this post by Jesper Louis Andersen-2
On Wed, Mar 02, 2016 at 03:47:41PM +0100, Jesper Louis Andersen wrote:

> Hi Erlangers,
>
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator) produces a list where each element is
> separated by separator, i.e.
>
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
>
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)), i.e,
>
> Thoughts?
>
> --
> J.

Hi Jesper

Please spell Separator consistently and correctly :-)

Bob
--
Kiss me, Kate, we will be married o' Sunday.
                -- William Shakespeare, "The Taming of the Shrew"
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Garrett Smith-5
In reply to this post by Pierre Fenoll-2
I recently implemented the second as "join" - ala string, but for arbitrary separators and list elements.

I think the name "intersperse" is descriptive enough.

"intercalculate" no so much. I'd go with "join".

In the interest of consistency with other lists functions, I'd put the list argument last in each case.

On Wed, Mar 2, 2016 at 8:54 AM Pierre Fenoll <[hidden email]> wrote:
I find myself reimplementing the first one a lot, for use with iolists.
The second one is string:join/2, right?


> On 02 Mar 2016, at 06:47, Jesper Louis Andersen <[hidden email]> wrote:
>
> Hi Erlangers,
>
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator) produces a list where each element is separated by separator, i.e.
>
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
>
> and it's cousin, intercalate(ListOfLists, Separator) is append(intersperse(ListOfLists, Seperator)), i.e,
>
> Y = ["a", "b", "c"]
> "a, b, c" = lists:intercalate(Y, ", "),
>
> The implementations are straightforward and easy to write tests for, even property based tests if needed.
>
> The rationale for this proposal is that I find myself implementing this function again and again in every project I write, and it is highly generic. It belongs in a typical list module. OCaml libraries add it. Haskell's Data.List has it. I believe Erlang, being a practical language, should have it as well.
>
> Thoughts?
>
> --
> J.
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Siraaj Khandkar-3
In reply to this post by Jesper Louis Andersen-2
On 3/2/16 9:47 AM, Jesper Louis Andersen wrote:

> Hi Erlangers,
>
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator) produces a list where each element is
> separated by separator, i.e.
>
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
>
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)), i.e,
>
> Y = ["a", "b", "c"]
> "a, b, c" = lists:intercalate(Y, ", "),
>
> The implementations are straightforward and easy to write tests for, even
> property based tests if needed.
>
> The rationale for this proposal is that I find myself implementing this
> function again and again in every project I write, and it is highly
> generic. It belongs in a typical list module. OCaml libraries add it.
> Haskell's Data.List has it. I believe Erlang, being a practical language,
> should have it as well.
>
> Thoughts?

+1

Though I prefer the name "interleave" to "intersperse", since its
meaning is more-precise and closer to the intended behavior here.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

dmkolesnikov
Hello,

“interleave” and “join” are friendly for non-native English programmers.  

Otherwise +1

- Dmitry

> On Mar 2, 2016, at 7:02 PM, Siraaj Khandkar <[hidden email]> wrote:
>
> On 3/2/16 9:47 AM, Jesper Louis Andersen wrote:
>> Hi Erlangers,
>>
>> I'd really like to add two functions to the lists module from Haskell:
>>
>> intersperse(List, Seperator) produces a list where each element is
>> separated by separator, i.e.
>>
>> X = [1,2,3]
>> [1, x, 2, x, 3] = lists:intersperse(X, x),
>>
>> and it's cousin, intercalate(ListOfLists, Separator) is
>> append(intersperse(ListOfLists, Seperator)), i.e,
>>
>> Y = ["a", "b", "c"]
>> "a, b, c" = lists:intercalate(Y, ", "),
>>
>> The implementations are straightforward and easy to write tests for, even
>> property based tests if needed.
>>
>> The rationale for this proposal is that I find myself implementing this
>> function again and again in every project I write, and it is highly
>> generic. It belongs in a typical list module. OCaml libraries add it.
>> Haskell's Data.List has it. I believe Erlang, being a practical language,
>> should have it as well.
>>
>> Thoughts?
>
> +1
>
> Though I prefer the name "interleave" to "intersperse", since its meaning is more-precise and closer to the intended behavior here.
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Joe Armstrong-2
In reply to this post by Jesper Louis Andersen-2
On Wed, Mar 2, 2016 at 3:47 PM, Jesper Louis Andersen
<[hidden email]> wrote:

> Hi Erlangers,
>
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator) produces a list where each element is separated
> by separator, i.e.
>
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
>
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)), i.e,
>
> Y = ["a", "b", "c"]
> "a, b, c" = lists:intercalate(Y, ", "),
>
> The implementations are straightforward and easy to write tests for, even
> property based tests if needed.
>
> The rationale for this proposal is that I find myself implementing this
> function again and again in every project I write, and it is highly generic.
> It belongs in a typical list module. OCaml libraries add it. Haskell's
> Data.List has it. I believe Erlang, being a practical language, should have
> it as well.
>

This is a splendid function - I have one myself called interleave, the only
problem is that if somebody uses this and puts their code in a library on github
and somebody fetches this code and runs it with an *old* version of erlang
then they will get a error and probably blame your code.

This is why I have a single library called elib2_misc.erl where I put *all* the
stuff that should be somewhere else - then I ship this with my code.

The whole story of dependency analysis seems to be be in a very sad state.

I use the 'keep your fingers crossed and hope it will work' method.

Years ago I wanted to add a -needs(Vsn) annotation to module

For example:

     -needs(erl18).

Means would mean you need version 18 of erlang.

That way if you added a needs annotation to the code that used the updated lists
then an earlier version of Erlang could give a meaningfull diagnostic
and not just
crash when the encountering a function 'from the future'.

Actually I had another problem today - a program I wrote in 2003 did not work
with the latest and greatest Erlang - not because the language had changed
but because the library functions had been renamed and moved around.

I guess a lint program would be useful here. It should be fairly
doable to download
all old erlangs and simple make lists of all the function names in all modules
and do a bit of consistency checking.

Cheers

/Joe




> Thoughts?
>
> --
> J.
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Jesse Gumm-2
I'd like to go ahead and throw in a big fat "same".  I've also
reimplemented the wheel in Nitrogen with a `join` function that
interleaves a list of things with some other things. So cast my vote
for a lists:join. `join` I would argue is much more accessible, as 1)
it's quite common in other languages, 2) stays remains consistent with
string:join and filename:join, and 3) brevity is awesome.

-Jesse

On Wed, Mar 2, 2016 at 12:28 PM, Joe Armstrong <[hidden email]> wrote:

> On Wed, Mar 2, 2016 at 3:47 PM, Jesper Louis Andersen
> <[hidden email]> wrote:
>> Hi Erlangers,
>>
>> I'd really like to add two functions to the lists module from Haskell:
>>
>> intersperse(List, Seperator) produces a list where each element is separated
>> by separator, i.e.
>>
>> X = [1,2,3]
>> [1, x, 2, x, 3] = lists:intersperse(X, x),
>>
>> and it's cousin, intercalate(ListOfLists, Separator) is
>> append(intersperse(ListOfLists, Seperator)), i.e,
>>
>> Y = ["a", "b", "c"]
>> "a, b, c" = lists:intercalate(Y, ", "),
>>
>> The implementations are straightforward and easy to write tests for, even
>> property based tests if needed.
>>
>> The rationale for this proposal is that I find myself implementing this
>> function again and again in every project I write, and it is highly generic.
>> It belongs in a typical list module. OCaml libraries add it. Haskell's
>> Data.List has it. I believe Erlang, being a practical language, should have
>> it as well.
>>
>
> This is a splendid function - I have one myself called interleave, the only
> problem is that if somebody uses this and puts their code in a library on github
> and somebody fetches this code and runs it with an *old* version of erlang
> then they will get a error and probably blame your code.
>
> This is why I have a single library called elib2_misc.erl where I put *all* the
> stuff that should be somewhere else - then I ship this with my code.
>
> The whole story of dependency analysis seems to be be in a very sad state.
>
> I use the 'keep your fingers crossed and hope it will work' method.
>
> Years ago I wanted to add a -needs(Vsn) annotation to module
>
> For example:
>
>      -needs(erl18).
>
> Means would mean you need version 18 of erlang.
>
> That way if you added a needs annotation to the code that used the updated lists
> then an earlier version of Erlang could give a meaningfull diagnostic
> and not just
> crash when the encountering a function 'from the future'.
>
> Actually I had another problem today - a program I wrote in 2003 did not work
> with the latest and greatest Erlang - not because the language had changed
> but because the library functions had been renamed and moved around.
>
> I guess a lint program would be useful here. It should be fairly
> doable to download
> all old erlangs and simple make lists of all the function names in all modules
> and do a bit of consistency checking.
>
> Cheers
>
> /Joe
>
>
>
>
>> Thoughts?
>>
>> --
>> J.
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions



--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Vlad Dumitrescu-2
In reply to this post by Joe Armstrong-2

On Wed, Mar 2, 2016 at 7:28 PM, Joe Armstrong <[hidden email]> wrote:
Years ago I wanted to add a -needs(Vsn) annotation to module

For example:

     -needs(erl18).

Means would mean you need version 18 of erlang.

That way if you added a needs annotation to the code that used the updated lists
then an earlier version of Erlang could give a meaningfull diagnostic
and not just
crash when the encountering a function 'from the future'.

Actually I had another problem today - a program I wrote in 2003 did not work
with the latest and greatest Erlang - not because the language had changed
but because the library functions had been renamed and moved around.

I guess a lint program would be useful here. It should be fairly
doable to download
all old erlangs and simple make lists of all the function names in all modules
and do a bit of consistency checking.

One such tool is geas https://github.com/crownedgrouse/geas

regards,
Vlad


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

PAILLEAU Eric
In reply to this post by Joe Armstrong-2

Hi Joe,

I did it in crownedgrouse/geas_devel database either in term and yaml format starting R15.
And as Vlad said, my project geas does this work on project and dependancies,  compiled or not. Integrated in rebar and erlang.mk.
Enjoy.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Richard A. O'Keefe-2
In reply to this post by Jesper Louis Andersen-2
On 3/03/16 3:47 am, Jesper Louis Andersen wrote:
> I'd really like to add two functions to the lists module from Haskell:
>
> intersperse(List, Seperator)
Why have you swapped the arguments?  The Haskellfunction
    Data.List.intersperse :: a -> [a] -> [a]
puts the separator *first*.

 > produces a list where each element is separated by separator,

You can't separate one thing.  The elements *are* separated by the
separator.
>
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)),
Once again, why have you switched the arguments?

Data.List.intercalate :: [a] -> [[a]] -> [a]

puts the separator *first*.

I've never really been happy with the name "intercalate".
To the extent that it's acceptableto use "intercalate"
for anything other than adding chunks to the calendar in
order to fix itup, it means what the intersperse function
does.  However, the name _is_ established inHaskell, and
there's no point in gratuitous difference.

Which is why it is important to get the argument order right.

intersperse and intercalate are very nearly the same function.

intersperse sep xs = interwhatsit (:)  [] (sep:)  xs
intercalate sep xs = interwhatsit (++) [] (sep++) xs

interwhatsit :: (a -> b -> b) -> b -> (b -> b) -> [a] -> b
--              Combine          End Separate Items

interwhatsit _ e _ []     = e
interwhatsit c e s (x:xs) = loop xs x
   where loop []     x = c x e
         loop (y:ys) x = c x $ s $ loop ys y

Do we want to have the special cases without the general case?
(There has to be a better name than interwhatsit.  sepfoldr?)
>
> The rationale for this proposal is that I find myself implementing
> this function again and again in every project I write

Why would you do that rather than putting it in a utilities module and
just reusing that module?
> , and it is highly generic.
That sounds as though you have some other use in mind for these
functions than pasting strings together.  I'd love to know what.
I've been using Haskell since before it had intersperse and
intercalate, and can't remember ever using them.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Richard A. O'Keefe-2
In reply to this post by Siraaj Khandkar-3


On 3/03/16 6:02 am, Siraaj Khandkar wrote:
> Though I prefer the name "interleave" to "intersperse", since its
> meaning is more-precise and closer to the intended behavior here.

No, interleaving requires *two* sequences.  This is interleave:

interleave([], []) -> [];
interleave([X|Xs], [Y|Ys]) -> [X,Y|interleave(Xs,Ys)].

interleave, v, OED, sense 3:
"Computing and Telecommunications. To interfile (two or more
digitized signals or sequences of information) by alternating
between them; to alternate (one such signal or sequence) with
others. Also, to divide (memory, etc.) between a number of
different tasks by allocating successive segments to each in turn."

(I must admit I hadn't encountered the word 'interfile' before.)

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Siraaj Khandkar-3
On 3/2/16 7:41 PM, Richard A. O'Keefe wrote:

>
>
> On 3/03/16 6:02 am, Siraaj Khandkar wrote:
>> Though I prefer the name "interleave" to "intersperse", since its
>> meaning is more-precise and closer to the intended behavior here.
>
> No, interleaving requires *two* sequences.  This is interleave:
>
> interleave([], []) -> [];
> interleave([X|Xs], [Y|Ys]) -> [X,Y|interleave(Xs,Ys)].
>
> interleave, v, OED, sense 3:
> "Computing and Telecommunications. To interfile (two or more
> digitized signals or sequences of information) by alternating
> between them; to alternate (one such signal or sequence) with
> others. Also, to divide (memory, etc.) between a number of
> different tasks by allocating successive segments to each in turn."
>
> (I must admit I hadn't encountered the word 'interfile' before.)

The way I understand it is:

- interleave: put 1 B between 2 As
- intersperse: put M Bs between N As

i.e. "intersperse" is more-general - interval length is unspecified.

Is this wrong?
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Michael Truog
In reply to this post by Joe Armstrong-2
On 03/02/2016 10:28 AM, Joe Armstrong wrote:

> On Wed, Mar 2, 2016 at 3:47 PM, Jesper Louis Andersen
> <[hidden email]> wrote:
>> Hi Erlangers,
>>
>> I'd really like to add two functions to the lists module from Haskell:
>>
>> intersperse(List, Seperator) produces a list where each element is separated
>> by separator, i.e.
>>
>> X = [1,2,3]
>> [1, x, 2, x, 3] = lists:intersperse(X, x),
>>
>> and it's cousin, intercalate(ListOfLists, Separator) is
>> append(intersperse(ListOfLists, Seperator)), i.e,
>>
>> Y = ["a", "b", "c"]
>> "a, b, c" = lists:intercalate(Y, ", "),
>>
>> The implementations are straightforward and easy to write tests for, even
>> property based tests if needed.
>>
>> The rationale for this proposal is that I find myself implementing this
>> function again and again in every project I write, and it is highly generic.
>> It belongs in a typical list module. OCaml libraries add it. Haskell's
>> Data.List has it. I believe Erlang, being a practical language, should have
>> it as well.
>>
> This is a splendid function - I have one myself called interleave, the only
> problem is that if somebody uses this and puts their code in a library on github
> and somebody fetches this code and runs it with an *old* version of erlang
> then they will get a error and probably blame your code.
>
> This is why I have a single library called elib2_misc.erl where I put *all* the
> stuff that should be somewhere else - then I ship this with my code.
>
> The whole story of dependency analysis seems to be be in a very sad state.
>
> I use the 'keep your fingers crossed and hope it will work' method.
>
> Years ago I wanted to add a -needs(Vsn) annotation to module
>
> For example:
>
>       -needs(erl18).
>
> Means would mean you need version 18 of erlang.
>
> That way if you added a needs annotation to the code that used the updated lists
> then an earlier version of Erlang could give a meaningfull diagnostic
> and not just
> crash when the encountering a function 'from the future'.
>
> Actually I had another problem today - a program I wrote in 2003 did not work
> with the latest and greatest Erlang - not because the language had changed
> but because the library functions had been renamed and moved around.

https://github.com/erlang/eep/blob/master/eeps/eep-0044.md#the-otp_release-macro
combined with
https://github.com/erlang/eep/blob/master/eeps/eep-0044.md#the--error-directive should
do what you want, though it is more verbose and flexible.

> I guess a lint program would be useful here. It should be fairly
> doable to download
> all old erlangs and simple make lists of all the function names in all modules
> and do a bit of consistency checking.

Dialyzer seems like the Erlang lint program.  If the Erlang install automatically generated a
plt file for it, it might be a bit easier to use it as part of normal compilation.

The alternative
might be to use http://erlang.org/doc/man/xref.html as a command line tool, if the checking
needs to only consider functions and not types.  I am not aware of a standalone (escript)
to run xref, but that may be useful for normal Erlang development (aside from rebar or
other build tool usage).

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Richard A. O'Keefe-2
In reply to this post by Siraaj Khandkar-3


On 3/03/16 2:23 pm, Siraaj Khandkar wrote:

>
>> interleave, v, OED, sense 3:
>> "Computing and Telecommunications. To interfile (two or more
>> digitized signals or sequences of information) by alternating
>> between them; to alternate (one such signal or sequence) with
>> others. Also, to divide (memory, etc.) between a number of
>> different tasks by allocating successive segments to each in turn."
>>
>> (I must admit I hadn't encountered the word 'interfile' before.)
>
> The way I understand it is:
>
> - interleave: put 1 B between 2 As
[A1,B1,A2,B2,A3,B3] will do.
[A1,B1,A2,B2,A3,B3,A4] will do.
Using the *same* B every time is not interleaving.

A simple example of interleaving: put the fingers of one hand between
the fingers of the other.

Please people, do NOT use the word "interleaving" to refer to what
the proposed "intersperse" function does.  We need "interleaving"
to refer to interleaving.
> - intersperse: put M Bs between N As
OED sense 1: "to scatter or sprinkle between or among other things;
to place here and there in the course of something; to mingle
dispersedly or at intervals."

I don't think anyone looking at the OED would expect the Haskell
function definition.  The only justification for adopting the names
intersperse and intercalate is that those *are* the names other
not-entirely-dissimilar programming languages are using.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Loïc Hoguin-3
In reply to this post by Michael Truog
On 03/03/2016 02:25 AM, Michael Truog wrote:
> The alternative
> might be to use http://erlang.org/doc/man/xref.html as a command line
> tool, if the checking
> needs to only consider functions and not types.  I am not aware of a
> standalone (escript)
> to run xref, but that may be useful for normal Erlang development (aside
> from rebar or
> other build tool usage).

https://github.com/inaka/xref_runner

--
Loïc Hoguin
http://ninenines.eu
Author of The Erlanger Playbook,
A book about software development using Erlang
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Siraaj Khandkar-3
In reply to this post by Richard A. O'Keefe-2
On 3/2/16 11:31 PM, Richard A. O'Keefe wrote:

>
> On 3/03/16 2:23 pm, Siraaj Khandkar wrote:
>>
>>> interleave, v, OED, sense 3:
>>> "Computing and Telecommunications. To interfile (two or more
>>> digitized signals or sequences of information) by alternating
>>> between them; to alternate (one such signal or sequence) with
>>> others. Also, to divide (memory, etc.) between a number of
>>> different tasks by allocating successive segments to each in turn."
>>>
>>> (I must admit I hadn't encountered the word 'interfile' before.)
>>
>> The way I understand it is:
>>
>> - interleave: put 1 B between 2 As
> [A1,B1,A2,B2,A3,B3] will do.
> [A1,B1,A2,B2,A3,B3,A4] will do.
> Using the *same* B every time is not interleaving.
>

The notion of sameness here is problematic, since you're now requiring
that the above-quoted "sequences of information" are sets, which is
unlikely the intended meaning.

If we have no uniqueness or distribution (i.e. how long until a repeat
is allowed) requirements for our two sequences, then there's no useful,
semantic distinction between a sequence of copied elements and a
sequence of randomly-generated elements.

Neither "interleaving" nor "interspersing" say anything about the
contents of the two sequences they operate on, where they differ is in
the degree to which they care about intervals between elements:

- "interleaving" promises a process which picks 1 element from A, then 1
element from B;
- "interspersing" only promises that some (undefined number of) elements
from A will end-up separated by some (undefined number of) elements from B.

One real problem, which goes against the requirement of the function
in-question, is that both, [a, b, a] and [a, b, a, b] seem to be allowed
by "interleave", but maybe not by "intersperse", but I'm not really sure
about this...


>> - intersperse: put M Bs between N As
> OED sense 1: "to scatter or sprinkle between or among other things;
> to place here and there in the course of something; to mingle
> dispersedly or at intervals."
>
> I don't think anyone looking at the OED would expect the Haskell
> function definition. The only justification for adopting the names
> intersperse and intercalate is that those *are* the names other
> not-entirely-dissimilar programming languages are using.
>

Just merely copying what has been done before, without looking into the
reasons, misses a valuable opportunity for improvement.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Jesper Louis Andersen-2
In reply to this post by Jesper Louis Andersen-2
So to catch up:

* I need to learn how to spell separator :)
* Indeed, the argument order should be intersperse(Sep, Xs), not the other way around. This looks consistent with most List functions.

* On Joe's comments of why not simply a utility library:

This would have been fine, were it not for the fact that Erlang is miserably bad at handling ad-hoc utility libraries for other modules. There is no way I can hide joe_utils.erl since it is a globally known name. This creates lots of versioning problems down the road. In e.g., Standard ML or OCaml, I could just package the modules such that 'module JoeUtils' is not exported outside the scope of the individual libraries and applications. But Erlang is not that language.

I also think intersperse/intercalate are so often used they shouldn't be in everyones utils library but should be put in the stdlib base.

* On Richard's comments of other uses than for a string:

The obvious use in Erlang is to produce iolist() types for binary data: intersperse($, [<<"a">>, <<"b">>, <<"c">>]) is relatively common in my code. The slightly less obvious use is to interleave a simplifier step in between every optimization step of a compiler, but I've only done that once in my life and I'm not sure it is that useful :)

One could argue we should just have tooling such as Hughes-combinators[0] for output rather than the singular intersperse function, but it is by the far the missing tool in the toolbox when trying to use standard Erlang as were it a pretty-printing combinator library.

* On the name choice of 'intersperse/intercalate':

I think the semantics I'm aiming for is so close to the OCaml(Core)/Haskell implementations I'm willing to reuse that name. Another valid name is 'join' which is common in many other languages. I don't think one is better than the other, but the FP roots in me says we should adopt FP-language naming.

NEXT STEP:

I'd like to produce the necessary PR for 'intersperse' first, which gives me an idea of where to put changes. This builds a small unit of work which should be easy to make concrete. We can always augment this with additional functions later.' We need at least:

* The implementation in lists.erl (I don't think we need to make it fast right away, so plain Erlang it is)
* Documentation of the function, with examples
* Tests with good coverage of the function.

Once this is done, adding 'intercalate' is easy, but the question is if it is needed since once can just 'append' after having interspersed. The power of having a special function is that it is amenable to certain optimizations if they are kept as a larger block.

[0] The thing I have in mind here is John Hughes "The Design of a Pretty-printing library" http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.8777



On Wed, Mar 2, 2016 at 3:47 PM, Jesper Louis Andersen <[hidden email]> wrote:
Hi Erlangers,

I'd really like to add two functions to the lists module from Haskell:

intersperse(List, Seperator) produces a list where each element is separated by separator, i.e.

X = [1,2,3]
[1, x, 2, x, 3] = lists:intersperse(X, x),

and it's cousin, intercalate(ListOfLists, Separator) is append(intersperse(ListOfLists, Seperator)), i.e,

Y = ["a", "b", "c"]
"a, b, c" = lists:intercalate(Y, ", "),

The implementations are straightforward and easy to write tests for, even property based tests if needed.

The rationale for this proposal is that I find myself implementing this function again and again in every project I write, and it is highly generic. It belongs in a typical list module. OCaml libraries add it. Haskell's Data.List has it. I believe Erlang, being a practical language, should have it as well.

Thoughts?

--
J.



--
J.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Garrett Smith-5
On Sat, Mar 5, 2016 at 4:04 AM Jesper Louis Andersen <[hidden email]> wrote:
So to catch up:

* On Richard's comments of other uses than for a string:

The obvious use in Erlang is to produce iolist() types for binary data: intersperse($, [<<"a">>, <<"b">>, <<"c">>]) is relatively common in my code. The slightly less obvious use is to interleave a simplifier step in between every optimization step of a compiler, but I've only done that once in my life and I'm not sure it is that useful :)

Yep, iolists is the application I have for this.
 
* On the name choice of 'intersperse/intercalate':

I think the semantics I'm aiming for is so close to the OCaml(Core)/Haskell implementations I'm willing to reuse that name. Another valid name is 'join' which is common in many other languages. I don't think one is better than the other, but the FP roots in me says we should adopt FP-language naming.

In looking over the naming conventions in Erlang, one might assume that the design pattern is for diversity rather than uniformity. That's a nice way to look at it I think.

Personally I'd vote to converge on some standards rather than diverge - and taking cues from Erlang, and not the many many other languages, will help there. The clear analog is string:join. It's also short - saves valuable typing cycles!


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Björn-Egil Dahlberg-2
I think those functions are nice things to add to the lists module. No reason to hide them in some weird extra lib module.

The names though. Naming isn't easy. I actually had to look up the word "intercalate" and now I know it roughly means "insert". Perhaps it really best describes what the functions does but .. can't we find anything shorter?

2016-03-05 15:22 GMT+01:00 Garrett Smith <[hidden email]>:
On Sat, Mar 5, 2016 at 4:04 AM Jesper Louis Andersen <[hidden email]> wrote:
So to catch up:

* On Richard's comments of other uses than for a string:

The obvious use in Erlang is to produce iolist() types for binary data: intersperse($, [<<"a">>, <<"b">>, <<"c">>]) is relatively common in my code. The slightly less obvious use is to interleave a simplifier step in between every optimization step of a compiler, but I've only done that once in my life and I'm not sure it is that useful :)

Yep, iolists is the application I have for this.
 
* On the name choice of 'intersperse/intercalate':

I think the semantics I'm aiming for is so close to the OCaml(Core)/Haskell implementations I'm willing to reuse that name. Another valid name is 'join' which is common in many other languages. I don't think one is better than the other, but the FP roots in me says we should adopt FP-language naming.

In looking over the naming conventions in Erlang, one might assume that the design pattern is for diversity rather than uniformity. That's a nice way to look at it I think.

Personally I'd vote to converge on some standards rather than diverge - and taking cues from Erlang, and not the many many other languages, will help there. The clear analog is string:join. It's also short - saves valuable typing cycles!


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
123