I am trying to have optional parameter (string array). So, My parameter for a function looks like f(names:dynamic=dynamic([])) to set default if i am not passing this parameter. However below check doen't work. Any pointers on how to do null check and if present apply that filter else retrieve all values.
| where isempty(_x) or x in(_x)
you could try using set_has_element().
for example:
let T = range x from 1 to 5 step 1
;
let F = (_x: dynamic = dynamic(null)) {
T
| where isnull(_x) or set_has_element(_x, x)
| order by x desc
}
;
F(dynamic([1,2,4]))
x
4
2
1
Though, please note the following, regarding stored functions: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions#features-that-are-currently-unsupported-by-user-defined-functions
1.
x in (_x) is a valid syntax
let _x = dynamic([1,2,4]);
print is_2_in_x = 2 in (_x)
,is_3_in_x = 3 in (_x)
is_2_in_x
is_3_in_x
true
false
Fiddle
2.
Figuring out if array is null/empty is complicated and the use of isnull() and/or isempty() is not sufficient for the task.
Please note the 3 last options in the demo below for potential solutions.
let F = (_x_as_str:string = "", _x: dynamic = dynamic(null))
{
print _x_as_str = _x_as_str
,isnull = isnull(_x)
,isempty = isempty(_x)
,array_length = array_length(_x)
,is_1st_null = isnull(_x[0])
,is_1st_empty = isempty(_x[0])
,coalesce_array_length = coalesce(array_length(_x), 0)
}
;
union F("dynamic([1,2,4])",dynamic([1,2,4]))
,F()
,F("dynamic(null)", dynamic(null))
,F("dynamic([])" , dynamic([]))
_x_as_str
isnull
isempty
array_length
is_1st_null
is_1st_empty
coalesce_array_length
dynamic([1,2,4])
false
false
3
false
false
3
true
true
true
true
0
dynamic([])
false
false
0
true
true
0
dynamic(null)
true
true
true
true
0
Fiddle
Related
First, I have a function called permeability.
# permeabiliy function
# L is short for the Lable
mu_0 = 4 * pi * 10^(-7);
mu_r_core = 50;
mu_r_air = 1;
L = Int16;
function permeability(L)
if L in 1:4
if L !== 3
return mu = mu_r_air * mu_0
else
return mu = mu_r_core * mu_0
end
else
println("null") #print output in a new line
end
end
Then, I have a matrix called domain, which is shown below,
domain
2392-element Array{Int16,1}:
1
1
3
1
...
When I called permeability(domain[3]), the output is,
L = domain[3]
permeability(L)
the output is
1.2566370614359177e-6
However, when I simply called permeability(3),
permeability(3)
the output is
6.283185307179588e-5
So, it seems that the value passed from matrix domain is just "1", but in this case, domain[3] should be 3 and the result should be the same in these 2 cases.
Can someone please tell me where I was wrong?
The problem is that your array stores Int16 while 3 is an Int64. L!==3 requires L to be of the same type as 3 ie Int64. You wanted instead L!=3. Your confusion probably comes from the fact that != is the inverse of == while !== is the inverse of ===
Title says most of it. Here is a related thread, but I do not have enough reputation to comment.
Here is the prompt:
A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a function called is_power that takes parameters a and b and returns True if a is a power of b.
In the archived post, the most upvoted answer was to address the trivial base cases of (x,x) and (1,x). Then, determine if (a % b == 0) and then recursively call to find (a/b is a power of b).
Here is the code I've written in Julia:
function ispower(a,b)
if a == 0 && b != 0
return false
elseif a == b
return true
elseif a % b == 0 && ispower(a/b, b)
return true
else
return false
end
end
As I wrote in the comment if you multiply 1 by 1 you can only get 1, so the only number that is a power of 1 is 1 and no other number. Therefore, your function should return false for an input (x,1), whenever x is not 1.
Then you should also consider the case where b==0. Then the only number that is a power of b is 0.
So, I would write you function like this:
function ispower(a::Integer,b::Integer)
if (a==b)
return true
elseif (a==0 || b==0 || b==1)
return false
elseif (a % b) == 0
return ispower(div(a,b), b)
else
return false
end
end
I am using Julia and I've designed a for loop that takes the outputs of a function in one loop and uses them as the input of that function in the next loop (and over and over). When I run this code, Julia flags an "undefined" error, however, if I run the code in debug mode, it executes perfectly. For example, the code looks like this:
function do_command(a,b,c,d)
a = a + 1
b = split(b, keepempty=false)[1]
c = split(b, keepempty=false)[1]
if a == 1000
d = true
else
d = false
end
return a, b, c, d
end
for ii in 1:length(x)
if ii == 1
a = 0
b = "string something"
c = ""
d = false
end
a,b,c,d = do_command(a,b,c,d)
if d == true
print(string(b))
break
end
end
What am I doing wrong here?
An issue with your code is that for introduces a new scope for each iteration of the loop. That is to say: variable a created within the loop body at iteration 1 is not the same as variable a created within the loop body at iteration 2.
In order to fix your problem, you should declare variables outside the loop, so that at each iteration, references to them from within the loop body would actually refer to the same variables from the enclosing scope.
I'd go with something like this:
function do_command(a,b,c,d)
a = a + 1
b = split(b, keepempty=false)[1]
c = split(b, keepempty=false)[1]
if a == 1000
d = true
else
d = false
end
return a, b, c, d
end
# Let's create a local scope: it's good practice to avoid global variables
let
# All these variables are declared in the scope introduced by `let`
a = 0
b = "string something"
c = ""
d = false
for ii in 1:10 #length(x)
# now these names refer to the variables declared in the enclosing scope
a,b,c,d = do_command(a,b,c,d)
if d == true
print(string(b))
break
end
end
end
I'm trying to write a basic function to take an integer and evaluate to a bool that will check whether the integer is a prime or not.
I've used an auxiliary function to keep track of the current divisor I'm testing, like so:
fun is_divisible(n : int, currentDivisor : int) =
if currentDivisor <= n - 1 then
n mod currentDivisor = 0 orelse is_divisible(n, currentDivisor + 1)
else
true;
fun is_prime(n : int) : bool =
if n = 2 then
true
else
not(is_divisible(n, 2));
It looks right to me but I test it on 9 and get false and then on 11 and get false as well.
Sorry for all the questions today and thanks!
The problem is that if your is_divisible reaches the last case it should return false because it means that all the iterated divisors have resulted in a remainder larger than zero except for the last one which is the number it self. So you should rename is_divisible and return false instead of true
I can't seem to find the resource I need. What does && do in a code that is comparing variables to determine if they are true? If there is a link with a list of the symbol comparisons that would be greatly appreciated.
example: Expresssion 1: r = !z && (x % 2);
In most programming languages that use &&, it's the boolean "and" operator. For example, the pseudocode if (x && y) means "if x is true and y is true."
In the example you gave, it's not clear what language you're using, but
r = !z && (x % 2);
probably means this:
r = (not z) and (x mod 2)
= (z is not true) and (x mod 2 is true)
= (z is not true) and (x mod 2 is not zero)
= (z is not true) and (x is odd)
In most programming languages, the operator && is the logical AND operator. It connects to boolean expressions and returns true only when both sides are true.
Here is an example:
int var1 = 0;
int var2 = 1;
if (var1 == 0 && var2 == 0) {
// This won't get executed.
} else if (var1 == 0 && var2 == 1) {
// This piece will, however.
}
Although var1 == 0 evaluates to true, var2 is not equals to 0. Therefore, because we are using the && operator, the program won't go inside the first block.
Another operator you will see ofter is || representing the OR. It will evaluate true if at least one of the two statements are true. In the code example from above, using the OR operator would look like this:
int var1 = 0;
int var2 = 1;
if (var1 == 0 || var2 == 0) {
// This will get executed.
}
I hope you now understand what these do and how to use them!
PS: Some languages have the same functionality, but are using other keywords. Python, e.g. has the keyword and instead of &&.
It is the logical AND operator
(&&) returns the boolean value true if both operands are true and returns false otherwise.
boolean a=true;
boolean b=true;
if(a && b){
System.out.println("Both are true"); // Both condition are satisfied
}
Output
Both are true
The exact answer to your question depends on the which language your are coding in. In R, the & operator does the AND operation pairwise over two vectors, as in:
c(T,F,T,F) & c(T,T,F,F)
#> TRUE FALSE FALSE FALSE
whereas the && operator operated only on the first element of each vector, as in:
c(T,F,T,F) && c(T,T,F,F)
#> TRUE
The OR operators (| and ||) behave similarly. Different languages will have different meanings for these operators.
In C && works like a logical and, but it only operates on bool types which are true unless they are 0.
In contrast, & is a bitwise and, which returns the bits that are the same.
Ie. 1 && 2 and 1 && 3 are true.
But 1 & 2 is false and 1 & 3 is true.
Let's imagine the situation:
a = 1
b = 2
if a = 1 && b = 2
return "a is 1 and b is 2"
if a = 1 && b = 3
return "a is 1 and b is 3"
In this situation, because a equals 1 AND b = 2, the top if block would return true and "a is 1 and b is 2" would be printed. However, in the second if block, a = 1, but b does not equal 3, so because only one statement is true, the second result would not be printed. && Is the exact same as just saying and, "if a is 1 and b is 1".