A Singleton List of List Is Automatically Decomposed

A singleton list of list is automatically decomposed

In Sass, parentheses are used to indicate order of operations, not to indicate that you have a list with a single element.

In Sass 3.3, adding a trailing comma will automatically turn your value into a list:

$foo: 1, ;

What you may be wanting to do instead is have your mixin take a variable number of arguments and use them as a list. That would look like this:

@mixin foo($list...){
@debug length($list);
}

.foo {
@include foo(1);
}

.foo {
@include foo(1, 2);
}

.foo {
@include foo((1, 2));
}

The console gives the desired results:

DEBUG: 1
DEBUG: 2
DEBUG: 1

If you do this, however, the list must be the last argument of the mixin (or function).

Sass nested list appears to be using nested values at root level

You should add a trailing comma to represent a nested list with a single element. From SASS reference:

Comma-separated lists may have a trailing comma. This is especially useful because it allows you to represent a single-element list. For example, (1,) is a list containing 1 and (1 2 3,) is a comma-separated list containing a space-separated list containing 1, 2, and 3.

So, in your case, this would be the code:

$array: (
('a', 'b','c'),
):

You can also use these alternatives:

// Alternative 1
$array: 'a' 'b' 'c',;

// Alternative 2
$array: ();
$array: append($array,('a', 'b','c'));

Appending values to a list

You have to set it to a variable, functions like append() are non-destructive (ie. they don't modify the original list, they only return a new list)

@function foo($list){
@if length($list) < 1 { $list: append($list, 10) }
@if length($list) < 2 { $list: append($list, 20) }
@if length($list) < 3 { $list: append($list, 30) }
// code follows that uses @list
}

Sass: Return list values with unique characters only

To try to resolve this, I found that a way could be check for every items how many same singole letter there are in it (using a double @for loop).

For example, imagine to have this list: abc. If the result is 3 (the length of my item) I know that every letter is only once in that item, then there is no duplicates (3 = 3).

a -- a // 1
a -- b
a -- c
b -- a
b -- b // 2
b -- c
c -- a
c -- b
c -- c // 3

Another example. Now our list is aab. The result is bigger than the length of my item so we have duplicates (5 > 3)

a -- a // 1
a -- a // 2
a -- b
a -- a // 3
a -- a // 4
a -- b
b -- a
b -- a
b -- b // 5

So when the number is bigger than my item's length, I don't append that $temp to my $result

The code:

@function dupChars($list, $separator: comma) {
$result: null;
$temp: null;

@each $item in $list {
$var: 0;
@for $i from 1 through length($item) {

@for $j from 1 through (str-length($item)) {

@for $k from 1 through (str-length($item)) {
//@debug str-slice($item, $j, $j) + "---" + str-slice($item, $k, $k) ;

@if (str-slice($item, $j, $j) == str-slice($item, $k, $k)) {
$var: $var + 1;
}
}
}
}
@if($var <= str-length($item)){
$temp: append($temp, #{$item}, $separator);
}
}

$result: append($result, $temp, $separator);

@return $result;
}

$lista: (aa, bb, cd, ef);

div{content: dupChars($lista)};

The output:

div {
content: cd, ef;
}

How to do length arithmetic when one argument is returned from a function?

The math in example c isn't able to be properly performed because Sass is interpolating that 52vw as a string rather than 52 viewport width units. This is because you're manually adding the letters vw to the function's output.

Instead, you can convert to vw units in the function like so. This will have your desired output:

@function px2vw($px) {
@return $px / 1200 * 100vw;
}

.abc {
a: 50vw + 2vw;
b: px2vw(600);
c: px2vw(600) + 2vw;
}

Returns:

.abc {
a: 52vw;
b: 50vw;
c: 52vw;
}

Lua: Decompose a number by powers of 2

Thanks to the comments from user2357112, I've got it fixed, so I'm posting the answer in case anyone else comes across this issue:

function powerfind(n)
local powers = {}
i = 1
while i <= n do
if bit.band(i, n) ~= 0 then -- bitwise and check
table.insert(powers, i)
end
i = bit.shl(i, 1) -- bitwise shift to the left
end
return powers
end

Find keys and decompose it into BCNF

Assuming 'wiki' as relation R and its attributes url,title,..heading_pos to be A,B,...H respectively.

We have,

R = {A,B,C,D,E,F,G,H}

FD's = {A->BC, E->F, AH->G}

The key here is ADEH.

We can first convert the relation R to 3NF and then to BCNF.

To convert a relation R and a set of functional dependencies(FD's) into 3NF you can use Bernstein's Synthesis. To apply Bernstein's Synthesis -

  • First we make sure the given set of FD's is a minimal cover
  • Second we take each FD and make it its own sub-schema.
  • Third we try to combine those sub-schemas

For example in your case:

First we check whether the FD's is a minimal cover (singleton right-hand side , no extraneous left-hand side attribute, no redundant FD)

  • Singleton RHS: We write the FD's with singleton RHS. So now we have FD's as {A->B, A->C, E->F, AH->G}
  • No extraneous LHS attribute: We remove the extraneous LHS attribute if any. There are no extraneous LHS attributes here.
  • No redundant FD's: We remove the redundant dependencies if any. There are no redundant dependencies here.

Second we make each FD its own sub-schema. So now we have - (the keys for each relation are in bold)

R1={A,B}

R2={A,C}

R3={E,F}

R4={A,H,G}

Third we combine all sub-schemas with the same LHS. So now we have -

S1 = {A,B,C}

S2 = {E,F}

S3 = {A,H,G}

Since none of the above decomposed relations contain contain a key of R, we need to create an additional relation schema that contains attributes that form of a key of R. This is to ensure lossless join decomposition that preserves dependencies. So we add -

S4 = {A,D,E,H}

This is in 3NF. Now to check for BCNF we check if any of these relations (S1,S2,S3,S4) violate the conditions of BCNF (i.e. for every functional dependency X->Y the left hand side (X) has to be a superkey) . In this case none of these violate BCNF and hence it is also decomposed to BCNF.

Note - The importance of some of these steps may not be clear in this example. Have a look at other examples here and here.

Singleton not working

You have to define the static m_singleton field like so in your Singleton.cpp file:

Singleton::m_instance = NULL;

But I would rewrite your Instance() method like so:

Singleton* Singleton::Instance()
{
static Singleton instance;
return &instance;
}

and remove the m_instance field altogether. This has the benefit that your singleton instance will be properly deleted when the program shuts down. As it is right now, your singleton instance will never be deleted, and more importantly, its destructor will never be called. So if you decide to add a (non-default) destructor to your Singleton class, for example to clean up some resources, it will not be called.

I would also return a reference to the singleton instead of a pointer, as it communicates the ownership semantics better than returning a pointer (which might prompt a client to delete the singleton instance!).

Making Methods All Static in Class

There is very little downside to calling new and constructing a class reference, especially if the class has no state. Allocations are fast in .NET, so I wouldn't use this alone as a justification for a class to be static.

Typically, I feel a class should be made static if the class has no specific context - if you're using the class just as a placeholder for "utility" methods or non-context specific operations, then it makes sense to be a static class.

If that class has a specific need for context, and a meaning in a concrete sense, then it probably does not justify being static, even if it has no state (although this is rare). There are times where the class purpose is defined by its reference itself, which provides "state" of a sort (the reference itself) without any local variables.

That being said, there is a big difference between a static class and a singleton. A singleton is a different animal - you want to use it when you need an instance, but only one instance, of the class to be created. There is state in a singleton, but you are using this pattern to enforce that there is only a single copy of the state. This has a very different meaning, and I would highly recommend avoiding using a singleton just to prevent needing to "call new".



Related Topics



Leave a reply



Submit