1
$\begingroup$

Mathematica beginner here.

I have a function that picks indices based on the value of a passed variable x. This works fine when a concrete value is picked for the variable:

Select[Range[4], # > 2 &] > {3, 4} 

However, when I compare based on an input variable x, it returns the empty set:

Select[Range[4], # > x &] > {} 

This is strange to me: How can you compare a concrete value with a variable name? I would have expected it to return an unresolved list of comparisons where x needs to be filled in. I need this since I want to create a ParametricRegion based on a function with such a selection process.

Also note that

Map[Range[n], # > x &] > {1, 2, 3, 4}[#1 > x] & 

doesn't resolve to anything.

$\endgroup$
2
  • $\begingroup$ "I have a function that picks indices based on the value of a passed variable x." -- Please show the function definition. (I strongly suspect it is not defined appropriately.) $\endgroup$ Commented Feb 22, 2022 at 16:24
  • $\begingroup$ The example was meant as a minimal reproducible example, the function itself is a bit more complex but not much. $\endgroup$ Commented Feb 22, 2022 at 16:38

2 Answers 2

0
$\begingroup$

Is this close to what you want?

Clear[MySelectFunc]; MySelectFunc[ls_List, x_?NumericQ] := Select[ls, # > x &]; SeedRandom[1]; MySelectFunc[RandomInteger[{1, 30}, 20], 12] (* {27, 22, 29, 17, 24, 15, 29, 20, 19} *) ``` 
$\endgroup$
2
  • $\begingroup$ Yes, I think so! Adding the ?NumbericQ ensures that MySelectFunc[RandomInteger[{1, 30}, 20], x] no longer evaluates to the empty set but rather to MySelectFunc[{17, 13, 1, 20, 5, 23, 22, 8, 4, 1, 5, 30, 21, 25, 4, 6, 13, 29, 20, 22}, x] :) I didn't realize Mathematica's type annotations actually change evaluation, unlike in eg Python/Typescript. $\endgroup$ Commented Feb 22, 2022 at 16:35
  • $\begingroup$ Ahh, Python-Shmython... Putting "type definitions" -- i.e. patterns matching the arguments -- like, _List and _?NumericQ, relates to one of the core, signature characteristics of Mathematica. (Pattern matching.) $\endgroup$ Commented Feb 22, 2022 at 18:50
0
$\begingroup$

Select isn't a structure-building function in the sense that you seem to expect. It simply returns a list of all elements for which the condition is true. In your case, an "unresolved" expression is not true, so none of the elements is kept.

In the case of Map, the function should be the first argument, the list should be second.

Map[# > x &, Range[4]] (*returns {1 > x, 2 > x, 3 > x, 4 > x}*) 

We can't help with your ParametricRegion until you provide more details about that.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.