ソースを参照

- Added some functions to Arrays

- Added native tags where needed
- Added EqualsComparable to designate types that have an equality operator
Stan Hebben 6年前
コミット
06358887e5

+ 1
- 1
stdlib/module.json ファイルの表示

@@ -4,11 +4,11 @@
4 4
 	"globals": {
5 5
 		"Comparable": {"type": "Definition", "definition": "Comparable"},
6 6
 		"Exception": {"type": "Definition", "definition": "Exception"},
7
+		"EqualsComparable": {"type": "Definition", "definition": "EqualsComparable"},
7 8
 		"Hashable": {"type": "Definition", "definition": "Hashable"},
8 9
 		"IllegalArgumentException": {"type": "Definition", "definition": "IllegalArgumentException"},
9 10
 		"StringBuilder": {"type": "Definition", "definition": "StringBuilder"},
10 11
 		"StringBuildable": {"type": "Definition", "definition": "StringBuildable"},
11
-		"panic": {"type": "Definition", "definition": "panic"},
12 12
 		"Result": {"type": "Definition", "definition": "Result"}
13 13
 	}
14 14
 }

+ 60
- 11
stdlib/src/Arrays.zs ファイルの表示

@@ -1,5 +1,8 @@
1
+[Native("stdlib::Arrays")]
1 2
 export expand <T : Comparable<T>> T[] {
3
+	[Native("sort")]
2 4
 	public extern sort() as void;
5
+	[Native("sorted")]
3 6
 	public extern sorted() as T[];
4 7
 }
5 8
 
@@ -10,27 +13,67 @@ export expand <T : Hashable<T>> T[] {
10 13
 	}
11 14
 }
12 15
 
16
+[Native("stdlib::Arrays")]
13 17
 export expand <T> T[] {
18
+	[Native("sortWithComparator")]
14 19
 	public extern sort(comparator as function(a as T, b as T) as int) as void;
20
+	[Native("sortedWithComparator")]
15 21
 	public extern sorted(comparator as function(a as T, b as T) as int) as T[];
22
+	[Native("copy")]
16 23
 	public extern copy() as T[];
24
+	[Native("copyResize")]
17 25
 	public extern copy(newSize as int) as T[];
26
+	[Native("copyTo")]
18 27
 	public extern copyTo(target as T[], sourceOffset as int, targetOffset as int, length as int) as void;
19 28
 	
20
-	public get first as T?;
21
-	public get last as T?;
22
-	public get reversed as T[];
29
+	public get first as T?
30
+		=> this.isEmpty ? null : this[0];
23 31
 	
32
+	public get last as T?
33
+		=> this.isEmpty ? null : this[$ - 1];
34
+	
35
+	[Native("reverse")]
36
+	public reverse() as void {
37
+		for i in 0 .. length / 2 {
38
+			var temp = this[i];
39
+			this[i] = this[length - i - 1];
40
+			this[length - i - 1] = temp;
41
+		}
42
+	}
43
+	
44
+	// TODO: fix compilation for this
45
+	/*[Native("reversed")]
46
+	public reversed() as T[] {
47
+		return new T[](this, (i, value) => this[length - i - 1]);
48
+	}*/
49
+	
50
+	[Native("mapValues")]
24 51
 	public map<U>(projection as function(value as T) as U) as U[] {
25 52
 		return new U[]<T>(this, projection);
26 53
 	}
27 54
 	
55
+	[Native("mapKeyValues")]
28 56
 	public map<U>(projection as function(index as int, value as T) as U) as U[] {
29 57
 		return new U[]<T>(this, projection);
30 58
 	}
31 59
 	
32
-	public extern filter(predicate as function(value as T) as bool) as T[];
33
-	public extern filter(predicate as function(index as int, value as T) as bool) as T[];
60
+	[Native("filterValues")]
61
+	public filter(predicate as function(value as T) as bool) as T[] {
62
+		var values = new List<T>();
63
+		for value in this
64
+			if predicate(value)
65
+				values.add(value);
66
+		return values as T[];
67
+	}
68
+	
69
+	[Native("filterKeyValues")]
70
+	public filter(predicate as function(index as int, value as T) as bool) as T[] {
71
+		var values = new List<T>();
72
+		for i, value in this
73
+			if predicate(i, value)
74
+				values.add(value);
75
+		return values as T[];
76
+	}
34 77
 	
35 78
 	public each(consumer as function(value as T) as void) as void {
36 79
 		for value in this
@@ -91,17 +134,23 @@ export expand <T> T[] {
91 134
 	}
92 135
 	
93 136
 	public last(predicate as function(value as T) as bool) as T? {
94
-		for i, value in this.reversed
95
-			if predicate(value)
96
-				return value;
137
+		var i = length;
138
+		while i > 0 {
139
+			i--;
140
+			if predicate(this[i])
141
+				return this[i];
142
+		}
97 143
 		
98 144
 		return null;
99 145
 	}
100 146
 	
101 147
 	public last(predicate as function(index as int, value as T) as bool) as T? {
102
-		for i, value in this.reversed
103
-			if predicate(i, value)
104
-				return value;
148
+		var i = length;
149
+		while i > 0 {
150
+			i--;
151
+			if predicate(i, this[i])
152
+				return this[i];
153
+		}
105 154
 		
106 155
 		return null;
107 156
 	}

+ 2
- 0
stdlib/src/Comparable.zs ファイルの表示

@@ -1,3 +1,5 @@
1
+[Native("stdlib::Comparable")]
1 2
 export interface Comparable<T> {
3
+	[Native("compareTo")]
2 4
 	compareTo(other as T) as int;
3 5
 }

+ 3
- 0
stdlib/src/EqualsComparable.zs ファイルの表示

@@ -0,0 +1,3 @@
1
+public interface EqualsComparable<T> {
2
+	==(other as T) as bool;
3
+}

+ 4
- 0
stdlib/src/Exception.zs ファイルの表示

@@ -1,4 +1,8 @@
1
+[Native("stdlib::Exception")]
1 2
 export virtual class Exception {
3
+	[Native("constructor")]
2 4
 	public this(message as string) {}
5
+	
6
+	[Native("constructorWithCause")]
3 7
 	public this(message as string, cause as Exception) {}
4 8
 }

+ 2
- 0
stdlib/src/IllegalArgumentException.zs ファイルの表示

@@ -1,4 +1,6 @@
1
+[Native("stdlib::IllegalArgumentException")]
1 2
 export class IllegalArgumentException : Exception {
3
+	[Native("constructor")]
2 4
 	public this(message as string) {
3 5
 		super(message);
4 6
 	}

+ 0
- 0
stdlib/src/IllegalOperationException.zs ファイルの表示


+ 5
- 0
stdlib/src/Integers.zs ファイルの表示

@@ -1,8 +1,13 @@
1
+[Native("stdlib::Integer")]
1 2
 expand int {
3
+	[Native("toHexString")]
2 4
 	public extern toHexString() as string;
3 5
 	
6
+	[Native("min")]
4 7
 	public static min(a as int, b as int) as int
5 8
 		=> a < b ? a : b;
9
+	
10
+	[Native("max")]
6 11
 	public static max(a as int, b as int) as int
7 12
 		=> a > b ? a : b;
8 13
 }

+ 11
- 2
stdlib/src/List.zs ファイルの表示

@@ -2,16 +2,25 @@
2 2
 export class List<T> {
3 3
 	[Native("constructor")]
4 4
 	public this() {}
5
+	
5 6
 	[Native("add")]
6 7
 	public add(value as T) as void;
7
-	[Native("getIndex")]
8
+	
9
+	[Native("insert")]
10
+	public insert(index as int, value as T) as void;
11
+	
12
+	[Native("getAtIndex")]
8 13
 	public [](index as int) as T;
9
-	[Native("setIndex")]
14
+	
15
+	[Native("setAtIndex")]
10 16
 	public []=(index as int, value as T) as T;
17
+	
11 18
 	[Native("toArray")]
12 19
 	public as T[];
20
+	
13 21
 	[Native("length")]
14 22
 	public get length as int;
23
+	
15 24
 	[Native("isEmpty")]
16 25
 	public get isEmpty as bool;
17 26
 }

+ 0
- 4
stdlib/src/Panic.zs ファイルの表示

@@ -1,4 +0,0 @@
1
-// TODO: function overloading on global functions
2
-//extern function panic() as void;
3
-
4
-extern function panic<T>(message as string) as T;

+ 2
- 2
stdlib/src/Result.zs ファイルの表示

@@ -19,7 +19,7 @@ export variant Result<T, E> {
19 19
 	public expect() as T {
20 20
 		return match this {
21 21
 			Ok(result) => result,
22
-			Error(error) => panic<T>("expect() called on an error value")
22
+			Error(error) => panic "expect() called on an error value"
23 23
 		};
24 24
 	}
25 25
 	
@@ -39,7 +39,7 @@ export variant Result<T, E> {
39 39
 }
40 40
 
41 41
 export expand <T, E : Exception> Result<T, E> {
42
-	public unwrap() as T {
42
+	public unwrap() as T throws E {
43 43
 		return match this {
44 44
 			Ok(result) => result,
45 45
 			Error(error) => throw error

+ 10
- 0
stdlib/src/Strings.zs ファイルの表示

@@ -1,7 +1,10 @@
1
+[Native("stdlib::String")]
1 2
 export expand string {
3
+	[Native("contains")]
2 4
 	public const in(c as char) as bool
3 5
 		=> indexOf(c) >= 0;
4 6
 	
7
+	[Native("indexOf")]
5 8
 	public const indexOf(c as char) as int {
6 9
 		for i in 0 .. length {
7 10
 			if this[i] == c
@@ -11,6 +14,7 @@ export expand string {
11 14
 		return -1;
12 15
 	}
13 16
 	
17
+	[Native("indexOfFrom")]
14 18
 	public const indexOf(c as char, from as int) as int {
15 19
 		for i in from .. length {
16 20
 			if this[i] == c
@@ -20,6 +24,7 @@ export expand string {
20 24
 		return -1;
21 25
 	}
22 26
 	
27
+	[Native("lastIndexOf")]
23 28
 	public const lastIndexOf(c as char) as int {
24 29
 		var i = length;
25 30
 		while i > 0 {
@@ -31,6 +36,7 @@ export expand string {
31 36
 		return -1;
32 37
 	}
33 38
 	
39
+	[Native("lastIndexOfFrom")]
34 40
 	public const lastIndexOf(c as char, until as int) as int {
35 41
 		var i = until;
36 42
 		while i > 0 {
@@ -42,6 +48,7 @@ export expand string {
42 48
 		return -1;
43 49
 	}
44 50
 	
51
+	[Native("split")]
45 52
 	public const split(delimiter as char) as string[] {
46 53
 		val result = new List<string>();
47 54
 		var start = 0;
@@ -55,6 +62,7 @@ export expand string {
55 62
 		return result as string[];
56 63
 	}
57 64
 	
65
+	[Native("trim")]
58 66
 	public const trim() as string {
59 67
 		var from = 0;
60 68
 		while from < this.length && this[from] in [' ', '\t', '\r', '\n']
@@ -66,9 +74,11 @@ export expand string {
66 74
 		return to < from ? "" : this[from .. to];
67 75
 	}
68 76
 	
77
+	[Native("lpad")]
69 78
 	public const lpad(length as int, c as char) as string
70 79
 		=> this.length >= length ? this : c.times(length - this.length) + this;
71 80
 	
81
+	[Native("rpad")]
72 82
 	public const rpad(length as int, c as char) as string
73 83
 		=> this.length >= length ? this : this + c.times(length - this.length);
74 84
 }

読み込み中…
キャンセル
保存