Map-like array function in Excel? - excel

Is there something like a map function in Excel?
For example, let's say I want to apply the formula ABS to a list of items. Something like:
=ABS,{-1,2,3,4,5}
As an example of python-like notation:
# list comprehension
>>> [abs(i) for i in [-1,2,3,4,5]]
[1, 2, 3, 4, 5]
# map
>>> map(abs,[-1,2,3,4,5])
[1, 2, 3, 4, 5]
# passing arbitrary function
>>> map(lambda x:abs(x)+1, [-1,2,4,5,6])
[2, 3, 5, 6, 7]
Is there something similar in Excel?
Note, the closest I've gotten is using native operators against lists, such as =2*{1,2,3,4,5}, but this just gives things like the basic arithmetic operations.

In Excel-365 you can directly use ABS() function with array of data. Try-
=ABS({-1,2,3,4,5})

You can use the MAP function in Excel with a similar notation to other languages. Note however, that you must pass it a LAMBDA function, not just an in-line formula. Here then would be your answer:
=MAP({-1,2,3,4,5},LAMBDA(x, ABS(x)))

Related

How to understand the `cmp::Reverse` Example in std docs

I am confused about this example in the std::cmp::Reverse.
use std::cmp::Reverse;
let mut v = vec![1, 2, 3, 4, 5, 6];
v.sort_by_key(|&num| (num > 3, Reverse(num)));
// v.sort_by_key(|&num| Reverse(num)); I know this means reverse the order.
assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
How to understand the tuple (num > 3, Reverse(num)) in this example when sorting? What does the first element in the tuple mean? I guess the tuple here means sort by the first element first, if equal, then sort by the second element. If so, any docs about this?
What does the first element in the tuple mean? I guess the tuple here means sort by the first element first, if equal, then sort by the second element. If so, any docs about this?
Yes.
https://doc.rust-lang.org/std/primitive.tuple.html
The sequential nature of the tuple applies to its implementations of various traits. For example, in PartialOrd and Ord, the elements are compared sequentially until the first non-equal set is found.

What kind of comprehension is this?

One of my students found that, for ell (a list of string) and estr (a string), the following expression is True iff a member of ell is contained in estr:
any(t in estr for t in ell)
Can anyone explain why this is syntactically legal, and if so what does the comprehension generate?
This is a generator expression.
func_that_takes_any_iterable(i for i in iterable)
It's like a list comprehension, but a generator, meaning it only produces one element at a time:
>>> a = [i for i in range(10)]
>>> b = (i for i in range(10))
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(b)
<generator object <genexpr> at 0x7fb9113fae40>
>>> print(list(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(list(b))
[]
When using a generator expression in isolation, the parentheses are required for syntax reasons. When creating one as an argument to a function, the syntax allows for the extra parentheses to be absent.
Most functions don't care what type of iterable they're given - list, tuple, dict, generator, etc - and generators are perfectly valid. They're also marginally more memory-efficient than list comprehensions, since they don't generate the entire thing up front. For all() and any(), this is especially good, since those methods short-circuit as soon as they would return False and True respectively.
As of python 3.8, the syntactic restrictions for the walrus operator := are similar - in isolation, it must be used inside its own set of parentheses, but inside another expression it can generally be used without them.
This is syntactically legal because it's not a list. Anything of the form (x for x in array) is a generator. Think of it like lazy list, which will generate answer only when you ask.
Now generator are also an iterable so it's perfectly fine for it to be inside an any() function.
willBeTrue = any(True for i in range(20))
So this will basically generate 20 Trues and any function looks for any true value; thus will return True.
Now coming to your expression:
ans = any(t in estr for t in ell)
This {t in estr} returns a boolean value. Now generator makes len(ell) amount of those boolean value and any thinks good.. since atleast one is true I return True.

Explanation required for the below python code

What will be output for the below code
for i in range(5):
continue
print(i)
Since the variable is out of scope, why does the above code works. Also why does the value of i restricts to 4?
It's iterating through the range, which is [0, 1, 2, 3, 4] and doing nothing. "i" is the iterator, and the last one is "4", so you're printing it.
The variable is not out of scope. Local variables in python are available everywhere in the function it is defined in. If it's not in a function, then the variable has global scope. This was probably done because, there is no need to declare variable in python, like we do in C or Java. Note that any variable you use inside the function is also available outside the loop.
For example:
for foo in range(10):
bar = 2
print(foo, bar)
This will print 9 2.
As for your second question, most things in python have inclusive lower bounds and exclusive upper bounds. That means, range(5) is actually [0, 1, 2, 3, 4].
You can verify this by running print(list(range(5))).
If you want the range 1-5, you can use range(1, 6).

How does the groovy range operator behave wiht other operators? What is the operator precedence?

I'm staring at a short line of groovy code, and I can't understand it.
Input:
1..<2+1+(3..<4)
Output:
[1, 2, 3, 4, 5]
How do I start to parse this? 1..<2+1 results in [1, 2] which is fine. But then I can't make sense of the later part. For example, where does the 5 come from?
An interesting example! It's confusing at first but the range operator has low precedence, so that the first range operator is actually being evaluated last.
It's evaluated as:
1..< (2 + 1 + (3..<4))
which, as the range 3..<4 is just 3, becomes
1..< (2 + 1 + 3)
finally giving
1..<6
which is [1, 2, 3, 4, 5] as you found.
The expression leads to a runtime error if the second range generates a list instead of a single value.
https://ideone.com/YTNBLm
Groovy's operator precedence is documented here: https://groovy-lang.org/operators.html

Mathematica Position of Elements of a List that fulfill an inequality

The task I have is pretty simple but I can not solve it in mathematica.
Given a list
myList = {1, 3, 4}
I would like to get the position of entries smaller than a number - say 2 in the example above.
Attempts such as
Position[myList, #[[1]] < 2 &]
Position[myList, # < 2 &]
which would be similar to the function SELECT don't work. How can I use Position or some other function. Thanks!
Reason: The reason is that Position takes a pattern not a function.
(i.e. Position[-list-,-pattern-])
Solution:
Position[myList, x_ /; x < 2]
{{1}}
Similarly:
myList2 = {1, 2, 3, 4, 5, 1, "notNumber"}
Position[myList2, x_ /; x < 3]
{{1}, {2}, {6}}
(i.e. Position[ myList, element_x where element_x < 2])
/; <-- denotes a condition (Super useful when defining functions over specific inputs too!)
x_ <-- is a named "pattern object"
x <-- is a reference to the pattern object
Deeper Reason:
I don't know exactly what the Mathematica internals look like, but I imagine it runs something like this: if you use a functional description instead of a pattern description (i.e. #...& instead of x_/;...) the function looks for patterns that contain "#...&" which doesn't make sense (since it's comparing objects not feeding them to your defined function). On the other hand when you use a pattern description it compares them, then checks the conditional for truth (the conditional limiting matches, the widely defined x_ matching everything) and you get meaningful matching. Flip all that for functions defined to work with other functions.
I love Mathematica, but it's not good at making it's pattern based functions and function based functions obviously separate from eachother (aside from looking at documentation).
Hope that helps.

Resources