While loop entered when True or False - recursion

When I run this function it's out put is continuous.
def func(l):
print(f' at begining not isnull{not isnull(l)}')
while not isnull(l):
print(f'in while loop isnull return is {isnull(l)}')
islat(l[1:])
return "At return."
Output that just runs on until I ctl-c.
>>> func(["this"])
At function begining: Not isnull(l) returns True
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
in while loop: Not isnull(l) returns True
At function begining: Not isnull(l) returns False
I do not understand why the while loop is entered when not isnull is True or False?

while loop will execute while the given expression is True. In your case, the given expression is not isnull . Since isnull False, not isnull is True, hence the loop executes untill isnull becomes True

Related

Passing Array parameter to Kusto user defined function and doing null check

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

if statement for if logical contains at least one TRUE (R)

I am creating an if statement for whether an output contains a specific string or not. I am using regular expression to do so.
I am using the grepl() function to investigate if the output contains the string 'Final evaluation: none (in check)' within the variable 'stockfish_response'. This then contains a logical matrix of FALSEs and TRUEs. E.g:
FALSE FALSE FALSE FALSE TRUE
grepl('Final evaluation: none \\(in check\\)', stockfish_response)
To incorporate this into an IF ELSE statement I need a logical process to check if a TRUE exists at least once in this matrix. Is there a function which will return TRUE if the logical matrix contains at least one TRUE and FALSE if the logical matrix contains no TRUEs.
For instance:
grepl_output <- grepl('Final evaluation: none \\(in check\\)', stockfish_response)
if (grepl_output == TRUE){
print('the phrase \'Final evaluation: none (in check) string \' exists')
} else {
print('the phrase \'Final evaluation: none (in check) string \' does not exist')
}
whereby grepl_output == TRUE is not just constrained to the first logical in the list and returns TRUE if any value in the list is TRUE
Could you use any?
if any(grepl_output == TRUE) {
# do something ...
}
See ?any for details.

Understanding 'and' and equal to operators in R

In R, TRUE && factor(FALSE) gives an error but TRUE && factor(FALSE) == FALSE returns TRUE. When TRUE && factor(FALSE) cannot be computed then how does R compares it with FALSE?
Also FALSE && factor(FALSE) returns FALSE but FALSE && factor(FALSE) == FALSE returns FALSE, it should return TRUE because the left hand side expression evaluates to FALSE. I tried FALSE && factor(FALSE) == TRUE but that also returns FALSE. Can someone explain the above results?
Kindly look at the operator precedence. As per the list == as highest precedence then && so the FALSE && factor(FALSE) == FALSEreturns FALSE as it first evaluates == and &&. If you want to execute && first and then == then use the proper bracket:
(FALSE && factor(FALSE)) == FALSE
And it returns TRUE. If you execute:
FALSE & factor(FALSE) == FALSE
Then first it executes factor(FALSE) == FALSE which executes to TRUE and then FALSE && TRUE so finally you will get FALSE.

Broadcast version of in() function or in operator?

Consider an array, say 0 to 4. I want to test if each element is in a list and return an array of booleans. A call to in returns a single boolean, because this left-hand side array is not an element of the right-hand side array:
> a = 0:4;
> a in [1, 2]
false
Does Julia have a broadcast version of the in() function or the in operator that returns an array like this call to map and a lambda function?
> map(x -> x in [1,2], a)
5-element Array{Bool,1}:
false
true
true
false
false
You can use broadcasting, but you have to tell Julia that the second argument should not be iterated over, so you should do:
julia> in.(a, [[1,2]])
5-element BitArray{1}:
false
true
true
false
false
or
julia> in.(a, Ref{Vector{Int}}([1,2]))
5-element BitArray{1}:
false
true
true
false
false
Both will work under Julia 0.6.3 and 0.7.
Similarly, the ∈ operator (\inTAB, synonymous with the in function) allows for broadcasting using infix notation.
julia> 0:4 .∈ [[1,2]]
5-element BitArray{1}:
false
true
true
false
false

R String, return a boolean vector of if or not the character is alphabetic

I was wondering if there is a way I can do something like this:
str = "3 .*MTNs2 AB33"
This is a string of 14 characters
I would like to have R return a vector of length 14, and true if this character is [a-zA-Z], and false if otherwise.
So it should return
FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
Is there a function that does that?
All the best,
Kathy
grepl("[A-Za-z]",unlist(strsplit(str,"")))

Resources