Skip to content

Instantly share code, notes, and snippets.

@lunelson
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunelson/8920028 to your computer and use it in GitHub Desktop.
Save lunelson/8920028 to your computer and use it in GitHub Desktop.
Additional functions for Sass List Maps
// ----
// Sass (v3.2.14)
// Compass (v0.12.2)
// Sass List-Maps (v0.9.3)
// ----
@import "sass-list-maps";
/*
EXTRA FUNCTIONS FOR SASS LIST MAPS
*/
// map-subvalues() function: retrieves values for key at given depth
@function map-subvalues($map, $key, $depth) {
$output: ();
@if $depth > 1 { @each $tuple in $map { $output: append($output, map-subvalues(value($tuple), $key, $depth - 1)); } }
@else { $output: get($map, $key); }
@return $output;
}
$memo-data: ();
// helper to chain arguments as a string
@function arg-chain($args...) {
$output: ('#{nth($args, 1)}');
@for $n from 2 through length($args) { $output: $output + '-#{nth($args, $n)}'; }
@return $output;
}
// memoization pattern
@function memoizingFunction($arg1, $arg2) {
$memo-key: 'memoizingFunction-' + arg-chain($arg1, $arg2);
$memo-val: map-get($memo-data, $memo-key);
@if $memo-val { @return $memo-val; }
@else {
$memo-val: 'TBD';
$memo-data: map-merge-z($memo-data, $memo-key, $memo-val);
@return $memo-val;
}
}
/*
EXTRA FUNCTIONS FOR SASS LIST MAPS
*/
@lunelson
Copy link
Author

revision of mq approach; waiting to merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment