at-import / SassyLists

MIT License
276 stars 27 forks source link

Support reduction #48

Open thertweck opened 10 years ago

thertweck commented 10 years ago

Hello!

A sl-reduce() function would be great! Check this:

@function reduce($list, $callback, $accumulator: 0){
    @if(not function-exists($callback)){
        @warn "Cannot find callback '" + inspect($callback) + "'.";
        @return $accumulator;
    }@else{
        @each $item in $list {
            $accumulator: call($callback, $accumulator, $item);
        }
        @return $accumulator;
    }
}

And a callback:

@function add($a, $b){
    @return $a + $b;
}

Could be used like this:

@debug reduce((1 2 3), add); // yields 6
@debug reduce(("hello" "world"), add, ""); // yields "hello world"
KittyGiraudel commented 10 years ago

You might want to have a look at this: http://sassmeister.com/gist/c36be3440dc2b5ae9ba2.

thertweck commented 10 years ago

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!

KittyGiraudel commented 10 years ago

I'll have a closer look at this.

KittyGiraudel commented 10 years ago

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);
}