Browse Source

- Added some functions to Arrays

- Added native tags where needed
- Added EqualsComparable to designate types that have an equality operator
Stan Hebben 6 years ago
parent
commit
06358887e5

+ 1
- 1
stdlib/module.json View File

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

+ 60
- 11
stdlib/src/Arrays.zs View File

1
+[Native("stdlib::Arrays")]
1
 export expand <T : Comparable<T>> T[] {
2
 export expand <T : Comparable<T>> T[] {
3
+	[Native("sort")]
2
 	public extern sort() as void;
4
 	public extern sort() as void;
5
+	[Native("sorted")]
3
 	public extern sorted() as T[];
6
 	public extern sorted() as T[];
4
 }
7
 }
5
 
8
 
10
 	}
13
 	}
11
 }
14
 }
12
 
15
 
16
+[Native("stdlib::Arrays")]
13
 export expand <T> T[] {
17
 export expand <T> T[] {
18
+	[Native("sortWithComparator")]
14
 	public extern sort(comparator as function(a as T, b as T) as int) as void;
19
 	public extern sort(comparator as function(a as T, b as T) as int) as void;
20
+	[Native("sortedWithComparator")]
15
 	public extern sorted(comparator as function(a as T, b as T) as int) as T[];
21
 	public extern sorted(comparator as function(a as T, b as T) as int) as T[];
22
+	[Native("copy")]
16
 	public extern copy() as T[];
23
 	public extern copy() as T[];
24
+	[Native("copyResize")]
17
 	public extern copy(newSize as int) as T[];
25
 	public extern copy(newSize as int) as T[];
26
+	[Native("copyTo")]
18
 	public extern copyTo(target as T[], sourceOffset as int, targetOffset as int, length as int) as void;
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
 	public map<U>(projection as function(value as T) as U) as U[] {
51
 	public map<U>(projection as function(value as T) as U) as U[] {
25
 		return new U[]<T>(this, projection);
52
 		return new U[]<T>(this, projection);
26
 	}
53
 	}
27
 	
54
 	
55
+	[Native("mapKeyValues")]
28
 	public map<U>(projection as function(index as int, value as T) as U) as U[] {
56
 	public map<U>(projection as function(index as int, value as T) as U) as U[] {
29
 		return new U[]<T>(this, projection);
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
 	public each(consumer as function(value as T) as void) as void {
78
 	public each(consumer as function(value as T) as void) as void {
36
 		for value in this
79
 		for value in this
91
 	}
134
 	}
92
 	
135
 	
93
 	public last(predicate as function(value as T) as bool) as T? {
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
 		return null;
144
 		return null;
99
 	}
145
 	}
100
 	
146
 	
101
 	public last(predicate as function(index as int, value as T) as bool) as T? {
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
 		return null;
155
 		return null;
107
 	}
156
 	}

+ 2
- 0
stdlib/src/Comparable.zs View File

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

+ 3
- 0
stdlib/src/EqualsComparable.zs View File

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

+ 4
- 0
stdlib/src/Exception.zs View File

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

+ 2
- 0
stdlib/src/IllegalArgumentException.zs View File

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

+ 0
- 0
stdlib/src/IllegalOperationException.zs View File


+ 5
- 0
stdlib/src/Integers.zs View File

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

+ 11
- 2
stdlib/src/List.zs View File

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

+ 0
- 4
stdlib/src/Panic.zs View File

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 View File

19
 	public expect() as T {
19
 	public expect() as T {
20
 		return match this {
20
 		return match this {
21
 			Ok(result) => result,
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
 }
39
 }
40
 
40
 
41
 export expand <T, E : Exception> Result<T, E> {
41
 export expand <T, E : Exception> Result<T, E> {
42
-	public unwrap() as T {
42
+	public unwrap() as T throws E {
43
 		return match this {
43
 		return match this {
44
 			Ok(result) => result,
44
 			Ok(result) => result,
45
 			Error(error) => throw error
45
 			Error(error) => throw error

+ 10
- 0
stdlib/src/Strings.zs View File

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

Loading…
Cancel
Save