Suggestion: a chainable in-place alternative of `Array.prototype.map`.

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

Suggestion: a chainable in-place alternative of `Array.prototype.map`.

Yeong-u Kim

# Suggestion: a chainable in-place alternative of `Array.prototype.map`.

 

----------

 

 I personally do make massive use of `Array.prototype.map` whenever I code in ECMAScript. However, it often leaves an array waiting for garbage collection (which makes unnecessary memory use and burdens the garbage collector) when you no longer need the original array values. In order to avoid it, you may do this in-place with `Array.prototype.forEach` instead. But the problem is that it is not chainable unlike `Array.prototype.fill`.

 

```javascript

/// To get ["#apple", "#banana", "#cabbage"]

 

const tag = name => ('#' + name);

 

if("Try #1")

{

["apple", "banana", "cabbage"].forEach((n, i, a) => {

a[i] = tag(n);

}); // It is not chainable -- returns nothing but `undefined`.

}

 

if("Try #2")

{

// So we need to declare a variable holds the array in this scope.

const array = ["apple", "banana", "cabbage"];

array.forEach((n, i) => {

array[i] = tag(n);

}); // We cannot feed it `tag` directly.

const result = array;

}

 

if("Try #3")

{

const result = ["apple", "banana", "cabbage"].map(tag);

// It looks neat, but does not make use of the existing memory and leaves ‘garbage.’

}

 

if("Suggestion")

{

const result = ["apple", "banana", "cabbage"].update(tag);

// The array values are modified (updated) by `tag` in-place,

// and it returns the updated array -- it's chainable.

}

```

 

 So I suggest `Array.prototype.update` and `TypedArray.prototype.update`. It is similar to `Array.prototype.map` except for its in-place modification. It is also similar to `Array.prototype.fill`.

 

```javascript

Array.prototype.update(function callback(currentValue[, index[, array]]) {......}[, thisArg])

```

 

|                                                             | fill | map | update |

|-------------------------------------------------------------|:----:|:---:|:------:|

| Chainable                                                   |  Yes | Yes |   Yes  |

| In-place modification (a side effect on the original array) |  Yes |  No |   Yes  |

| Returns a reference to the input (original) array.          |  Yes |  No |   Yes  |

| Ignores holes (empty elements).                             |  No  | Yes |   Yes  |

| Makes use of the input (original) array values.             |  No  | Yes |   Yes  |

 

 

 How do you think about this suggestion?



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Suggestion: a chainable in-place alternative of `Array.prototype.map`.

Naveen Chawla
Already discussed (at the end there's an Array.prototype.each idea that's the same): https://esdiscuss.org/topic/return-value-of-foreach 

On Thu, 8 Feb 2018 at 14:17 Yeong-u Kim <[hidden email]> wrote:

# Suggestion: a chainable in-place alternative of `Array.prototype.map`.

 

----------

 

 I personally do make massive use of `Array.prototype.map` whenever I code in ECMAScript. However, it often leaves an array waiting for garbage collection (which makes unnecessary memory use and burdens the garbage collector) when you no longer need the original array values. In order to avoid it, you may do this in-place with `Array.prototype.forEach` instead. But the problem is that it is not chainable unlike `Array.prototype.fill`.

 

```javascript

/// To get ["#apple", "#banana", "#cabbage"]

 

const tag = name => ('#' + name);

 

if("Try #1")

{

["apple", "banana", "cabbage"].forEach((n, i, a) => {

a[i] = tag(n);

}); // It is not chainable -- returns nothing but `undefined`.

}

 

if("Try #2")

{

// So we need to declare a variable holds the array in this scope.

const array = ["apple", "banana", "cabbage"];

array.forEach((n, i) => {

array[i] = tag(n);

}); // We cannot feed it `tag` directly.

const result = array;

}

 

if("Try #3")

{

const result = ["apple", "banana", "cabbage"].map(tag);

// It looks neat, but does not make use of the existing memory and leaves ‘garbage.’

}

 

if("Suggestion")

{

const result = ["apple", "banana", "cabbage"].update(tag);

// The array values are modified (updated) by `tag` in-place,

// and it returns the updated array -- it's chainable.

}

```

 

 So I suggest `Array.prototype.update` and `TypedArray.prototype.update`. It is similar to `Array.prototype.map` except for its in-place modification. It is also similar to `Array.prototype.fill`.

 

```javascript

Array.prototype.update(function callback(currentValue[, index[, array]]) {......}[, thisArg])

```

 

|                                                             | fill | map | update |

|-------------------------------------------------------------|:----:|:---:|:------:|

| Chainable                                                   |  Yes | Yes |   Yes  |

| In-place modification (a side effect on the original array) |  Yes |  No |   Yes  |

| Returns a reference to the input (original) array.          |  Yes |  No |   Yes  |

| Ignores holes (empty elements).                             |  No  | Yes |   Yes  |

| Makes use of the input (original) array values.             |  No  | Yes |   Yes  |

 

 

 How do you think about this suggestion?


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss