langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
char fooKey;
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id e = [[NSObject alloc] init];
// set
objc_setAssociatedObject(e, &fooKey, [NSNumber numberWithInt:1], OBJC_ASSOCIATION_RETAIN);
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, &fooKey);
NSLog(@"associatedObject: %@", associatedObject);
[e release];
[pool drain];
return 0;
}

View file

@ -0,0 +1,19 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id e = [[NSObject alloc] init];
// set
objc_setAssociatedObject(e, @selector(foo), [NSNumber numberWithInt:1], OBJC_ASSOCIATION_RETAIN);
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, @selector(foo));
NSLog(@"associatedObject: %@", associatedObject);
[e release];
[pool drain];
return 0;
}

View file

@ -0,0 +1,3 @@
% Given struct "test"
test.b=1;
test = setfield (test, "c", 3);

View file

@ -0,0 +1,70 @@
'=================
class fleximembers
'=================
indexbase 0
bstring buf, *varl
sys dp,en
method addVar(string name,dat)
sys le=len buf
if dp+16>le then
buf+=nuls 0x100 : le+=0x100 :
end if
@varl=?buf
varl[en]=name
varl[en+1]=dat
dp+=2*sizeof sys
en+=2 'next slot
end method
method find(string name) as sys
sys i
for i=0 to <en step 2
if name=varl[i] then return i+1
next
end method
method vars(string name) as string
sys f=find(name)
if f then return varl[f]
end method
method VarF(string name) as double
return vars(name)
end method
method VarI(string name) as sys
return vars(name)
end method
method vars(string name,dat)
bstring varl at buf
sys f=find(name)
if f then varl[f]=dat
end method
method delete()
sys i
sys v at buf
for i=0 to <en
freememory v[i]
next
freememory ?buf
? buf=0 : en=0 : dp=0
end method
end class
'TEST
fleximembers a
a.addVar "p",5
a.addVar "q",4.5
a.addVar "r","123456"
print a.Vars("q")+a.vars("q") 'result 4.54.5
print a.Varf("q")+a.varf("q") 'result 9
a.delete

View file

@ -0,0 +1,29 @@
declare
%% Creates a new class derived from BaseClass
%% with an added feature (==public immutable attribute)
fun {AddFeature BaseClass FeatureName FeatureValue}
class DerivedClass from BaseClass
feat
%% "FeatureName" is escaped, otherwise a new variable
%% refering to a private feature would be created
!FeatureName:FeatureValue
end
in
DerivedClass
end
class Base
feat
bar:1
meth init
skip
end
end
Derived = {AddFeature Base foo 2}
Instance = {New Derived init}
in
{Show Instance.bar} %% inherited feature
{Show Instance.foo} %% feature of "synthesized" class

View file

@ -0,0 +1,22 @@
class Bar { } # an empty class
my $object = Bar.new; # new instance
role a_role { # role to add a variable: foo,
has $.foo is rw = 2; # with an initial value of 2
}
$object does a_role; # compose in the role
say $object.foo; # prints: 2
$object.foo = 5; # change the variable
say $object.foo; # prints: 5
my $ohno = Bar.new; # new Bar object
#say $ohno.foo; # runtime error, base Bar class doesn't have the variable foo
my $this = $object.new; # instantiate a new Bar derived from $object
say $this.foo; # prints: 2 - original role value
my $that = $object.clone; # instantiate a new Bar derived from $object copying any variables
say $that.foo; # 5 - value from the cloned object

View file

@ -0,0 +1,4 @@
my $lue = 42 but role { has $.answer = "Life, the Universe, and Everything" }
say $lue; # 42
say $lue.answer; # Life, the Universe, and Everything

View file

@ -0,0 +1,5 @@
use MONKEY_TYPING;
augment class Int {
method answer { "Life, the Universe, and Everything" }
}
say 42.answer; # Life, the Universe, and Everything

View file

@ -0,0 +1,31 @@
class CSV
{
mapping variables = ([]);
mixed `->(string name)
{
return variables[name];
}
void `->=(string name, mixed value)
{
variables[name] = value;
}
array _indices()
{
return indices(variables);
}
}
object csv = CSV();
csv->greeting = "hello world";
csv->count = 1;
csv->lang = "Pike";
indices(csv);
Result: ({ /* 3 elements */
"lang",
"count",
"greeting"
})

View file

@ -0,0 +1,28 @@
lib objectclass;
define :class foo;
enddefine;
define define_named_method(method, class);
lvars method_str = method >< '';
lvars class_str = class >< '';
lvars method_hash_str = 'hash_' >< length(class_str) >< '_'
>< class_str >< '_' >< length(method_str)
>< '_' >< method_str;
lvars method_hash = consword(method_hash_str);
pop11_compile([
lvars ^method_hash = newassoc([]);
define :method ^method(self : ^class);
^method_hash(self);
enddefine;
define :method updaterof ^method(val, self : ^class);
val -> ^method_hash(self);
enddefine;
]);
enddefine;
define_named_method("met1", "foo");
lvars bar = consfoo();
met1(bar) => ;;; default value -- false
"baz" -> met1(bar);
met1(bar) => ;;; new value

View file

@ -0,0 +1,5 @@
$x = 42 `
| Add-Member -PassThru `
NoteProperty `
Title `
"The answer to the question about life, the universe and everything"

View file

@ -0,0 +1,50 @@
REBOL [
Title: "Add Variables to Class at Runtime"
Author: oofoe
Date: 2009-12-04
URL: http://rosettacode.org/wiki/Adding_variables_to_a_class_instance_at_runtime
]
; As I understand it, a REBOL object can only ever have whatever
; properties it was born with. However, this is somewhat offset by the
; fact that every instance can serve as a prototype for a new object
; that also has the new parameter you want to add.
; Here I create an empty instance of the base object (x), then add the
; new instance variable while creating a new object prototyped from
; x. I assign the new object to x, et voila', a dynamically added
; variable.
x: make object! [] ; Empty object.
x: make x [
newvar: "forty-two" ; New property.
]
print "Empty object modifed with 'newvar' property:"
probe x
; A slightly more interesting example:
starfighter: make object! [
model: "unknown"
pilot: none
]
x-wing: make starfighter [
model: "Incom T-65 X-wing"
]
squadron: reduce [
make x-wing [pilot: "Luke Skywalker"]
make x-wing [pilot: "Wedge Antilles"]
make starfighter [
model: "Slayn & Korpil B-wing"
pilot: "General Salm"
]
]
; Adding new property here.
squadron/1: make squadron/1 [deathstar-removal-expert: yes]
print [crlf "Fighter squadron:"]
foreach pilot squadron [probe pilot]

View file

@ -0,0 +1,3 @@
define: #Empty -> Cloneable clone.
define: #e -> Empty clone.
e addSlotNamed: #foo valued: 1.