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

Creating list output in less #2699

Closed
YahweRaj opened this issue Sep 19, 2015 · 6 comments
Closed

Creating list output in less #2699

YahweRaj opened this issue Sep 19, 2015 · 6 comments

Comments

@YahweRaj
Copy link

I am trying to create a list which is calculated from another list. For example, if I have a list like 1,2,3,4... then the output has to be 10,20,30,40... Is there any other way to create a list from another in less? Please refer the below codes.

@input: 1,2,3,4;
.for(@array)   when (default()) {.for-impl_(length(@array))}
.for-impl_(@i) when (@i > 1)    {.for-impl_((@i - 1))}
.for-impl_(@i) when (@i > 0)    {.-each(extract(@array, @i), @i)}

.create-list(@input){
  .for(@input); .-each(@value, @a) { 
     .output_@{a}(@a) {  // Create dynamic mixin based on input list
       @output_@{a}: @a * 10; // Create dynamic variable to store each calc
     } 
   }
} 
.create-list(@input);

.loop(@count) when (@count > 0){
  @prev: @count - 1;
  .loop(@prev);
  .first() when (@count = 1){ 
    .output_@{count}();  // call first mixin which created already
    @res_@{count}: @output_@{count} // Store the result in another dynamic var
  }
  .first() when not (@count = 1){
    .output_@{count}();
    @res_@{count}: @res_@{prev},  @output_@{count}; // join prev and current result
  }
  .first();  
}
.loop(4);

The above implementation similar I expect like below.

.a1(){
  @o1: #fff;
}

.a2(){
  @o2: #aaa;
}

.a3(){
  @o3: #ccc;
}

.loop(@counter) when (@counter > 0) {
  .loop((@counter - 1));   
  .a1();
   @out1: @o1;
  .a2(); 
  @out2: @out1, @o2;
  .a3();
   @out: @out2, @o3;
  div{
   colors: @out;
  }
}
.loop(1);

and the output is #fff, #aaa, #ccc.

@YahweRaj YahweRaj changed the title Ceating list output in less Creating list output in less Sep 19, 2015
@rjgotten
Copy link
Contributor

Is there any other way to create a list from another in less?

Not without custom functions.

I baked a map/reduce based on detached rulesets and calling them from custom plugin functions myself some time ago, but it's pretty involved.

Iirc there was somethin regarding building support for list comprehension and/or an (inverse) spread operator into Less some time ago. If that follows through, it will become substantially more easy.

@seven-phases-max
Copy link
Member

Not without custom functions.

Yes, though the trick is that first of all he does not need to create such list at all for his use-case. And secondary even if he would, just plain concatenation will also work just fine (even if resulting list would not be a plain list). (I just did not have time to answer the SO question since currently I'm away from my normal Less environment).

@SomMeri
Copy link
Member

SomMeri commented Sep 21, 2015

Looping is already possible, all you are missing is list concatenation and concatenation will be possible after next release due to #1857

.toList(...) {
  @new-list: @arguments;
}

#example {
  @list-1: 10 20 30;
  @next-number: 40;
  .toList(@list-1..., @lnext-number)
  new-list: @new-list; // prints 10 20 30 40
}

@rjgotten
Copy link
Contributor

after next release due to #1857

Yup. That's the (inverse) spread operator I was referring to before.

@seven-phases-max
Copy link
Member

I answered http://stackoverflow.com/questions/32665124 (as well as commented on the original OP's use-case at http://stackoverflow.com/questions/32658697, thus yet again stressing it's an "XY Problem" and the OP does not need anything like above at all), so I suppose now it's safe to close this (as an offtopic-here "How-To" question).

@seven-phases-max
Copy link
Member

Closing as not an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@rjgotten @SomMeri @seven-phases-max @YahweRaj and others