Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map filter and map functions #2653

Closed
3 tasks
damienwebdev opened this issue Feb 8, 2019 · 1 comment
Closed
3 tasks

Map filter and map functions #2653

damienwebdev opened this issue Feb 8, 2019 · 1 comment

Comments

@damienwebdev
Copy link

Proposal

Add common "functional" functions to iterable objects SASS.
There are several functions of this class that could be added and would provide significant benefit. E.g.

  • map
  • filter
  • reduce

Use Case

When writing sass, I find that I spend a significant amount of time writing things that look like:

$map: (test: 100, test: 200);
$newMap: ();
@each $key, $value in $map {
    $newMap: map-merge(
        $newMap, 
        ($key: (someFunction($value)))
    );
}

This common structure is often known as "map" in many functional languages.

In an ideal world I'd write something like:

$map: (test: 100, test: 200);
@function mapFunction ($key, $value){
 @return ($key: someFunction($value));
}
$newMap: map($map, mapFunction);

Side notes

There are some odd language semantics here (lambdas), that I've not yet seen in SASS, but may be possible (first class functions).

Additionally, if SASS could support anonymous lambdas (probably difficult), that may also be cool.

Edge cases

There's also probably some edge cases around providing values that are not args to the callback as values inside the callback function.

@nex3
Copy link
Contributor

nex3 commented Feb 8, 2019

I'm going to mark this as a duplicate of #472. Higher-order functions like map() and filter() don't really make sense without the ability to write anonymous functions, and if we added anonymous functions we'd definitely add library functions that supported them.

This is difficult for a number of reasons, though. Anonymous functions are syntactically and semantically complex, especially for a userbase that is often coming from a pure CSS background and unfamiliar with more general-purpose programming languages. At the same time, because Sass is not general-purpose, the set of circumstances where these features are applicable is narrower than in other languages. It's not a priori clear to me that this feature would pull its weight, especially if we consider alternatives like a dedicated list comprehension syntax.

@nex3 nex3 closed this as completed Feb 8, 2019
@nex3 nex3 transferred this issue from another repository Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants