Open thertweck opened 10 years ago
You might want to have a look at this: http://sassmeister.com/gist/c36be3440dc2b5ae9ba2.
I think it's not quite the same. The walk()-function resembles the typical "map" collection processing pattern. The difference to reduce() would be that the items in a list are reduced to a single value.
Applications include calculating the sum/product/... of elements or if the items are strings concat them.
For a complete set of lambda style collection processing functions a filter() method could also come in handy sometimes.
Btw.: I love your Blog!
I'll have a closer look at this.
I would probably implement it like this:
@function reduce($list, $function, $base, $args...) {
@if not function-exists($function) {
@error "Cannot find callback `#{$callback}`.";
}
@each $item in $list {
$base: call($function, $base, $item, $args...);
}
@return $base;
}
@function add($a, $b, $args...) {
$result: $a + $b;
@if length($args) == 0 {
@return $result;
}
@each $arg in $args {
$result: $result + $arg;
}
@return $result;
}
test {
a: reduce(a b c d e, add, "");
b: reduce(1 2 3 4 5, add, 0);
c: reduce(red green blue, mix, red);
}
Hello!
A sl-reduce() function would be great! Check this:
And a callback:
Could be used like this: