Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support reduction #48

Open
thertweck opened this issue Nov 9, 2014 · 4 comments
Open

Support reduction #48

thertweck opened this issue Nov 9, 2014 · 4 comments

Comments

@thertweck
Copy link

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
Copy link
Contributor

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

@thertweck
Copy link
Author

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
Copy link
Contributor

I'll have a closer look at this.

@KittyGiraudel
Copy link
Contributor

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants