Message Boards Message Boards

0
|
6943 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Isn't there any way put functions in a list?

Posted 10 years ago

I want to use a function like method in an OOP programming.

If there is a list which is named k, then I want to have a list like

k={1,3,4,a,whatever,f[x_String]:=Print[x<>"Samy"]}
k[[5]]["Hello, "]

The result I expect from the last input above is "Hello, Samy". The point is whether it is possible to deal with functions as elements in a list so that we can get the ability of OOP's method features.

Thanks. :)

POSTED BY: Dong Hyeon Ghang
3 Replies

There are number of ways to emulate OOP in Mathematica, if you search Mathematica stackexchange you'll find number of topics on this. Here is a small note on one method.

POSTED BY: Nasser M. Abbasi

Thank you for help. Your answer helped me a lot.

There are many problems with this OOP-related-features in Mathematica. And one is solved due to your explanation. Have a Good day.

POSTED BY: Dong Hyeon Ghang

Hello, There are several ways to do this. Here is one:

myPrint[x_] := "x = " <> ToString[x]
fs = {Sin, Cos, myPrint}
fs[[3]][Pi]
Through[fs[21]]
POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract