How GOTO statement in Groovy? - groovy

I saw this nice blog post about a Scala continuations that 'emulates' a GOTO statement in the Scala language. (read more about Continuations here)
I would like to have the same in the programming language Groovy. I think it's possible within a Groovy compiler phase transformation.
I'm working on an Domain-Specific Language (DSL), and preferred embedded in Groovy. I would like to have the GOTO statement, because the DSL is an unstructured language (and is generated from workflow diagrams). I need a 'labeled' goto statement, not to line numbers.
The DSL is a language for workflow definitions, and because there are no restrictions for the arrows between nodes, a goto is needed. (or unreadable code with while etc)
As a beginner of Groovy and Scala I don't know If I can translate the Scala solution to Groovy, but I don think there are continuations in Groovy.
I'm looking for an algorithm/code for emulating labeled goto's in Groovy. One algorithm I had in mind is using eval repeatedly; doing the eval when your are at a goto.
The DSL is evaluated with an eval already.
I'm not looking for a 'while' loop or something, but rather translating this code so that it works (some other syntax is no problem)
label1:
a();
b();
goto label1;
PS:
I don't prefer the discussion if I should really use/want the GOTO statement. The DSL is a specification-language and is probably not coping with variables, efficiency etc.
PS2: Some other keyword then GOTO can be used.

You might want to tell a little bit more about the language you are trying to build, perhaps it's simple enough that dealing with transformations would be overengineering.
Playing with the AST is something groovy people have been doing for years and it's really powerful.
The spock framework guys rewrite the tests you create annotating the code with labels. http://code.google.com/p/spock/
Hamlet D'Arcy has given several presentations on the matter. Several posts can also be found on his blog. http://hamletdarcy.blogspot.com/
Cedric Champeau describes an interesting transformation he built and its evolution http://www.jroller.com/melix/
Probably missing lots of other guys but those I remember.
A possible starting points that you probably already know but are really useful. http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations
http://groovy.codehaus.org/Building+AST+Guide
Long story short, I'd say its quite possible

You won't get anywhere trying this, as goto is a reserved word in Groovy (as it is in Java), so using it in your DSL will be problematic.
It's not a reserved word in Scala, so this isn't an issue

You can emulate if and goto with while loops. It won't be pretty, it will introduce lots of unnecessary code-blocks, but it should work for any function. There is some proof that this is always possible to rewrite code like that, but of course being possible does not mean it's nice or easy.
Basically you move all local variables to the beginning of the function and add a bool takeJump local variable. Then add a while(takeJump){+} pair for any goto+label pair and set the flag before the while and before the end of the while to the value you want.
But to be honest I don't recommend that approach. I'd rather use a library that allows me to build an AST with labels and gotos and then translates that directly to byte-code.
Or use some other language built on the java vm that does support goto. I'm sure there is such a language.

Just throwing this out there, perhaps you could have a scoped switch case
So if your DSL says this:
def foo() {
def x = x()
def y
def z
label a:
y = y(x)
if(y < someConst) goto a
label b:
z = y(z)
if(z > someConst) goto c
x = y(y(z+x))
z = y(x)
label c:
return z;
}
Your "compiler" can turn it into this:
def foo() {
String currentLABEL = "NO_LABEL"
while(SCOPED_INTO_BLOCK_0143) {
def x
def y
def z
def retval
switch(currentLABEL) {
case "NO_LABEL":
x = x()
case "LABEL_A"
y = y(x)
if(y < someConst) {
currentLABEL = "LABEL_A"
break
}
case "LABEL_B"
z = y(z)
if(z > someConst) {
currentLabel = "LABEL_C"
break
}
x = y(y(z+x))
z = y(x)
case "LABEL_C"
SCOPED_INTO_BLOCK_0143 = false
retval = z
}
}
return retval
}

Related

Simple check in haskell

I am new to haskel.What would be a good way of doing something like this in haskell?
var1 = //can be true or false
if(var1==true)
{
//return someething
}
else
{
//
}
Haskell is a functional and declarative language. That means that usually that there is not much "do something". There is more calculate something and return it.
That may look like nitpicking, but for instance in Haskell one cannot set a variable twice: once you assign it an expression (not per se a value)
, you cannot set it to a different value.
If you want to return something, you usually work with pattern matching. For instance:
f :: Bool -> String
f True = "Yes"
f False = "No"
This would be somewhat equivalent in Java/C#/... to:
public String f (boolean var1) {
if(var1) {
return "Yes";
} else {
return "No";
}
}
Note that Haskell works lazy as well: if you return a function call or anything, you do not immediately evaluate that function call: a call is only evaluated if that is necessary.
A problem might arise how to do I/O. For that, there is the concept of an I/O monad. A monad is a functional programming technique that enforces a certain order of evaluation.
But functional programming thus requires a different "mindset" than imperative programming: you do not think of a program in terms of commands that are done one after another, but more in terms of composing functions together to generate output for a given input. Like usually a mathematician or physicist does. You compose for instance a function that, given the mass and the velocity of something, calculates the kinetic energy of that object.
Haskell has if-then-else conditionals.
The closest code to yours I can write is something like this:
let var = length "hello" == 5
in if var then "ok" else "no"
Note that such conditional is more similar to C or Java's var ? "ok" : "no" expression than an if()... statement, but this is to be expected since Haskell is functional, so it has no "statements", only expressions.
Any Haskell tutorial should cover this. I'd recommend you read one, if you want to learn Haskell. Trying to convert idioms from other languages is a poor strategy.

Control structures beyond standard conditionals and loops?

Structured programming languages typically have a few control structures, like while, if, for, do, switch, break, and continue that are used to express high-level structures in source code.
However, there are many other control structures that have been proposed over the years that haven't made their way into modern programming languages. For example, in Knuth's paper "Structured Programming with Go To Statements," page 275, he references a control structure that looks like a stripped-down version of exception handling:
loop until event1 or event2 or ... eventN
/* ... */
leave with event1;
/* ... */
repeat;
then event1 -> /* ... code if event1 occurred ... */
event2 -> /* ... code if event2 occurred ... */
/* ... */
eventN -> /* ... code if eventN occurred ... */
fi;
This seems like a useful structure, but I haven't seen any languages that actually implement it beyond as a special case of standard exception handling.
Similarly, Edsger Dijkstra often used a control structure in which one of many pieces of code is executed nondeterministically based on a set of conditions that may be true. You can see this on page 10 of his paper on smoothsort, among other places. Sample code might look like this:
do
/* Either of these may be chosen if x == 5 */
if x <= 5 then y = 5;
if x >= 5 then y = 137;
od;
I understand that historically C influenced many modern languages like C++, C#, and Java, and so many control structures we use today are based on the small set offered by C. However, as evidenced by this other SO question, we programmers like to think about alternative control structures that we'd love to have but aren't supported by many programming languages.
My question is this - are there common languages in use today that support control structures radically different from the C-style control structures I mentioned above? Such a control structure doesn't have to be something that can't be represented using the standard C structures - pretty much anything can be encoded that way - but ideally I'd like an example of something that lets you approach certain programming tasks in a fundamentally different way than the C model allows.
And no, "functional programming" isn't really a control structure.
Since Haskell is lazy, every function call is essentially a control structure.
Pattern-matching in ML-derived languages merges branching, variable binding, and destructuring objects into a single control structure.
Common Lisp's conditions are like exceptions that can be restarted.
Scheme and other languages support continuations which let you pause and resume or restart a program at any point.
Perhaps not "radically different" but "asynchronous" control structures are fairly new.
Async allows non-blocking code to be executed in parallel, with control returning to the main program flow once completed. Although the same could be achieved with nested callbacks, doing anything non-trivial in this way leads to fugly code very quickly.
For example in the upcoming versions of C#/VB, Async allows calling into asynchronous APIs without having to split your code across multiple methods or lambda expressions. I.e. no more callbacks. "await" and "async" keywords enable you to write asynchronous methods that can pause execution without consuming a thread, and then resume later where it left off.
// C#
async Task<int> SumPageSizesAsync(IList<Uri> uris)
{
int total = 0;
var statusText = new TextBox();
foreach (var uri in uris)
{
statusText.Text = string.Format("Found {0} bytes ...", total);
var data = await new WebClient().DownloadDataTaskAsync(uri);
total += data.Length;
}
statusText.Text = string.Format("Found {0} bytes total", total);
return total;
}
(pinched from http://blogs.msdn.com/b/visualstudio/archive/2011/04/13/async-ctp-refresh.aspx)
For Javascript, there's http://tamejs.org/ that allows you to write code like this:
var res1, res2;
await {
doOneThing(defer(res1));
andAnother(defer(res2));
}
thenDoSomethingWith(res1, res2);
C#/Python iterators/generators
def integers():
i = 0
while True:
yield i
i += 1
(I don't know a lot about the subject so I marked this a wiki)
Haskell's Pattern Matching.
Plain example:
sign x | x > 0 = 1
| x == 0 = 0
| x < 0 = -1
or, say, Fibonacci, which looks almost identical to the math equation:
fib x | x < 2 = 1
| x >= 2 = fib (x - 1) + fib (x - 2)

What's the name of this programming feature?

In some dynamic languages I have seen this kind of syntax:
myValue = if (this.IsValidObject)
{
UpdateGraph();
UpdateCount();
this.Name;
}
else
{
Debug.Log (Exceptions.UninitializedObject);
3;
}
Basically being able to return the last statement in a branch as the return value for a variable, not necessarily only for method returns, but they could be achieved as well.
What's the name of this feature?
Can this also be achieved in staticly typed languages such as C#? I know C# has ternary operator, but I mean using if statements, switch statements as shown above.
It is called "conditional-branches-are-expressions" or "death to the statement/expression divide".
See Conditional If Expressions:
Many languages support if expressions, which are similar to if statements, but return a value as a result. Thus, they are true expressions (which evaluate to a value), not statements (which just perform an action).
That is, if (expr) { ... } is an expression (could possible be an expression or a statement depending upon context) in the language grammar just as ?: is an expression in languages like C, C# or Java.
This form is common in functional programming languages (which eschew side-effects) -- however, it is not "functional programming" per se and exists in other language that accept/allow a "functional like syntax" while still utilizing heavy side-effects and other paradigms (e.g. Ruby).
Some languages like Perl allow this behavior to be simulated. That is, $x = eval { if (true) { "hello world!" } else { "goodbye" } }; print $x will display "hello world!" because the eval expression evaluates to the last value evaluated inside even though the if grammar production itself is not an expression. ($x = if ... is a syntax error in Perl).
Happy coding.
To answer your other question:
Can this also be achieved in staticly typed languages such as C#?
Is it a thing the language supports? No. Can it be achieved? Kind of.
C# --like C++, Java, and all that ilk-- has expressions and statements. Statements, like if-then and switch-case, don't return values and there fore can't be used as expressions. Also, as a slight aside, your example assigns myValue to either a string or an integer, which C# can't do because it is strongly typed. You'd either have to use object myValue and then accept the casting and boxing costs, use var myValue (which is still static typed, just inferred), or some other bizarre cleverness.
Anyway, so if if-then is a statement, how do you do that in C#? You'd have to build a method to accomplish the goal of if-then-else. You could use a static method as an extension to bools, to model the Smalltalk way of doing it:
public static T IfTrue(this bool value, Action doThen, Action doElse )
{
if(value)
return doThen();
else
return doElse();
}
To use this, you'd do something like
var myVal = (6 < 7).IfTrue(() => return "Less than", () => return "Greater than");
Disclaimer: I tested none of that, so it may not quite work due to typos, but I think the principle is correct.
The new IfTrue() function checks the boolean it is attached to and executes one of two delegates passed into it. They must have the same return type, and neither accepts arguments (use closures, so it won't matter).
Now, should you do that? No, almost certainly not. Its not the proper C# way of doing things so it's confusing, and its much less efficient than using an if-then. You're trading off something like 1 IL instruction for a complex mess of classes and method calls that .NET will build behind the scenes to support that.
It is a ternary conditional.
In C you can use, for example:
printf("Debug? %s\n", debug?"yes":"no");
Edited:
A compound statement list can be evaluated as a expression in C. The last statement should be a expression and the whole compound statement surrounded by braces.
For example:
#include <stdio.h>
int main(void)
{
int a=0, b=1;
a=({
printf("testing compound statement\n");
if(b==a)
printf("equals\n");
b+1;
});
printf("a=%d\n", a);
return 0;
}
So the name of the characteristic you are doing is assigning to a (local) variable a compound statement. Now I think this helps you a little bit more. For more, please visit this source:
http://www.chemie.fu-berlin.de/chemnet/use/info/gcc/gcc_8.html
Take care,
Beco.
PS. This example makes more sense in the context of your question:
a=({
int c;
if(b==a)
c=b+1;
else
c=a-1;
c;
});
In addition to returning the value of the last expression in a branch, it's likely (depending on the language) that myValue is being assigned to an anonymous function -- or in Smalltalk / Ruby, code blocks:
A block of code (an anonymous function) can be expressed as a literal value (which is an object, since all values are objects.)
In this case, since myValue is actually pointing to a function that gets invoked only when myValue is used, the language probably implements them as closures, which are originally a feature of functional languages.
Because closures are first-class functions with free variables, closures exist in C#. However, the implicit return does not occur; in C# they're simply anonymous delegates! Consider:
Func<Object> myValue = delegate()
{
if (this.IsValidObject)
{
UpdateGraph();
UpdateCount();
return this.Name;
}
else
{
Debug.Log (Exceptions.UninitializedObject);
return 3;
}
};
This can also be done in C# using lambda expressions:
Func<Object> myValue = () =>
{
if (this.IsValidObject) { ... }
else { ... }
};
I realize your question is asking about the implicit return value, but I am trying to illustrate that there is more than just "conditional branches are expressions" going on here.
Can this also be achieved in staticly
typed languages?
Sure, the types of the involved expressions can be statically and strictly checked. There seems to be nothing dependent on dynamic typing in the "if-as-expression" approach.
For example, Haskell--a strict statically typed language with a rich system of types:
$ ghci
Prelude> let x = if True then "a" else "b" in x
"a"
(the example expression could be simpler, I just wanted to reflect the assignment from your question, but the expression to demonstrate the feature could be simlpler:
Prelude> if True then "a" else "b"
"a"
.)

Are there any programming languages where variables are really functions?

For example, I would write:
x = 2
y = x + 4
print(y)
x = 5
print(y)
And it would output:
6 (=2+4)
9 (=5+4)
Also, are there any cases where this could actually be useful?
Clarification: Yes, lambdas etc. solve this problem (they were how I arrived at this idea); I was wondering if there were specific languages where this was the default: no function or lambda keywords required or needed.
Haskell will meet you halfway, because essentially everything is a function, but variables are only bound once (meaning you cannot reassign x in the same scope).
It's easy to consider y = x + 4 a variable assignment, but when you look at y = map (+4) [1..] (which means add 4 to every number in the infinite list from 1 upwards), what is y now? Is it an infinite list, or is it a function that returns an infinite list? (Hint: it's the second one.) In this case, treating variables as functions can be extremely beneficial, if not an absolute necessity, when taking advantage of laziness.
Really, in Haskell, your definition of y is a function accepting no arguments and returning x+4, where x is also a function that takes no arguments, but returns the value 2.
In any language with first order functions, it's trivial to assign anonymous functions to variables, but for most languages you'll have to add the parentheses to indicate a function call.
Example Lua code:
x = function() return 2 end
y = function() return x() + 4 end
print(y())
x = function() return 5 end
print(y())
$ lua x.lua
6
9
Or the same thing in Python (sticking with first-order functions, but we could have just used plain integers for x):
x = lambda: 2
y = lambda: x() + 4
print(y())
x = lambda: 5
print(y())
$ python x.py
6
9
you can use func expressions in C#
Func<int, int> y = (x) => x + 5;
Console.WriteLine(y(5)); // 10
Console.WriteLine(y(3)); // 8
... or ...
int x = 0;
Func<int> y = () => x + 5;
x = 5;
Console.WriteLine(y()); // 10
x = 3;
Console.WriteLine(y()); // 8
... if you are really wanting to program in a functional style the first option would probably be best.
it looks more like the stuff you saw in math class.
you don't have to worry about external state.
Check out various functional languages like F#, Haskell, and Scala. Scala treats functions as objects that have an apply() method, and you can store them in variables and pass them around like you can any other kind of object. I don't know that you can print out the definition of a Scala function as code though.
Update: I seem to recall that at least some Lisps allow you to pretty-print a function as code (eg, Scheme's pretty-print function).
This is the way spreadsheets work.
It is also related to call by name semantics for evaluating function arguments. Algol 60 had that, but it didn't catch on, too complicated to implement.
The programming language Lucid does this, although it calls x and y "streams" rather than functions.
The program would be written:
y
where
y = x + 4
end
And then you'd input:
x(0): 2
y = 6
x(1): 5
y = 7
Of course, Lucid (like most interesting programming languages) is fairly obscure, so I'm not surprised that nobody else found it. (or looked for it)
Try checking out F# here and on Wikipedia about Functional programming languages.
I myself have not yet worked on these types of languages since I've been concentrated on OOP, but will be delving soon once F# is out.
Hope this helps!
The closest I've seen of these have been part of Technical Analysis systems in charting components. (Tradestation, metastock, etc), but mainly they focus on returning multiple sets of metadata (eg buy/sell signals) which can be then fed into other functions that accept either meta data, or financial data, or plotted directly.
My 2c:
I'd say a language as you suggest would be highly confusing to say the least. Functions are generally r-values for good reason. This code (javascript) shows how enforcing functions as r-values increases readability (and therefore maintenance) n-fold:
var x = 2;
var y = function() { return x+2; }
alert(y());
x= 5;
alert(y());
Self makes no distinction between fields and methods, both are slots and can be accessed in exactly the same way. A slot can contain a value or a function (so those two are still separate entities), but the distinction doesn't matter to the user of the slot.
In Scala, you have lazy values and call-by-name arguments in functions.
def foo(x : => Int) {
println(x)
println(x) // x is evaluated again!
}
In some way, this can have the effect you looked for.
I believe the mathematically oriented languages like Octave, R and Maxima do that. I could be wrong, but no one else has mentioned them, so I thought I would.

In what languages can you dynamically rewrite functions on the fly?

I recently had the necessity of rewriting a javascript function in javascript, dynamically. The ease with which I did it, and how fun it was, astounded me.
Over here I've got some HTML:
<div id="excelExport1234"
onclick="if(somestuff) location.href='http://server/excelExport.aspx?id=56789&something=else'; else alert('not important');"
>Click here to export to excel</div>
And I couldn't change the outputted HTML, but I needed to add an extra parameter to that link. I started thinking about it, and realized I could just do this:
excelExport = $('excelExport1234');
if (needParam)
eval('excelExport.onclick = ' + excelExport.onclick.toString().replace("excelReport.aspx?id", "excelReport.aspx?extraParam=true&id") + ';');
else
eval('excelExport.onclick = ' + excelExport.onclick.toString().replace("extraParam=true&", "") + ';');
And it worked like a champ! excelExport.onclick returns a function object which I convert to a string, and do some string manip on. Since it's now in the form of "function() { ... }", I just go back and assign it to the onclick event of the dom object. It's a little ugly having to use eval, but AFAIK there isn't a javascript function constructor that can take a string of code and turn it into an object nicely.
Anyway, my point isn't that I'm super clever (I'm not), my point is that this is cool. And I know javascript isn't the only language that can do this. I've heard that lisp has had macros for years for this exact purpose. Except to really grok macros you need to really grok lisp, and I don't grok it, I just 'kind of get it'.
So my question is: In what other languages can you (easily) dynamically rewrite functions, and can you show me a simple example? I want to see where else you can do this, and how it's done!
(also, I have no idea what to tag this as, so I took random guesses)
LISP is the ultimate language at this. LISP functions are actual LISP lists, meaning you can manipulate LISP source code as if it were any other data structure.
Here's a very trivial example of how it works:
(define hi
(lambda () (display "Hello World\n")))
;; Displays Hello World
(hi)
(set! hi
(lambda () (display "Hola World\n")))
;; Displays Hola World
(hi)
This, however, is possible in any language where functions are first-class objects. One of the most interesting showcases of the power of this syntax for LISP is in its macro system. I really don't feel I could do the topic justice, so read these links if you're interested:
http://en.wikipedia.org/wiki/Macro_(computer_science)#Lisp_macros
http://cl-cookbook.sourceforge.net/macros.html
I guess it depends on what exactly you define as "easily dynamic rewriting". For example in .Net you have the Func type and lambdas which allows you to define functions as variables or as temporary anonymous functions eg.
int[] numbers = {1, 2, 3, 4, 5};
Func<int[], int> somefunc;
if (someCondition)
{
somefunc = (is => is.Sum());
} else {
somefunc = (is => is.Count());
}
Console.WriteLine(somefunc(numbers).ToString());
The above is a very contrived example of either counting the items in an array of integers or summing then using dynamically created functions subject to some arbitrary condition.
Note - Please don't point out that these things can be easily accomplished without lambdas (which they obviously can) I was simply trying to write a very simple example to demonstrate the concept in C#
Self-modifying code is also called degenerate code. This is generally considered a bad thing, and it used to be a goal of high-level languages to prevent it from being written easily.
This is from the wikipedia entry:
Self-modifying code is seen by some as a bad practice which makes code harder to read and maintain. There are however ways in which self modification is nevertheless deemed acceptable, such as when sub routine pointers are dynamically altered - even though the effect is almost identical to direct modification.
I think that it is the case in most of dynamic languages. Here is an example in Python
def f(x):
print x
def new_function(x): print "hello", x
f("world")
f = new_function
f("world")
The output is
world
hello world
I think that such technique should be used carefully
Scheme allows you to do that.
(define (salute-english name) (display "Hello ") (display name))
(define (salute-french nom) (display "Salut ") (display nom))
Now you redefine a fonction by assigning the salute variable to the right function, either salute-english or salute-french, like this:
(define salute salute-english)
(define (redefined-the-salute-function language)
(if (eq? language 'french)
(set! salute salute-french)
(set! salute salute-english)))
More generaly functional programming language allows you to do that or as functions are first class value. Functions can be manipulated, passed around, sometimes assigned to variables and so on. The list then include: Lisp, Scheme, Dylan, OCaml and SML. Some languages having first class functions includes Python, Ruby, Smalltalk and i think Perl.
Note that when you have an interactive language where you can interactively type your program, the redefinition of functions/methods must be possible: the REPL has to be able to do that, just in case you happen to retype the definition of an already defined functions.
I used to do this all the time in TCL, it was a breeze and worked wonderfully. I could investigate somethings interface over the network and then create a custom-made interface on the fly to access and control things. For example, you could make a custom SNMP interface from a generic SNMP library.
I haven't used it, but C# has some built-in support for generating it's own byte-code, which is fairly impressive.
I've done this sort of thing in C as well, but there it is non-portable and almost never worth the hassle. It is a technique used sometimes for "self-optimizing" code to generate the appropriate C function to optimally process a given data set.
You could do it in C++, but it wouldn't be easy, safe, or recommended.
Generate the text of the source code
invoke the compiler (fork & exec) to build a dynamic library. In gcc, you can pass the source code you want to compile on standard input, it doesn't have to be in a file.
Load the library (LoadLibrary() on windows, dlopen() on linux)
get a function pointer to whatever function you want (GetProcAddress() on windows, dlsym() on linux)
If you want to replace an existing function, if it's a virtual function you could modify the v-table to point to the new function (that part especially is a horrible idea fraught with peril). The location of the v-table or the format of it isn't part of the C++ standard, but all the toolchains I've used have been consistent within themselves, so once you figure out how they do it, it probably won't break.
Easy enough in Perl.
*some_func = sub($) {
my $arg = shift;
print $arg, "\n";
};
some_func('foo');
Re Sam Saffron's request:
*hello_world = sub() {
print "oops";
};
hello_world();
*hello_world = sub() {
print "hello world";
};
hello_world();
In PLSQL:
create or replace procedure test
as
begin
execute immediate '
create or replace procedure test2
as
begin
null;
end;
';
end;
/
Here's something else in Python (in addition to luc's answer), which I am not recommending, but just to show it - there is exec, which can execute a string which you could build to be whatever code...
I/O shown here is from a Python 2.5.2 interpreter session. Just some simple examples of constructing strings to execute from substrings (>>> is the interpreter prompt)...
>>> def_string = 'def my_func'
>>> param_string_1 = '():'
>>> param_string_2 = '(x):'
>>> do_string_1 = ' print "Do whatever."'
>>> do_string_2 = ' print "Do something with", x'
>>> do_string_3 = ' print "Do whatever else."'
>>> do_string_4 = ' print "Do something else with", x'
>>> def_1 = '\n'.join([def_string+param_string_1, do_string_1, do_string_3])
>>> print def_1
def my_func():
print "Do whatever."
print "Do whatever else."
>>> exec def_1
>>> my_func()
Do whatever.
Do whatever else.
>>> def_2 = '\n'.join([def_string+param_string_2, do_string_2, do_string_4])
>>> print def_2
def my_func(x):
print "Do something with", x
print "Do something else with", x
>>> exec def_2
>>> my_func('Tom Ritter')
Do something with Tom Ritter
Do something else with Tom Ritter
>>>
Trivial in Ruby:
def hello_world; puts "oops"; end
hello_world
# oops
def hello_world; puts "hello world"; end
hello_world
# hello world
Of course that example is boring:
require "benchmark"
# why oh _why
class Object
def metaclass; class << self; self; end; end
def meta_eval &blk; metaclass.instance_eval &blk; end
end
class Turtle
end
def make_it_move(klass)
klass.send(:define_method, :move) { |distance|
puts "moving #{distance} meters"
sleep(0.1 * distance)
}
end
make_it_move(Turtle)
turtle = Turtle.new
turtle.move(1)
# moving 1 meters
def profile(instance, method)
instance.meta_eval do
m = instance_method(method)
define_method method do |*a|
puts "Benchmarking #{instance.class} #{method}"
puts Benchmark.measure {
m.bind(instance).call(*a)
}
end
end
end
profile(turtle, :move)
turtle.move(10)
# Benchmarking Turtle move
# moving 10 meters
# 0.000000 0.000000 0.000000 ( 1.000994)
Turtle.new.move(3)
# moving 3 meters
The code above:
Defines a blank class
Adds a method to it
Grabs an instance
Intercepts that method on that instance only
Changing what a function does is supported in a lot of languages, and it's not as complicated as you might think. In functional languages, functions are values, and function names are symbols that are bound to them like any variable. If the language allows you to reassign the symbol to a different function, this is trivial.
I think the more interesting features are the ability to get the source code for a function (toString above) and to create a new function from a string (eval in this case).

Resources