Skip to content

Instantly share code, notes, and snippets.

@pdaoust
Created December 5, 2013 22:57
Show Gist options
  • Save pdaoust/7815559 to your computer and use it in GitHub Desktop.
Save pdaoust/7815559 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
// 'map', 'transform', 'select', or 'project' function. Iterate over a list,
// performing a function on each item, and collecting the new items. Each
// function takes an item as a first argument and returns a transformed item.
// You can pass additional arguments to the function too, which is a decent poor
// man's function composition.
// (I didn't call this f-map because the term 'map' is already used in Sass to
// denote a hash or dictionary.)
@function f-apply($func, $list, $args...) {
$new-list: ();
@each $item in $list {
$new-list: append($new-list, call($func, $item, $args...));
}
@return $new-list;
}
@function square($x) {
@return $x * $x;
}
p {
// using the plain ol' square function
square: square(3);
// now let's lift it to work on lists using a higher-order function
squares: f-apply(square, (1 2 3 4 5 6 7));
}
p {
square: 9;
squares: 1 4 9 16 25 36 49;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment