How to represent an anonymous function as a string - string

I want to represent a function like x->x^2 as a string - simply doing string(x->x^2) doesn't work, is there any way around this?

You could create a function from string using fun=eval(parse("x->x^2")) but as far as I know it's an irreversible process.

Just because something works, doesn't mean it is a solution... Suppose f is the anonymous function. With f = x->x^2. Then the following:
join(map(strip,match(r"line \d*:\n(.*)\n",string(f.code)).captures),"\n")
Gives:
return x ^ 2
More can be extracted using regular expressions from f.code. Also note this doesn't work for non-anonymous functions. And it isn't something one should rely on to work. Perhaps some other way to implement the functionality would be best.

Related

Trying to print a generator object with a short code

It probably has been asked before,
But I couldn't find something like that,
I'm trying to print a generator object:
n="12234451"
print(*[n[c]for c in range(len(n))if n[c]!=n[c-1]or c==0],sep="")
I usually use something like that, however, I'm wondering if it's the most efficient way to do it
(without changing the fact that the expression is in the print)
Thanks for your time

Matlab function defintion method that takes string as the definition (MFILE)

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:
f=//command//(x,'sin(x)');
something like this.
im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:
x= 0;
y= f(x);
and then the results were y=0;
What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:
f=#(x)(sin(x))
In case you really need to evaluate from a string:
f=str2func('#(x)sin(x)')
I would advice against the second option unless absolutely required, it can lead to hard to debug errors.
well I found the answer myself and it was "inline" command; :)
f=inline('sin(x+y+z)','x','y','z');
you can add as much variables as needed too.

Unicode name from Char

I'm looking for a function that takes a Char as input and gives the unicode name of that code point (::Char->String), but I couldn't find any results on Hoogle. I assume that there is no builtin (If there is, please let me know) and so I wonder what's the best way to write this function and its inverse (::String->Maybe Char).
I know you'd have to read UnicodeData.txt or a similar document, but I don't know what the best/fastest function would be.
The unicode-names package contains the function
getCharacterName :: Char -> String
First of all, thanks to #TwanVanLaarhoven who provided an excellent answer. I did however need a function that did the reverse of getCharacterName.
What I originally wanted was a function that would read the file and not have it hard-coded, but I eventually realized that that would require unsafe IO operations.
What I decided to do was to copy UnicodeData.txt into notepad++ and use the following regex replacements:
write module UnicodeNames (characterToName,nameToCharacter) where
paste UnicodeData.txt
replace this: ^([\dA-F]+);([^<;>]+).*$|^([\dA-F]+);(?:[^;]*;){9}([^<;>]+).*$
with this: characterToName '\\x$1$3' = "$2$4"
append characterToName _ = ""
paste again
replace this (again): ^([\dA-F]+);([^<;>]+).*$|^([\dA-F]+);(?:[^;]*;){9}([^<;>]+).*$
with this: nameToCharacter "$2$4" = Just '\\x$1$3'
append nameToCharacter _ = Nothing
replace ^.*<.*$ with nothing to remove extra lines.
The file will be incredibly long and take forever to compile :-) In addition to having an inverse function, this method has the advantage of providing more names than the unicode-names package by using unicode 1.0 names as well. The two functions in this file rely on pattern matching to act as a dictionary from char to string and vice-versa. I would put my solution on PasteBin or somewhere else if it didn't use a ton of memory.

Assigning a mathematical string expression to double variable

I have a mathematical expression in string form like:
string strExpression = "10+100+Math.Sin(90)";
I want to simply assign this expression (at run time) to a float variable (say result), so that it becomes the following code statement:
float result = 10+100+Math.Sin(90);
How can I do this?
You have to compile the expression within a syntactically correct code block. See http://devreminder.wordpress.com/net/net-framework-fundamentals/c-dynamic-math-expression-evaluation/ as an example.
Edit: Or alternatively write your own expression parser if the expression is going to be VERY simple (I wouldn't recommend this though)
You could use CS-Script to dynamically make a class with a method that you can run, if you don't want to write your own parser but rather use C# which you allready know..

Can IDL evaluate strings as code?

Is there any functionality in IDL that will allow it to evaluate a a string as code?
Or, failing that, is there a nice, dynamic way of including /KEYWORD in functions? For example, if I wanted to ask them for what type of map projection the user wants, is there a way to do it nicely, without large if/case statements for the /Projection_Type keyword it needs?
With even a small number of user options, the combinations would cause if/case statements to get out of hand very quickly to handle all the possible options.
The best bet is to use a case statement because you can't trust that your user is going to type in the same string for Projection_Type that you're expecting as in the keyword.
Though if you are set on doing something like this, there is the EXECUTE function that treats a string as an IDL statement:
Result = EXECUTE(String [, QuietCompile] [, QuietExecution])
Edited to add, there's also CALL_FUNCTION and CALL_PROCEDURE that are faster but maybe less flexible. Look them all up in the IDL help and see what works for you.

Resources