-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPair.hx
78 lines (65 loc) · 1.55 KB
/
Pair.hx
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
package moon.core;
/**
* Pair is a pair of values.
* Pair is similar to Tuple2, but different implementation.
*
* Usage:
* var x:Pair<String, Int> = Pair.of("hello", 5);
* trace(x.head);
*
* @author Munir Hussin
*/
class Pair<A, B>
{
private var data:Array<Dynamic>;
public var head(get, set):A;
public var tail(get, set):B;
public function new(head:A, tail:B)
{
data = [head, tail];
}
// (head . tail)
public static inline function of<A, B>(head:A, tail:B):Pair<A, B>
{
return new Pair(head, tail);
}
private inline function get_head():A
{
return data[0];
}
private inline function get_tail():B
{
return data[1];
}
private inline function set_head(v:A):A
{
return data[0] = v;
}
private inline function set_tail(v:B):B
{
return data[1] = v;
}
/*==================================================
Methods
==================================================*/
public function iterator():Iterator<Dynamic>
{
return data.iterator();
}
public inline function swap():Pair<B, A>
{
return Pair.of(tail, head);
}
public inline function join(sep:String):String
{
return data.join(sep);
}
public function equals(other:Pair<A, B>):Bool
{
return this.head == other.head && this.tail == other.tail;
}
public function toString():String
{
return "(" + join(": ") + ")";
}
}