Skip to content

Instantly share code, notes, and snippets.

@seven-phases-max
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seven-phases-max/5280eacdf3d591f35163 to your computer and use it in GitHub Desktop.
Save seven-phases-max/5280eacdf3d591f35163 to your computer and use it in GitHub Desktop.

Well, I'm afraid this is another big story to discuss here. (My solution for such loops would be as in less/less.js#1648 (comment), in short: instead of having "native" each() hardcoded for a very limited particular pattern it would be more generic to allow to write your own mixins which you can adopt for your specific cases by allowing to call mixins by reference. And "more users" and stuff lead to another discussion -> "standard library" (which you can import implicitly if necessary to look "native") ;). Also notice that for the particular example above a separate mixin is only necessary to workaround specific args handling problem. And for a minimal loop, e.g.:

@import "for";

.mixin(@args...) {
    .for(@args); .-each(@v) {
        box-shadow+: @v black;
    }
}

a native each won't really help (or at least it won't make this less verbose). (Technically even more "clean-in-use" syntax is possible now using detached-rulesets but it has its own problems with scopes and hardcoded argument names).

@rjgotten
Copy link

Technically even more "clean-in-use" syntax is possible now using detached-rulesets but it has its own problems with scopes and hardcoded argument names.

Problem (mostly) solved
The trick is in unlocking a special mixin inside the detached ruleset, that is then called explicitly with arguments. This prevents the declaration scope from clobbering 'arguments' passed via the calling scope.

This actually works even in scenarios where calls are nested, e.g. iterating over 'lists of lists', as long as the defined signature for the special .\\ 'lambda mixin' is always the same (and thus the nested call overwrites the outer one).

Now if only I had a way of getting return values back out (detached rulesets don't bubble variables back up); then I'd be able to create map & reduce as well and get some real list manipulation boilerplate going. ^_^

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