Skip to content

Commit

Permalink
feat(reduce): run-function-with-params for decorating function
Browse files Browse the repository at this point in the history
  • Loading branch information
jackw committed Sep 3, 2020
1 parent 32952ac commit d7666f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/_reduce.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
@use "./internal/is";
@import './internal/run-function-with-params';
@import './head';
@import './tail';

/// Returns a single item by iterating through the list, successively calling
/// the iterator function and passing it an accumulator value and the current
/// value from the array, and then passing the result to the next call.
Expand All @@ -15,12 +20,13 @@
/// @debug $sum-of-numbers-plz; //=> 45

@function reduce($function, $acc, $list) {
$fn: if(is.list($function), head($function), $function);
$fn-params: if(is.list($function), tail($function), ());
$accumulator: $acc;
@for $i from 1 through length($list) {
$accumulator: call(
get-function($function),
$accumulator,
nth($list, $i)
$accumulator: _run-function-with-params(
$fn,
join($fn-params, ($accumulator, nth($list, $i)))
);
}

Expand Down
3 changes: 2 additions & 1 deletion test/_reduce.spec.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@import 'true';
@import '../src/reduce';
@import '../src/add';
@import '../src/sum';

@include describe('reduce [function]') {
@include it('should run a function against each item in a list') {
@include assert-equal(reduce(add, 0, (1 2 3 4 5 6 7 8 9)), 45);
@include assert-equal(reduce((sum 5), 0, (1 2 3 4 5 6 7 8 9)), 90);
}
}

0 comments on commit d7666f2

Please sign in to comment.