How to create list output in less? - css

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.

You'll create a modified "list" by passing the concatenated result of each loop iteration to next iteration, thus the final iteration defines the actual result. E.g. (just illustrating the principle w/o adopting it to your use-case):
// usage:
#input: 1, 2, 3, 4;
result {
.modify-list(#input);
result: #result;
}
// impl:
.modify-list(#list) {
#n: length(#list);
.-(#n, extract(#list, #n) * 10);
.-(#i, #r) when (#i > 1) {
.-(#i - 1; extract(#list, #i - 1) * 10, #r);
}
.-(1, #r) {#result: #r}
}
Demo
Technically this code does not create a one dimensional list (i.e. the result there is not really equal to #result: 10, 20, 30, 40;) but since in final CSS it's rendered identically it would work just fine.
Also assuming your original use-case was How to apply less functions to gradient colors?, you don't really need a code like this (i.e. it's an "XY Problem" and instead of generating a new variable with the new list, direct generation of the corresponding gradient values would be much less verbose and much more readable. See the linked answer in comments of the mentioned question).

Related

Issue with Deprecated Division warning in SASS

I am doing a large project converting old SASS code (specifically node-sass), into the newer SASS using the SASS npm package, and I am getting this error message
DEPRECATION WARNING: Using / for division is deprecated and will be removed in Dart Sass 2.0.0. Recommendation: math.div(3, 4)
However, it seems to relate to this line of code
$_this: nth($short, $_i);
I am finding this confusing as I am not trying to divide the numbers, and the aren't separated by a /, can someone explain please, or at least help me with a solution, as ignoring the line isn't working either?
If it helps anyone, I have included the whole statement now...
#function parse-span(
$short,
$key: span
) {
$_return: ();
#if type-of($short) == map {
$_return: $short;
} #else {
$_at: index($short, at);
#if $_at {
$_loci: $_at + 1;
$_location: nth($short, $_loci);
$_return: map-merge($_return, (location: $_location));
$short: set-nth($short, $_at, null);
$short: set-nth($short, $_loci, null);
}
$_i: 1;
$_span: ();
#while $_i <= length($short) {
$_this: nth($short, $_i); <--- faulty line?
#if type-of($_this) == number {
$_span: append($_span, $_this);
$short: set-nth($short, $_i, null);
} #else if $_this == of {
$short: set-nth($short, $_i, null);
$_i: length($short) + 1;
}
$_i: $_i + 1;
}
#if length($_span) > 0 {
$_span: if(length($_span) == 1, nth($_span, 1), $_span);
$_return: map-merge($_return, ($key: $_span));
}
$_return: map-merge($_return, parse-grid($short));
}
#return $_return;
}
$_gutters: parse-span($short, gutter-override);
I have tried the solution in the comments but to no resolution.
Here is an image of the actual warning

Trying to use variables in HSL function (color functions take numbers as parameters)

I'm getting this error while trying to compile a HSL function:
http://codepen.io/anon/pen/GJVbzB?editors=010
#baseColor: #003459;
#randomVar: `Math.floor(Math.random()*10 + 90).toString()`;
#color: ~"#{randomVar}%";
#new: hsl(hue(#baseColor), #color, #color);
If I'm using the #test variable, it works!
How is that possible and what am I wrong?
The rough thing is that this function wants percentage and I had to find a way to concat the result of the math.random with an "%".
Your analysis of the problem is spot on. The hsl() function does need a percentage value as input. But the problem was in the way you were creating the percentage input.
There were two problems and they are as follows:
You were using .toString() which was converting the number into a String. This is not needed. You can just remove it altogether.
Second thing to note is that you were using String concatenation to convert the random number into a percentage. This means that the value formed is a String and no longer a percentage or a number. You can avoid this by multiplying the #randomVar with 1% which would automatically make it a percentage.
The below snippet (having both those corrections done) should work for you:
.randomColor() {
#baseColor: #003459;
#randomVar: `Math.floor(Math.random()*10 + 90)`;
#color: #randomVar * 1%;
#new: hsl(hue(#baseColor), #color, #color);
#test: 25%;
}
.generate-bg-color(#spanNumber, #i: 1) when (#i =< #spanNumber) {
span:nth-child(#{i}) {
.randomColor();
background-color: #new;
}
.generate-bg-color(#spanNumber, (#i + 1));
}
.generate-bg-color(24);
When you assign a percentage value directly to a variable (like #test: 25%), Less compiler by default treats it as a number (or a percentage) instead of as a String and hence doing so does not cause any problems.
You also have a couple of other options among which one is to use the unit() function as mentioned in nils answer and the other is to use the built-in percentage() function like in the below snippet. All of them do pretty much the same thing but a multiplication by 1% is a bit more intuitive in my opinion.
.randomColor() {
#baseColor: #003459;
#randomVar: `Math.floor(Math.random()*10 + 90)`;
#color: percentage(#randomVar / 100);
#new: hsl(hue(#baseColor), #color, #color);
#test: 25%;
}
.generate-bg-color(#spanNumber, #i: 1) when (#i =< #spanNumber) {
span:nth-child(#{i}) {
.randomColor();
background-color: #new;
}
.generate-bg-color(#spanNumber, (#i + 1));
}
.generate-bg-color(24);
You are pretty close. There is a function called unit in less that appends a unit to a variable's contents (if they are numeric).
Just remove the .toString() part, because that turns your number into a string and unit only takes numbers.
.randomColor() {
#baseColor: #003459;
#randomVar: `Math.floor(Math.random()*10 + 90)`;
#color: unit(#randomVar, %);
#new: hsl(hue(#baseColor), #color, #color);
#test: 25%;
}
.generate-bg-color(#spanNumber, #i: 1) when (#i =< #spanNumber) {
span:nth-child(#{i}) {
.randomColor();
background-color: #new;
}
.generate-bg-color(#spanNumber, (#i + 1));
}
.generate-bg-color(24);

Confused by simplescalr preditor

now I am learning simplescalar source code. But I am confused by the predictor module. It is about bimod predictor.
Here is the initialization :
case BPred2bit:
if (!l1size || (l1size & (l1size-1)) != 0)
fatal("2bit table size, `%d', must be non-zero and a power of two",
l1size);
pred_dir->config.bimod.size = l1size;
if (!(pred_dir->config.bimod.table =
calloc(l1size, sizeof(unsigned char))))
fatal("cannot allocate 2bit storage");
/* initialize counters to weakly this-or-that */
flipflop = 1;
for (cnt = 0; cnt < l1size; cnt++)
{
pred_dir->config.bimod.table[cnt] = flipflop;
flipflop = 3 - flipflop;
}
break;
Here we use the PHT table:
case BPred2bit:
p = &pred_dir->config.bimod.table[BIMOD_HASH(pred_dir, baddr)];
break;
But what to my suprise is the PHT tale NEVER be updated!!!. I find the code nowhere, neither in the pred_update() funtion!!!.
Can you tell me the reason? What mechanism dose simplescalar use?
But it is updated. In bpred_update() you find this code
if (dir_update_ptr->pdir1)
{
if (taken)
{
if (*dir_update_ptr->pdir1 < 3)
++*dir_update_ptr->pdir1;
}
else
{ /* not taken */
if (*dir_update_ptr->pdir1 > 0)
--*dir_update_ptr->pdir1;
}
}
An entry of the table is either incremented or decremented depending on the outcome of the branch. That particular entry comes from the second code segment in your question. It's just a pointer to a 2-bit counter.

Less mixin scope problems

I built a parametric mixin that finds the children of an element based on a "node-value" system that I created. What happens is the children are found by a loop function ".extractArrays" with ".deliverChild" inside it. Then the children (up to 4 of them) are put into ".calculateWidth", which returns the width of the child in a variable (ex. "calculatedWidth1").
The problem is: when there are no third and fourth children, #child3 and #child4 are unset, which creates an error. I set #child3 and #child4 to zero before the mixins, hoping that this would give them default values of 0 before the mixins are called. For some reason, however, when there is a third child, the mixin's returned value of #child3 won't override the #child3 that is set to 0. I am unsure why this is happening, but I feel like there are a couple things that throw me off about Less when it comes to mixins and scope.
#child3: none, 0 0, 0, 0;
#child4: none, 0 0, 0, 0;
.extractArrays(#index, #node, #node-value, #elements-array) when (#index <= #length-elements-array) {
#element-array: extract(#elements-array, #index);
#found-node: extract(#element-array, 2);
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value1) {
#child1: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value2) {
#child2: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value3) {
#child3: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value4) {
#child4: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node);
.extractArrays(#index + 1, #node, #node-value, #elements-array);
}
.extractArrays(1, #node, #node-value, #elements-array);
.calculateWidth(#child1, 1);
.calculateWidth(#child2, 2);
.calculateWidth(#child3, 3);
width: #calculatedWidth1 + #calculatedWidth2 + #calculatedWidth3 + #calculatedWidth4;
(Not an exact answer to the "scope" question above but an algorithm to use as a starting point in an alternative approach suggested in comments above):
A generic array filtering mixin would look like this (Less 1.7.5 or higher):
#array: // key value
a 1,
b 2,
c 3,
a 4,
d 5,
a 6,
c 7;
// usage:
usage {
.filter-array(#array, a);
result: #filter-array;
}
// impl.:
.filter-array(#array, #key, #key-index: 1) {
.-(length(#array));
.-(#i, #r...) when (#i > 0)
and not(#key = extract(extract(#array, #i), #key-index)) {
// skip item since key does not match
.-((#i - 1), #r);
}
.-(#i, #r...) when (length(#r) > 0)
and (#key = extract(extract(#array, #i), #key-index)) {
// key matches and #r is not empty so concat current item to #r
.-((#i - 1); extract(#array, #i), #r);
}
.-(#i, #r...) when (length(#r) = 0)
and (#key = extract(extract(#array, #i), #key-index)) {
// key matches and #r is empty so this is our first item
.-((#i - 1), extract(#array, #i));
}
.-(#i, #r...) when (#i = 0) {
// define "return" variable:
#filter-array: #r;
}
}
With resulting array being equal to:
#filter-array:
a 1,
a 4,
a 6;
This looks quite scary and verbose but still more clean and more controllable than the original approach.
Additionally it would make sense to provide a dedicated mixin to return a sum (or any other "transformation" with result in a single value) of certain values of the array items (that way the implementation can be simplified to a certain point since you won't need to concatenate anything and some of above conditions and/or mixin specializations become unnecessary).

Using Count To Split A Checklist Into 2 Columns in X++ Fetch Method

Here is what I have so far, this is returning two columns, but each counter is stopping and then duplicating the same value over and over...
if(lLogisticsControlTable.APMJobTypeId)
select count (RecID) from jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId;
{
counter = jobTypeCheck.RecId;
}
while select jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId
{
counter1 = counter / 2;
halfCount1 = counter - counter1;
if(halfcount <= counter1)
{
halfCount++;
jobListCheck1 = jobTypeCheck.Name;
}
if (halfCount1 > halfCount)
{
halfCount1++;
jobListCheck2 = jobTypeCheck.Name;
}
element.execute(2);
}
}
As Michael Brown indicated, it's difficult to understand the problem with half of the code ;)
However, I would suggest that you call the element.execute(2) method on every second pass through the loop? That way jobListCheck1 would be on the left, and jobListCheck2 would be on the right hand side. Finally you would then need to check immediately outside of your loop if you had an odd number of jobTypeCheck elements, and call the element.execute(2) method one last time remembering to set the jobListCheck2 variable as empty beforehand.
Regards

Resources