Created
March 24, 2014 12:00
-
-
Save FWeinb/9738899 to your computer and use it in GitHub Desktop.
Functional Sass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.3.4) | |
// Compass (v1.0.0.alpha.18) | |
// ---- | |
@function _f-normalize($param){ | |
@if ( type-of($param) != list ) { $param : ($param,) }; | |
@return $param; | |
} | |
@function _f-check-func($func){ | |
@if not function-exisits($func) { | |
@warn "#{$func} isn't a valid function"; | |
@return false; | |
} | |
@return true; | |
} | |
@function _f-check-funcList($func-list){ | |
@each $func in $func-list { | |
@if not _f-check-func($func) { | |
@return false; | |
} | |
} | |
@return true; | |
} | |
// Filter all items in $value-list by calling the function $func for each item. | |
// -------------------------------------------------------------------------------- | |
// @param $value-list : List of values to map | |
// -------------------------------------------------------------------------------- | |
// @throw "First parameter must be a list of values" | |
// -------------------------------------------------------------------------------- | |
// @throw "Second parameter must be a list or string of functions" | |
// -------------------------------------------------------------------------------- | |
// @return values | null | |
@function f-filter($value-list, $func){ | |
$value-list : _f-normalize($value-list); | |
@if not _f-check-func($func){ | |
@return null; | |
} | |
$result: (); | |
@each $value in $value-list{ | |
@if ( call($func, $value) ){ | |
$result : append($result, $value); | |
} | |
} | |
@return $result; | |
} | |
// Run each value in $value-list through each function in $func-list | |
// -------------------------------------------------------------------------------- | |
// @param $value-list : List of values to map | |
// -------------------------------------------------------------------------------- | |
// @param $func-list : List of functions to run on the values | |
// -------------------------------------------------------------------------------- | |
// @throw "First parameter must be a list of values" | |
// -------------------------------------------------------------------------------- | |
// @throw "Second parameter must be a list or string of functions" | |
// -------------------------------------------------------------------------------- | |
// @return values | null | |
@function f-map($value-list, $func-list){ | |
// Convert a single value to be in a list | |
$value-list : _f-normalize($value-list); | |
$func-list : _f-normalize($func-list); | |
@if not _f-check-func($func-list){ | |
@return null; | |
} | |
@each $func in $func-list{ | |
@for $i from 1 through length($value-list){ | |
$value-list : set-nth($value-list, $i, call($func, nth($value-list, $i))); | |
} | |
} | |
@return $value-list; | |
} | |
// Test functions | |
@function add($x, $y){ | |
@return $x + $y; | |
} | |
@function mul($x, $y){ | |
@return $x * $y; | |
} | |
@function add2($x){ | |
@return add($x, 2); | |
} | |
@function mul2($x){ | |
@return mul($x, 2); | |
} | |
@function biggerThanFive($value){ | |
@return $value > 5; | |
} | |
$list : 1 2 3 4 5 6 7 8 9 10; | |
.test{ | |
map: f-map($list, add2); | |
filter: f-filter($list, biggerThanFive); | |
filterAndMap: f-map(f-filter($list, biggerThanFive), mul2); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.test { | |
map: 3 4 5 6 7 8 9 10 11 12; | |
filter: 6 7 8 9 10; | |
filterAndMap: 12 14 16 18 20; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment