How would a function FunctionQ
look like, maybe in a way I can even specify the number of arguments allowed?
相关问题
- Puzzled by Function body evaluation
- how to overload Times and Plus for matrix multipli
- AOSP Build TARGET_PRODUCT fails
- Modifying a Graphics3D object generated by Paramet
- Mathematica “AppendTo” function problem
相关文章
- histogram without vertical lines in Mathematica
- Entering data with Input[] in mathematica
- Using Fold to calculate the result of linear recur
- How should I write a function to be used in Apply
- NMinimize eats all memory b/c of unnecessary symbo
- Mathematica: set default value for argument to non
- how does mathematica determine which rule to use f
- Animate the movement of a point along the plot of
I really feel bad posting after Simon and Daniel, but their codes fail on non-functions which are not symbols. Checking for that and adding a check for builtins via
NumericFunction
, as suggested by Simon, we arrive at something likewhich should work in some (sigh) real-world cases
If you know the signature of the function you are looking for (i.e. how many arguments and of what type), I would agree with Simon that the way to go is duck typing:
Apply
the function to typical arguments, and look for valid output. Caching might be worthwhile:As Daniel said, his test (which probably should read)
Is quick and dirty. It will fail for built in functions, e.g.
FunctionQ[Sin]
will return False (Many built-in functions will be caught by checking for theAttribute
NumericFunction
). It will also fail for things likef[x_][y_]
etc... It should probably also testUpValues
,SubValues
and maybeNValues
(see here for their meanings).This problem was discussed in this thread. Many useful ideas are in this thread - eg ways to find the number of arguments that some functions can take, but there was no real consensus reached in the discussion.
I think that the best approach is a kind of duck typing. You probably know how many and what type of arguments you want your function to take, so test it with ValueQ. Then make sure that you catch errors using Check.
EDIT: Another comp.soft-sys.math.mathematica thread.
Here's something quick and dirty which may do what you need: