Array.prototype.joinWith(iterable)

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Aug 16 10:08:17 UTC 2019


given an array, it joins it through the values of the iterable argument,
without ever resulting to undefined

['a', 'b', 'c'].joinWith(['-']) would produce "a-b-c"

['a', 'b', 'c'].joinWith([1, 2]) would produce "a1b2c"

['a', 'b', 'c'].joinWith('012') would produce "a0b1c"
note the string, as iterable, is acceptable too

const tag = (template, ...values) => template.joinWith(values);
tag`a${Math.random()}b${Math.random()}`; would fill the gap between a and
b, or b and c, with the value returned by the two Math.random()

['a', 'b', 'c', 'd'].joinWith('01'); would produce "a0b1c0d" so that
there's never an `undefined









On Fri, Aug 16, 2019 at 12:01 PM Naveen Chawla <naveen.chwl at gmail.com>
wrote:

> I'm just not seeing what it's supposed to do. If you could give a brief
> explanation of the array method, and the string method then of course I
> would get it. I know it would seem obvious to you from the examples alone,
> it's just not to me.
>
> On Fri, 16 Aug 2019 at 08:32, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> Just to re-state: zip from lowdash, does **not** do what my proposed
>> method does ... anything that won't produce the following result is not
>> what I'm proposing
>>
>> console.log(['a', 'b', 'c', 'd'].joinWith([1, 2]));
>> // a1b2c1d
>>
>> function tag2str(template, ...values) {
>>   return template.joinWith(values);
>> }
>>
>> tag2str`a${1}b${2}c`;
>> // "a1b2c"
>>
>> On Fri, Aug 16, 2019 at 5:57 AM Isiah Meadows <isiahmeadows at gmail.com>
>> wrote:
>>
>>> For that, I'd rather see an `interleave` that just rotates through all
>>> its arguments. It'd be basically sugar for `.zip().flat()`, but an
>>> implementation could optimize the heck out of it. (In particular, they
>>> could iterate through them one-by-one and only allocate once, not in
>>> the hot loop, so it'd be fast.)
>>>
>>> I at one point had it in my list of wishlist proposals, but it somehow
>>> disappeared. I've since recreated it:
>>>
>>> https://github.com/isiahmeadows/es-stdlib-proposals/blob/master/proposals/array/interleave.md
>>>
>>> -----
>>>
>>> Isiah Meadows
>>> contact at isiahmeadows.com
>>> www.isiahmeadows.com
>>>
>>>
>>> On Thu, Aug 15, 2019 at 1:12 PM Andrea Giammarchi
>>> <andrea.giammarchi at gmail.com> wrote:
>>> >
>>> > That;s not useful for template literals tags though
>>> >
>>> > _.zip(['a', 'b', 'c'], [1, 2]);
>>> > [["a", 1], ["b", 2], ["c", undefined]]
>>> >
>>> > it basically does nothing I've proposed ... any other name suggestion?
>>> >
>>> > On Thu, Aug 15, 2019 at 3:40 PM Michał Wadas <michalwadas at gmail.com>
>>> wrote:
>>> >>
>>> >> https://lodash.com/docs/#zip
>>> >> https://docs.python.org/3/library/functions.html#zip
>>> >>
>>> >> On Thu, 15 Aug 2019, 15:34 Andrea Giammarchi, <
>>> andrea.giammarchi at gmail.com> wrote:
>>> >>>
>>> >>> the suggested name is just ... suggested, I don't have strong
>>> opinion on it, it just `join` values through other values
>>> >>> what's `Array.zip` ? I've no idea
>>> >>>
>>> >>>
>>> >>> On Thu, Aug 15, 2019 at 12:53 PM Michał Wadas <michalwadas at gmail.com>
>>> wrote:
>>> >>>>
>>> >>>> I would rather see Array.zip, it covers this use case.
>>> >>>>
>>> >>>> On Thu, 15 Aug 2019, 10:50 Andrea Giammarchi, <
>>> andrea.giammarchi at gmail.com> wrote:
>>> >>>>>
>>> >>>>>
>>> >>>>> I wonder if there's any interest in adding another handy Array
>>> method as joinWith could be:
>>> >>>>>
>>> >>>>> ```js
>>> >>>>> // proposal example
>>> >>>>> Array.prototype.joinWith = function (values) {
>>> >>>>>   const {length} = this;
>>> >>>>>   if (length < 2)
>>> >>>>>     return this.join('');
>>> >>>>>   const out = [this[0]];
>>> >>>>>   const len = values.length;
>>> >>>>>   for (let i = 1; i < length; i++) {
>>> >>>>>     console.log(i, len);
>>> >>>>>     out.push(values[(i - 1) % len], this[i]);
>>> >>>>>   }
>>> >>>>>   return out.join('');
>>> >>>>> };
>>> >>>>> ```
>>> >>>>>
>>> >>>>> The goal is to simplify joining array entries through not the same
>>> value, example:
>>> >>>>>
>>> >>>>> ```js
>>> >>>>> console.log(['a', 'b', 'c', 'd'].joinWith([1, 2]));
>>> >>>>> // a1b2c1d
>>> >>>>>
>>> >>>>> function tag2str(template, ...values) {
>>> >>>>>   return template.joinWith(values);
>>> >>>>> }
>>> >>>>>
>>> >>>>> tag2str`a${1}b${2}c`;
>>> >>>>> // "a1b2c"
>>> >>>>> ```
>>> >>>>>
>>> >>>>> Throughts?
>>> >>>>> _______________________________________________
>>> >>>>> es-discuss mailing list
>>> >>>>> es-discuss at mozilla.org
>>> >>>>> https://mail.mozilla.org/listinfo/es-discuss
>>> >
>>> > _______________________________________________
>>> > es-discuss mailing list
>>> > es-discuss at mozilla.org
>>> > https://mail.mozilla.org/listinfo/es-discuss
>>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190816/2575d712/attachment.html>


More information about the es-discuss mailing list