Is it possible to parse min and max functions? - math.net

As per title, is it possible to parse and evaluate the min and max functions?
As example:
Expression e = Infix.ParseOrThrow("min(L,B)");
It throws:

Related

Does Python implement short-circuiting in built-in functions such as min()?

Does Python 3 implement short-circuiting in built-in functions whenever possible, just like it does for boolean statements?
A specific example, take the below code snippet:
min((20,11), key = lambda x : x % 10) # 20
Does Python evaluate beforehand that the minimum value possible of the function passed as the key argument is 0, and therefore stops right after evaluating the first integer in the iterable passed (20) as 20 % 10 is equal to 0?
Or does it have to evaluate all the elements in the iterable before returning the answer?
I guess short-circuiting isn't even always possible especially for more complex functions, but what about for well-known, built-in functions or operators like %?
I couldn't find the answer in the official docs.
Thanks,
python has to evaluate all values inside the iterable because the languaje evaluate element by element, if you have in your tuple something that is not a number it will trigger an exception when try to perform the % operation. Python can not guess what is inside your list. You can test this by defining a function instead of a lambda and set debug point inside.
def my_mod(x):
import ipdb; ipdb.set_trace()
return x % 20
then call the function
min((20,11), key = my_mod)
you can do a quick error test case with
min((20,11, "s"), key = my_mod)
It will trigger an exception but first had to evaluate all the previous element in the list.

Find maximum element in an RDD in Pyspark by using map/filter

a = sc.parallelize((1,9,3,10))
I want to find the maximum element in a without using any max function.
I tried
a.filter( lambda x,y: x if x>y else y)
I am not able to compare elements in the RDD. How do I use for loop or if else condition properly in the map/filter function. Is it possible?
Thank you.
I was trying to post a different question. But not able to.
a = sc.parallelize((11,7,20,10,1,7))
I want to sort the elements in increasing order without using sort() function.
I tried:
def srt(a,b):
if a>b:
i=a
a=b
b=i
final=a.map(lambda x,y: srt(x,y))
I am not getting the required result.
I want to get
(1,7,7,10,11,20)
thank you.
You cannot find the max/min using filters. You may achieve that using comparison in a reduce operation:
a = sc.parallelize([1,9,3,10])
max_val = a.reduce(lambda a, b: a if a > b else b)
The lambda just compares and returns the bigger of 2 values.

How to write a function max_and_min that accepts a tuple containing integer elements as an argument?

Functions can only return a single value but sometimes, we may want functions to return multiple values. Tuples can come in handy in such cases. We can create a tuple containing multiple values and return the tuple instead of a single value.
Write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a tuple containing the largest and smallest value, in that order.
for example: max_and_min((1, 2, 3, 4, 5)) = (5, 1)
I am told to use an iteration to loop through each value of the tuple parameter to find the maximum and minimum values. Also, I must use Python 3.x.
How do I do this? I am really clueless. Thanks for your help!
def max_and_min(values):
# Write your code here
You are looking to pass a variable number of arguments to a function. In python, you can get multiple arguments passed at invocation with the * notation:
def max_and_min(*arg):
return (max(arg), min(arg))
Note that the Python 3 min and max functions themselves accept a variable number of arguments.

Haskell calling functions within functions

I have a function called bounds1Accum. this function works great
bounds1Accum :: [Integer] -> Integer -> Integer -> (Integer, Integer)
bounds1Accum l min max = (minimum (min:l), maximum (max:l))
What I am having problems with is I need another function called bounds1 which will take in just a list and return a min max pair. This function is supposed to wrap around bounds1accum and check to first see if the list being passed to it is empty or not. if the list is empty it will return Nothing or it will Just return the result of calling the bounds1Accum function.
I need two cases and for my signature I have
bounds1 :: [l] -> Maybe(min,max)
bounds1 [] = Nothing (which I am unsure if this will be correct)
then the 2nd case is where I am stumped
I originally had
bounds1 l min max = if null l then Nothing else Just $ bounds1 bounds1Accum min max
but this does not even compile so if anyone can offer suggestions or another way I can look at this problem it would be great. This is for an assignment so I do not want the answer but more so just guidance or help solving the problem.
Thank you
In the second case, you do not have min and max as arguments. You have to take those values from the list.
The if is unnecessary, since you already handle the empty list in the first case.
And why is there bounds1 before the use of bounds1Accum?

Groovy closure - what is happening in this code?

As a beginner groovy developer, I am trying to understand the following lines of groovy code I've inherited:
maxCount = skillsDist.findAll {it.mValue.value >= 0 }.max { it.mValue.value }.mValue.value
minCount = skillsDist.findAll { it.mValue.value >= 0 }.min { it.mValue.value }.mValue.value
The skillsDist object is a reference to a Java object of type Set<CalculationResult>. Each CalculationResult has an int field mValue.
The part I am struggling with is the closures after the max and min. Obviously, I am guessing it finds the min and max values out of the set but I need to modify this and am uncomfortable not understanding this.
Thanks!
The findAll iterater over the set. It creates a new set and adds all elements with a value bigger or equals than 0. The max operation iterates trough the subset and searches the maximum value.
The same in the second line (expect it looks for the min value).

Resources