Group Abstract Group Abstract

Message Boards Message Boards

How to list the supported functions on a variable?

Posted 3 years ago
POSTED BY: Bob Rick
4 Replies
Posted 3 years ago

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

Don't think about it as "list syntax". The double square brackets, [[]], is just syntactic sugar. It's just a convenient alternative to Part. Part is just a normal "function" that can be applied to any expression.

Rohit showed you how to inline the selection function. The Last function that it uses is very common. Other useful "destructuring" functions are Head, First, Most, Rest, and Part. Part has some interesting features, including things similar to Python's slice function.

When filtering/searching lists, Select, Cases, and Position are frequently used. Reorganizing lists can be done with GroupBy and GatherBy.

That's just the tip of the iceberg.

POSTED BY: Eric Rimbey
Posted 3 years ago
POSTED BY: Bob Rick
Posted 3 years ago

Hi Bob,

Can the same be written without using "fzero". i.e could the same be achieved in a single line.

trainingData // Select[Last@# == 0 &]

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

r = 1 -> 2
FullForm@r
(* Rule[1, 2] *)

Part[r, 0]
(* Rule *)

Part[r, 1]
(* 1 *)

Part [[ ]] can be used on any non-atomic symbol.

r[[1]]
(* 1 *)
POSTED BY: Rohit Namjoshi
Posted 3 years ago

"If I want to browse the images how to do it". Well, with 60K images, I'm guessing that you don't really want to browse them. Maybe you can clarify what you mean by "browse". Do you want to search, filter, iterate, etc?

"I guessed this might behave as list". There was no reason to guess this. The ResourceData output that you assigned to trainingData was clearly a list. You can see this because it was surrounded by brackets, "{" and "}".

"is there a way to list all functions supported on a variable?" This question makes me think there is a misunderstanding of how Mathematica works. Mathematica is basically an expression evaluation system. The following explanation is very rough, but hopefully helpful. When Mathematica sees an expression, it parses that expression and looks for rewrite rules that match the expression. It does this by using OwnValues, DownValues, and UpValues, and it does this in a very specific order and recursively. At some point, it will find no further evaluation rules and it stops. This might terminate in something that we might interpret as an "answer", like an integer value. But it can also terminate in something that to us looks only partially evaluated. It can also generate messages and errors in the process. So, a short answer to your question is that all functions are supported on all expressions.

Another way to think about this is that Mathematica doesn't have anything analogous to static type checking. So variables don't have types, and so they don't have a specific set of "supported functions". So another answer to your question is simply "no", there is no way to programmatically determine the intended semantics of an expression and thereby know what "functions it supports". However, by defining your own rewrite rules (typically via SetDelayed) and then using pattern matching and/or conditional constraints, you can precisely specify which expressions "work" in the sense of evaluating to a satisfying terminal expression. So there is a sense in which you can realize strong typing for your own custome "types".

You can inspect an expression in various ways. You can use Head to determine the head of the expression, the outermost wrapper, the thing that might sort of seem like a type. FullForm and TreeForm can help you understand the structure of an expression. There are other "functions" that help you extract parts, apply functions to subexpressions, search, etc. So there are many ways to interactively build an understanding of an expression that you're looking at. But if you're wanting something programmatic that will take an arbitrary expression and give you back a list of "things you can do to this expression that will result in a satisfying terminal expression", then this is effectively impossible (not literally impossible, because you have access to all of the DownValues and so forth, but I seriously doubt that this is the kind of thing you were asking about).

If this didn't answer your question, maybe you can provide a fuller example that shows the kind of thing you would like to be able to do.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard