Skip to content

Instantly share code, notes, and snippets.

@lunelson
Last active December 17, 2015 08:58
Show Gist options
  • Save lunelson/5583473 to your computer and use it in GitHub Desktop.
Save lunelson/5583473 to your computer and use it in GitHub Desktop.
Compound Sum, Length and Average
// functions for finding sum, length and averages
// supports complex/compound lists of values
// assumes either no units or identical units
@function compound-sum($args...) {
$sum: 0;
@each $arg in $args {
@if type-of($arg) == list {
$sum: $sum + compound-sum($arg...);
} @else {
$sum: $sum + $arg;
}
}
@return $sum;
}
@function compound-length($args...) {
$len: 0;
@each $arg in $args {
@if type-of($arg) == list {
$len: $len + compound-length($arg...);
} @else {
$len: $len + 1;
}
}
@return $len;
}
@function compound-average($args...) {
@return compound-sum($args...)/compound-length($args...);
}
//================================== tests
$test-list: 2 4 6 3, 4 5, 6;
.test {
sum: compound-sum($test-list, 5);
len: compound-length($test-list, 5,4);
avg: compound-average($test-list, 5,9);
u-sum: compound-sum(1px 2px, 3px 4px);
}
.test {
sum: 35;
len: 9;
avg: 4.88889;
u-sum: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment