patternMinor
CFML implementation of Array.reduce()
Viewed 0 times
arrayimplementationreducecfml
Problem
G'day
Just before I submit this to CFLib, it'd be great to get feedback:
I've created proper unit tests as a gist.
Just before I submit this to CFLib, it'd be great to get feedback:
/**
* @hint CFML implementation of Array.reduce(), similar to Javascript's one ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
* @array Array to reduce
* @callback Callback function to use to reduce. Will receive the following arguments: element (of current iteration of the all), index, array, (optional) result (of preceeding call to callback())
* @initialValue The initial value to use to start the reduction
*/
public any function arrayReduce(required array array, required any callback, any initialValue){
var startIdx = 1;
if (!structKeyExists(arguments, "initialValue")){
if (arrayLen(array) > 0){
var result = callback(array[1], 1, array);
startIdx = 2;
}else{
return;
}
}else{
var result = initialValue;
}
for (var i=startIdx; i I've created proper unit tests as a gist.
Solution
Interesting discussion!
@AdamTuttle Regarding the behavior in Underscore that you've highlighted:
I did some quick tests and found that the same behavior exists in Ruby and native JS (Chrome and FF). This confirmed my suspicions that this seemingly weird behavior is just a product of the reduce algorithm itself.
In the algorithm, "memo" represents the current state of the fold operation. If you don't pass in an initial value, you're implying that the first element of the collection represents the initial state of the fold. From that perspective, the behavior you've shown is exactly what I'd expect.
If you want to remove uppercase letters from a collection and convert that to a string in a functional way, it would make more sense to call
@AdamTuttle Regarding the behavior in Underscore that you've highlighted:
I did some quick tests and found that the same behavior exists in Ruby and native JS (Chrome and FF). This confirmed my suspicions that this seemingly weird behavior is just a product of the reduce algorithm itself.
In the algorithm, "memo" represents the current state of the fold operation. If you don't pass in an initial value, you're implying that the first element of the collection represents the initial state of the fold. From that perspective, the behavior you've shown is exactly what I'd expect.
If you want to remove uppercase letters from a collection and convert that to a string in a functional way, it would make more sense to call
filter() to and then reduce(). Example: adamArr = ['A','d','a','m'];
damArr = _.filter(adamArr, function (letter) {
var asciiCode = asc(letter);
return asciiCode >= 97 && asciiCode <= 122;
};
damString = _.reduce(damArr, function (memo, letter) {
return memo & letter;
};Code Snippets
adamArr = ['A','d','a','m'];
damArr = _.filter(adamArr, function (letter) {
var asciiCode = asc(letter);
return asciiCode >= 97 && asciiCode <= 122;
};
damString = _.reduce(damArr, function (memo, letter) {
return memo & letter;
};Context
StackExchange Code Review Q#28968, answer score: 4
Revisions (0)
No revisions yet.