question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rework coordReduce Meta

See original GitHub issue

I’m trying to use @turf/meta coordReduce and it doesn’t seem to have the same flow & expected output as Array.reduce().

Issues:

  • Missing Index
  • Missing Array
  • Number of iterations are different (first set of coordinates should be excluded)

Using Array.reduce()

var line = lineString([[126, -11], [129, -21], [135, -31]]);
var coordinates = line.geometry.coordinates;
coordinates.reduce(function (previous, current, index, array) {
    console.log(previous, current, index, array);
    return current;
});

Output

[ 126, -11 ] [ 129, -21 ] 1 [ [ 126, -11 ], [ 129, -21 ], [ 135, -31 ] ]
[ 129, -21 ] [ 135, -31 ] 2 [ [ 126, -11 ], [ 129, -21 ], [ 135, -31 ] ]

Using Turf Meta coordReduce()

var line = lineString([[126, -11], [129, -21], [135, -31]]);
coordReduce(line, function (previous, current, index, array) {
    console.log(previous, current, index, array);
    return current;
});

Output

undefined [ 126, -11 ] undefined undefined
[ 126, -11 ] [ 129, -21 ] undefined undefined
[ 129, -21 ] [ 135, -31 ] undefined undefined

CC: @tmcw

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tmcwcommented, Mar 1, 2017

Yep! index and at least trying to implement the special case of Array.reduce sounds good.

0reactions
DenisCarrierecommented, Mar 1, 2017

@tmcw OK FINALLY… I’ve got BOTH examples working. We were both right, you can now use coordReduce in both scenarios we mentioned above:

previous & current coordinates (my example)

var line = lineString([[126, -11], [129, -21], [135, -31]]);
coordReduce(line, function (previousCoords, currentCoords, currentIndex) {
    console.log(previousCoords, currentCoords);
    return currentCoords;
})

output

[ 126, -11 ] [ 129, -21 ] 1
[ 129, -21 ] [ 135, -31 ] 2

memo calculation (your example)

var line = lineString([[126, -11], [129, -21], [135, -31]]);
var sum = coordReduce(line, function (previousValue, currentCoords) {
    console.log('previous:', previousValue);
    var currentValue = previousValue + currentCoords[0];
    return currentValue;
}, 0);
console.log('total:', sum);

output

previous: 0
previous: 126
previous: 255
total: 390

Final JSDocs

/**
 * Callback for coordReduce
 *
 * The first time the callback function is called, the values provided as arguments depend
 * on whether the reduce method has an initialValue argument.
 *
 * If an initialValue is provided to the reduce method:
 *  - The previousValue argument is initialValue.
 *  - The currentValue argument is the value of the first element present in the array.
 *
 * If an initialValue is not provided:
 *  - The previousValue argument is the value of the first element present in the array.
 *  - The currentValue argument is the value of the second element present in the array.
 *
 * @private
 * @callback coordReduceCallback
 * @param {*} accumulator The accumulated value previously returned in the last invocation
 * of the callback, or initialValue, if supplied.
 * @param {[number, number]} currentCoords The current coordinate being processed.
 * @param {number} currentIndex The index of the current element being processed in the
 * array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
 */

/**
 * Reduce coordinates in any GeoJSON object into a single value,
 * similar to how Array.reduce works. However, in this case we lazily run
 * the reduction, so an array of all coordinates is unnecessary.
 *
 * @name coordReduce
 * @param {Object} layer any GeoJSON object
 * @param {coordReduceCallback} callback a method that takes (previousValue, currentCoords, currentIndex)
 * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.
 * @param {boolean=} excludeWrapCoord whether or not to include
 * the final coordinate of LinearRings that wraps the ring in its iteration.
 * @returns {*} The value that results from the reduction.
 */
function coordReduce(layer, callback, initialValue, excludeWrapCoord) {
Read more comments on GitHub >

github_iconTop Results From Across the Web

Riot Reworks the entire Game! - (Meta Breaking Patch)
Valorant: Riot Reworks the entire Game - ( Meta Changing Patch)➡️ https://www.twitch.tv/dittozkul➡️ Wanna join our games?
Read more >
@turf/meta | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
turf meta module - Npms.io
2 results for coordReduce. @turf/meta(6.5.0). Q. P. M. 73. turf meta module. local_offerfunctional, programming, turfjs, geojson, meta, flattenEach, ...
Read more >
red-contrib-turf-module (node) - Node-RED Library
This is a complete rework of the node-red-contrib-turf node. ... For turf modules with callback functions (e.g.: coord-reduce) only msg , flow ,...
Read more >
League of Legends' high-damage meta finally being ... - Dexerto
Back in December, 2021, Riot promised that changes were coming to balance out defense and offense in Season 12, and Patch 12.10 is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found