Skip to content

Instantly share code, notes, and snippets.

@BPScott
Created September 20, 2015 11:59
Show Gist options
  • Save BPScott/ef6b84e8df6aace3c0d0 to your computer and use it in GitHub Desktop.
Save BPScott/ef6b84e8df6aace3c0d0 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// libsass (v3.2.5)
// ----
$items: (
small: 320,
medium: 600,
large: 1008
);
// Imperative style, but without mutation
$new-items-imperative: ();
@each $key, $value in $items {
$new-value: $value *2;
$new-items-imperative: map-merge($new-items-imperative, ($key: $new-value))
}
// Function Programming style, enforcing no mutation
// Generic map function, currently this only handles Sass maps, but we could make
// it handle lists too later
@function map-map($map, $function) {
@if not function-exists($function) {
@error "There is no `#{$function}` function.";
}
$new-map: ();
@each $key, $value in $map {
$new-map: map-merge($new-map, ($key: call($function, $value)));
}
@return $new-map;
}
// Sass doesn't do anonymous functions, so you need to define the function separatly
// to the call to map
@function double($value) {
@return $value *2;
}
$new-items-functional: map-map($items, 'double');
// Demoing the output of these three variables to prove no mutation has occured
html {
items: $items;
new-items-imperative: $new-items-imperative;
new-items-functional: $new-items-functional;
}
html {
items: (small: 320, medium: 600, large: 1008);
new-items-imperative: (small: 640, medium: 1200, large: 2016);
new-items-functional: (small: 640, medium: 1200, large: 2016);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment