My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2009, Justin Donaldson and the haXe Project Contributors
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

/*
Glue provides a simple method of attaching two functions together. The
various function calls (joinVoid, join1, join2) refer to the arity of the
functions that are being joined. The result of a join function is a new
function. The function calls the first function argument (func1) and saves
the result. Then it calls the second function (func2) as a side effect,
discarding the result. It returns the saved result from func1.
All arguments are typed, so both functions (func1 and func2) must match
argument arity. However, return type can differ, since the result of func2
is discarded.
*/

package org.sugar.sigslot;

class Glue{

/*
Join two nullary matched functions together and return them as a new "glued"
function.
*/
public static function joinVoid<A,B>(func1:Void->A, func2:Void->B) : Void->A {
return join(func1,func2);
}
/*
Join two unary matched functions together and return them as a new "glued"
function.
*/
public static function join1<A,B,C>(func1:A->B, func2:A->C) : A->B {

return join(func1,func2);
}
/*
Join two binary matched functions together and return them as a new "glued"
function.
*/
public static function join2<A,B,C,D>(func1:A->B->C, func2:A->B->D) : A->B->C {
return join(func1,func2);
}
/*
Join two ternary matched functions together and return them as a new "glued"
function.
*/
public static function join3<A,B,C,D,E>(func1:A->B->C->D, func2:A->B->C->E) : A->B->C->D {
return join(func1,func2);
}
/*
Join two quaternary matched functions together and return them as a new "glued"
function.
*/
public static function join4<A,B,C,D,E,F>(func1:A->B->C->D->E, func2:A->B->C->D->F) : A->B->C->D->E {
return join(func1,func2);
}
/*
Join two quinary matched functions together and return them as a new "glued"
function.
*/
public static function join5<A,B,C,D,E,F,G>(func1:A->B->C->D->E->F, func2:A->B->C->D->E->G) : A->B->C->D->E->F {
return join(func1,func2);
}
/*
Join two senary matched functions together and return them as a new "glued"
function.
*/
public static function join6<A,B,C,D,E,F,G,H>(func1:A->B->C->D->E->F->G, func2:A->B->C->D->E->F->H) : A->B->C->D->E->F->G {
return join(func1,func2);
}

/*
This is the private function call that actually performs the function rebinding. It is not necessary
to match function arity here, since the public functions already catch these.
*/

private static function join<A,B>(func1:A, func2:B) : A {

var init = false;
var f = function(args:Array<Dynamic>) {
var ret = Reflect.callMethod({}, func1, args);
Reflect.callMethod({}, func2, args);
return ret;
}
var fmv = Reflect.makeVarArgs(f);
return fmv;
}

/**
* This is a memoize function that saves results under an argument hash, which can
* speed up repeated calls with the same arguments.
* @param func Dynamic A function
* @param hash_size Int The maximum size that the hash should grow to (in elements)
* @return Dynamic The memoized function
*/

public static function memoize(func:Dynamic , hash_size:Int = 100) : Dynamic {

var arg_hash = new Hash<Dynamic>();
var f = function(args:Array<Dynamic>){
var arg_string = args.join('|');
if (arg_hash.exists(arg_string)) return arg_hash.get(arg_string);
else{
var ret = Reflect.callMethod({},func,args);
if(Lambda.count(arg_hash) < hash_size) arg_hash.set(arg_string, ret);
return ret;
}
}
f = Reflect.makeVarArgs(f);
return f;
}




}

Change log

r18 by jdonaldson on Nov 16, 2009   Diff
added a "memoize" function
Go to: 
Project members, sign in to write a code review

Older revisions

r17 by jdonaldson on Oct 10, 2009   Diff
fixed copyright date
r16 by jdonaldson on Oct 10, 2009   Diff
added some code comments
r14 by jdonaldson on Oct 8, 2009   Diff
Added Glue and GlueTest
All revisions of this file

File info

Size: 4912 bytes, 137 lines
Powered by Google Project Hosting