Why don't Sass functions work outside selectors? - function

I have an example.scss file. When it is imported (parsed), I would like to push a value to a map.
// Declare selectors
register(example);
Prior to its import, the following has already been imported:
$example-map: (
);
#function register($key){
$pointless: map-set($example-map, $key, true); // Have to assign to a variable
#return true; // Have to return something
}
This is invalid as it appears that functions cannot be called outside of selectors. Why is this?
Codepen here

You can use a mixin instead. Sure it's a little more to type but it should work.
Here's my quick test:
$register: ();
#mixin register ($key) {
#if not index($register, $key) {
$register: append($register, $key);
}
}
#include register(foo);
#include register(bar);
#include register(bar);
#include register(baz);
body {
content: $register;
}
Then you can test if it's registed just using index.
#if index($register, foo) {
.foo {
color: red;
}
}
Here's a small demo:
Sassmeister: http://sassmeister.com/gist/6069ebaba50d29052eeb

Related

Checking for a maximum level of indentation in Checkstyle

I want to ensure that my code does not exceed 5 levels of indentation, like so:
class Foo { // 0
void bar() { // 1
if () { // 2
if () { // 3
if () { // 4
if () { // 5
if () { // 6 - error
}
}
}
}
}
}
}
Looking through the Checkstyle documentation, I couldn't find a rule that specifically implements this. Does such a rule exist, or do I have to look into custom rules?
There is an issue still in "Open" state about this feature: https://github.com/checkstyle/checkstyle/issues/5487
Not exactly what you need, but you can achieve something similar using the NestedForDepth, NestedIfDepth and NestedTryDepth checks.

What does this Binary search tree function do?

I was wondering what does this function do and what could be the possible output of this code?
void TreeType::Function()
{
Queue<TreeNode*> q;
TreeNode* node;
if (root!= NULL) {
q.Enqueue(root);
do {
q.Dequeue(node);
cout << node->info << endl;
if (node->left)
{ q.Enqueue(node->left); }
if (node->right)
{ q.Enqueue(node->right); }
while (!q.IsEmpty()); }
this function is like level order traversal of any tree.
while there are many ending semi colons missing.

Passing operator to setInterval() actionscript

I would like assign setInterval() to pass an operator as an argument by following the Answer of LiraNuna
function actualFunction(passedValue:Number, compareFunction:Function) {
/* ... */
if(compareFunction(passedValue, staticValue)) {
/* ... Do something ... */
}
/* ... */
}
actualFunction(6, function(x:Number, y:Number) {
return x > y;
});
from this link pass < or > operator into function as parameter?
But I don't seem to know how to do it since only the function name is called when initiating setInterva().
Typical initiation:
function actualFunction(passedValue:Number, compareFunction:Function) {
/* ... */
if(compareFunction(passedValue, staticValue)) {
/* ... Do something ... */
}
/* ... */
}
setInterval(actualFunction,10)
Now, I want to assign
actualFunction(6, function(x:Number, y:Number) {
return x > y;
});
within setInterval(), how would I do it?
Try this (not tested) :
setInterval(actualFunction, 10, 6, function(x:Number, y:Number){return x > y});
Read this for more information : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#setInterval()
Hope this helps.

Share a function between two passes inside CG Shader for unity3d

I'm writing a shader in CG language for Unity3d.
If you make a shader for transparent object you need to create two similar passes in SubShader. The first one to render only back faces (with Cull Front) and the second one to render only front faces (with Cull Back). But the code for vertex and fragment function is the same for two passes.
Is it possible not to double a code and declare some functions, that would be shared between passes?
I want to have something like in my code example:
Shader "cool shader" {
Properties {
...
}
SubShader {
CGPROGRAM
// need to declare vertexOutput somewhow here
float4 sharedFragFoo(vertexOutput i) : COLOR // How to make smth like this?
{
....
return float4(...);
}
ENDCG
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
vertexOutput vert(vertexInput v) {
vertexOutput o;
...
return o;
}
float4 frag(vertexOutput i) : COLOR
{
return sharedFragFoo(i); // call the shared between passes function
}
ENDCG
}
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
vertexOutput vert(vertexInput v) {
vertexOutput o;
...
return o;
}
float4 frag(vertexOutput i) : COLOR
{
return sharedFragFoo(i); // call the shared between passes function
}
ENDCG
}
}
}
UPD: Found out how to do it using includes.
But is it possible to do inside one file?
You can do it in one file using CGINCLUDE. If you look at the shader for MobileBlur ("Hidden/FastBlur") by Unity it has shared code at the top and passes below.
Here are just the key parts - note CGINCLUDE/ENDCG outside of the SubShader/Pass
Shader "YourShader"
{
...
CGINCLUDE
#include "UnityCG.cginc"
struct shared_v2f
{
float4 pos : SV_POSITION;
}
shared_v2f myVert( appdate_img v )
{
shared_v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 myFrag( shared_v2f i ) : SV_Target
{
return fixed4( 1.0, 0.5, 0.0, 1.0 );
}
ENDCG
SubShader
{
...
Pass
{
CGPROGRAM
#pragma vertex myVert
#pragma fragment myFrag
ENDCG
}
}
}
Answering my own question. Wierdo!
Hope it will help somebody else.
You can write all that betwee CGPROGRAM and ENDCG in separate *.cginc file and include it inside each pass.
Important! But you need to write
#pragma vertex vert and #pragma fragment frag inside your main shader file, otherwise it will compile but won't work. I suppose that the reason is that pragma'ss are processed before include's.
Here is my code sample.
Main shader definition file:
Shader "cool shader" {
Properties {
// properties
}
SubShader {
...
pass {
Cull Front
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "shared.cginc"
ENDCG
}
pass {
Cull Back
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "shared.cginc"
ENDCG
}
}
}
Shared file shared.cginc:
#ifndef SHARED_FOO
#define SHARED_FOO
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform float4 _Color;
// other variables....
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float4 posInObjectCoords : TEXCOORD2;
float3 normalDir : TEXCOORD3;
};
vertexOutput vert(vertexInput v) {
vertexOutput o;
// do staff
return o;
}
float4 frag(vertexOutput i) : COLOR
{
// do staff
return float4(...);
}
#endif // SHARED_FOO

Jinja2 automatic creation of prefix whitespace

In StringTemplate - which I've used in some projects to emit C code - whitespace prefixes are automatically added in the output lines:
PrintCFunction(linesGlobal, linesLocal) ::= <<
void foo() {
if (someRuntimeFlag) {
<linesGlobal>
if (anotherRuntimeFlag) {
<linesLocal>
}
}
}
>>
When this template is rendered in StringTemplate, the whitespace
prefixing the multilined linesGlobal and linesLocal strings,
is copied for all the lines emitted. The generated C code is
e.g.:
void foo() {
if (someRuntimeFlag) {
int i;
i=1; // <=== whitespace prefix copied in 2nd
i++; // <=== and 3rd line
if (anotherRuntimeFlag) {
int j=i;
j++; // <=== ditto
}
}
}
I am new to Jinja2 - and tried to replicate this, to see if I can use Python/Jinja2 to do the same thing:
#!/usr/bin/env python
from jinja2 import Template
linesGlobal='\n'.join(['int i;', 'i=1;'])
linesLocal='\n'.join(['int j=i;', 'j++;'])
tmpl = Template(u'''\
void foo() {
if (someRuntimeFlag) {
{{linesGlobal}}
if (anotherRuntimeFlag) {
{{linesLocal}}
}
}
}
''')
print tmpl.render(
linesGlobal=linesGlobal,
linesLocal=linesLocal)
...but saw it produce this:
void foo() {
if (someRuntimeFlag) {
int i;
i=1;
if (anotherRuntimeFlag) {
int j=i;
j++;
}
}
}
...which is not what I want.
I managed to make the output emit proper whitespace prefixes with this:
...
if (someRuntimeFlag) {
{{linesGlobal|indent(8)}}
if (anotherRuntimeFlag) {
{{linesLocal|indent(12)}}
}
}
...but this is arguably bad, since I need to manually count whitespace
for every string I emit...
Surely Jinja2 offers a better way that I am missing?
There's no answer (yet), because quite simply, Jinja2 doesn't support this functionality.
There is, however, an open ticket for this feature - if you care about it, join the discussion there.