Check if value in a Sass Map exists - css

In the following SASS-code snippet I want to check if a map has a specific key. If not, it returns a default value (in this case #bbb)
#function contains($list, $var) {
#return (false == index($list, $var));
}
$colors: (aaa: #aaa, bbb: #bbb);
$out: if(contains($colors, aaa), map-get($colors, aaa), #bbb);
body {
color: $out;
}
For some reason it always returns #bbb. Any suggestions what I'm doing wrong here ?

The first issue is that the return value is null if the value isn't found:
#function contains($list, $var) {
#return (null == index($list, $var));
}
The second issue relates to the semantics of your contains function. Usually a function called contains should return true if the value was found in the list and false otherwise (at least I would expect that):
#function contains($list, $var) {
#return (null != index($list, $var));
}
Then there is a third issue with calling the index function on a map. You would have to pass a key-value pair not just the key to this function:
$out: if(contains($colors, aaa #aaa), map-get($colors, aaa), #bbb);
Now it works as expected but for the sake of completeness let me tell you that there is already a built-in function which does exactly what you try to achieve with your contains-function; the map_has_key($map, $key):
$out: if(map-has-key($colors, aaa), map-get($colors, aaa), #bbb);

Related

R: How to create a function with if-statements to search for a file in multiple paths

The code below is what I usually use in Python. Now I want to do this in R, but I can't find an example of the documentation of if statements in a def. Is this even possible?
def loadfile():
if 'test.csv' in os.listdir():
return 'test.csv'
elif os.path.exists('../../project1/data/'):
return '../../project1/data/test.csv'
elif os.path.exists('../../../project1/data/'):
return '../../../project1/data/test.csv'
Thanks a lot!!
If statements are build like following example in R:
if (condition) {
statement
} else if (condition) {
...
} else {
...
}
You can use file.exists and dir.exists for your checks.
For the if statements, you need parentheses around the Boolean clause and curly braces for the body. You do not need to specify a return(value) directly, because R will automatically return the last variable evaluated in a function.
Bringing it together:
loadfile <- function() {
if (file.exists('test.csv')) {
'test.csv'
} else if (dir.exists('../../project1/data')) {
'../../project1/data/test.csv'
} else if (dir.exists('../../../project1/data')) {
'../../../project1/data/test.csv'
}
}

Firebase Firestore rule that allows either undefined or max length of 600 characters

I'm trying to get a rule that will allow a value to not be set at all(undefined) or to pass another test, in this case, a max length of 600 characters.
My current rules are set as follows:
function incomingData() {
return request.resource.data;
}
function hasNotMaxSizeOfDescriptionExceeded() {
return !incomingData().description || incomingData().description.size() <= 600;
}
Ok, I figured it out with the line:
!incomingData().keys().hasAll(['description'])
You can check if a prop. exists in the incoming collection
In order to check that a property is either undefined or matches another test you could use the in operator:
function incomingData() {
return request.resource.data;
}
function hasNotMaxSizeOfDescriptionExceeded() {
return !('description' in incomingData()) || incomingData().description.size() <= 600;
}

how to follow a recursive isBST function?

I'm trying to follow a specific implementation of a recursive (in-order traversal) isBST function.
I tried to simulate it with the simple invalid BST example:
root=5, root.left=3, root.left.right=8
5
/
3
\
8
i'm getting either 'true' or 'false' but without having to check the last node '8'.
the code is as follows:
/* wrapper that keeps track of the previous value */
class PrevWrapper {
int data = Integer.MIN_VALUE;
}
boolean isBST(Node root, PrevWrapper prev) {
/* base case: we reached null*/
if (root == null) {
return true;
}
if(!isBST(root.left, prev)) {
return false;
}
/* If previous in-order node's data is larger than
* current node's data, BST property is violated */
if (prev.data > root.data) {
return false;
}
/* set the previous in-order data to the current node's data*/
prev.data = root.data;
return isBST(root.right, prev);
}
boolean isBST(Node root) {
return isBST(root, new PrevWrapper());
}
what is the correct order of returning the recursive calls?
EDIT:
I was thinking:
(#)isBST(5,min), isBST(3,min), isBST(3.left=null,min) - returns true, skips to prev=3, isBST(8,3), isBST(8.left=null,3) - returns true, skips to prev=8, isBST(8.right=null,8) - returns true to (#), checks 8 > 5? returns false.
I think I got it, is this the correct order of calls?
thanks in advance.

Having a second helper call inside a helper, overwrites the first helper value in handlebars

If I have helpers like:
Handlebars.registerHelper("testHelper", function (v) {
console.log(v);
}
Handlebars.registerHelper("testHelper2", function (v) {
return v;
}
and have two subhelpers like:
{{testHelper first=(testHelper2 '1') second=(testHelper2 '2')}}
both first and second are returning '2'. The console output is:
data: {},
hash: {
first: '2',
second: '2'
}
How would I make it return the correct values? If I do the following it returns first as 1:
{{testHelper first=(testHelper2 '1')}}
Does anyone have a workaround for this? Please note that I made the helpers simple and wouldn't use a helper to return the same value normally.
Here is a fiddle example.
The only way that I see is to not use a hash as parameter for first helper. Instead of it use arguments directly.
Helper:
Handlebars.registerHelper("testHelper", function () {
var args = Array.prototype.slice.call(arguments, 0);
return 'first: '+args[0]+' second: '+args[1];
});
Template:
{{testHelper (testHelper2 '1') (testHelper2 '2')}}
Fiddle.

Groovy :: Map Find Recursive

Edit
See #tim's solution below for the "correct" Groovy-esque approach to map recursion. Since Map findRecursive does not yet exist in Groovy, if you find yourself needing this functionality in various parts of your app, just add it to Map metaClass:
Map.metaClass.findRecursive = {String key->
if(delegate.containsKey(key)) return delegate."$key"
else
for(m in delegate) {
if(m.value in Map) return m.value.findRecursive(key)
}
}
// then anywhere in your app
someMap.findRecursive('foo')
Original
Was hoping something like findResult{it.key=='foo'} would recurse through map elements beyond 1-d deep, but appears not to be the case.
Rolled my own recursive map finder, but am wondering if there's a better way to do this. Maybe there's a built-in function I'm missing, or an even Groovier (concise) way to pull off the below:
Map map = [school:[id:'schoolID', table:'_school',
children:[team:[id:'teamID',table:'_team',
children:[player:[id:'playerID',table:'_roster']]
]]
]]
class Foo {
static finder = {Map map, String key->
if(map.containsKey(key)) return map[key]
else
for(m in map) {
if(m.value in Map) return this.finder(m.value,key)
}
}
}
println Foo.finder(map,'team')
With Groovy 1.8 (reqd for the findResult method), you could do something like this:
class DeepFinder {
static Object findDeep( Map map, Object key ) {
map.get( key ) ?: map.findResult { k, v -> if( v in Map ) v.findDeep( key ) }
}
}
use( DeepFinder ) {
println map.findDeep( 'team' )
}
There's no recursing default Groovy method that I know of...

Resources