tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,5 @@
Invoke an object method where the name of the method to be invoked can be generated at run time.
;Cf:
* [[Respond to an unknown method call]].
* [[Runtime evaluation]]

View file

@ -0,0 +1,2 @@
---
note: Object oriented

View file

@ -0,0 +1,13 @@
obj := {mA: Func("mA"), mB: Func("mB"), mC: Func("mC")}
InputBox, methodToCall, , Which method should I call?
obj[methodToCall].()
mA(){
MsgBox Method A
}
mB(){
MsgBox Method B
}
mC(){
MsgBox Method C
}

View file

@ -0,0 +1,3 @@
for name in ["foo", "bar"] {
E.call(example, name, [])
}

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"reflect"
)
type example struct{}
// the method must be exported to be accessed through reflection.
func (example) Foo() int {
return 42
}
func main() {
// create an object with a method
var e example
// get the method by name
m := reflect.ValueOf(e).MethodByName("Foo")
// call the method with no argments
r := m.Call(nil)
// interpret first return value as int
fmt.Println(r[0].Int()) // => 42
}

View file

@ -0,0 +1,12 @@
procedure main()
x := foo() # create object
x.m1() # static call of m1 method
# two examples where the method string can be dynamically constructed ...
"foo_m1"(x) # ... need to know class name and method name to construct name
x.__m["m1"] # ... general method (better)
end
class foo(a,b,c) # define object
method m1(x)
end
end

View file

@ -0,0 +1,18 @@
import java.lang.reflect.Method;
class Example {
public int foo(int x) {
return 42 + x;
}
}
public class Main {
public static void main(String[] args) throws Exception {
Object example = new Example();
String name = "foo";
Class<?> clazz = example.getClass();
Method meth = clazz.getMethod(name, int.class);
Object result = meth.invoke(example, 5); // result is int wrapped in an object (Integer)
System.out.println(result); // prints "47"
}
}

View file

@ -0,0 +1,7 @@
example = new Object;
example.foo = function(x) {
return 42 + x;
};
name = "foo";
example[name](5) # => 47

View file

@ -0,0 +1,5 @@
funName = 'foo'; % generate function name
feval (funNAME, ...) % evaluation function with optional parameters
funName = 'a=atan(pi)'; % generate function name
eval (funName, 'printf(''Error\n'')')

View file

@ -0,0 +1,23 @@
#import <Foundation/Foundation.h>
@interface Example : NSObject { }
- (NSNumber *)foo;
@end
@implementation Example
- (NSNumber *)foo {
return [NSNumber numberWithInt:42];
}
@end
int main (int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id example = [[Example alloc] init];
SEL selector = @selector(foo); // or = NSSelectorFromString(@"foo");
NSLog(@"%@", [example performSelector:selector]);
[example release];
[pool release];
return 0;
}

View file

@ -0,0 +1,2 @@
foo()=5;
eval(Str("foo","()"))

View file

@ -0,0 +1,15 @@
<?php
class Example {
function foo($x) {
return 42 + $x;
}
}
$example = new Example();
$name = 'foo';
echo $example->$name(5), "\n"; // prints "47"
// alternately:
echo call_user_func(array($example, $name), 5), "\n";
?>

View file

@ -0,0 +1,3 @@
my $object = 42 but role { method add-me($x) { self + $x } }
my $name = 'add-me';
say $object."$name"(5); # 47

View file

@ -0,0 +1,12 @@
package Example;
sub new {
bless {}
}
sub foo {
my ($self, $x) = @_;
return 42 + $x;
}
package main;
my $name = "foo";
print Example->new->$name(5), "\n"; # prints "47"

View file

@ -0,0 +1 @@
(send (expression) Obj arg1 arg2)

View file

@ -0,0 +1,3 @@
string unknown = "format_nice";
object now = Calendar.now();
now[unknown]();

View file

@ -0,0 +1,6 @@
class Example(object):
def foo(self, x):
return 42 + x
name = "foo"
getattr(Example(), name)(5) # => 47

View file

@ -0,0 +1,6 @@
(define foo -> 5)
(define execute-function
Name -> (eval [(INTERN Name)]))
(execute-function "foo")

View file

@ -0,0 +1,14 @@
class Example
def foo
42
end
def bar(arg1, arg2, &block)
block.call arg1, arg2
end
end
symbol = :foo
Example.new.send symbol # => 42
Example.new.send( :bar, 1, 2 ) { |x,y| x+y } # => 3
args = [1, 2]
Example.new.send( "bar", *args ) { |x,y| x+y } # => 3

View file

@ -0,0 +1,11 @@
class Example
private
def privacy; "secret"; end
public
def publicity; "hi"; end
end
e = Example.new
e.public_send :publicity # => "hi"
e.public_send :privacy # raises NoMethodError
e.send :privacy # => "secret"

View file

@ -0,0 +1,9 @@
Object subclass: #Example.
Example extend [
foo: x [
^ 42 + x ] ].
symbol := 'foo:' asSymbol. " same as symbol := #foo: "
Example new perform: symbol with: 5. " returns 47 "

View file

@ -0,0 +1,14 @@
package require Tcl 8.6
oo::class create Example {
method foo {} {return 42}
method 1 {s} {puts "fee$s"}
method 2 {s} {puts "fie$s"}
method 3 {s} {puts "foe$s"}
method 4 {s} {puts "fum$s"}
}
set eg [Example new]
set mthd [format "%c%c%c" 102 111 111]; # A "foo" by any other means would smell as sweet
puts [$eg $mthd]
for {set i 1} {$i <= 4} {incr i} {
$eg $i ...
}