use package core.intrinsics.onyx { __zero_value }
+as_iterator :: #match {}
+
+Iterable :: interface (T: type_expr) {
+ as_iterator(T);
+}
+
close :: (it: Iterator($T)) {
it.close(it.data);
}
};
}
+fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R {
+ for value: it {
+ initial_value = combine(value, initial_value);
+ }
+
+ return initial_value;
+}
+
+to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T {
+ array :: package core.array
+
+ arr := array.make(T, allocator=allocator);
+ for v: it do array.push(^arr, v);
+
+ return arr;
+}
+
+#match as_iterator from_array
from_array :: (arr: [] $T) -> Iterator(^T) {
Context :: struct (T: type_expr) {
data: ^T;
};
}
-fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R {
- for value: it {
- initial_value = combine(value, initial_value);
+#match as_iterator (r: range) -> Iterator(i32) {
+ Context :: struct {
+ r: range;
+ v: i32;
}
-
- return initial_value;
-}
-to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T {
- array :: package core.array
+ next :: (use c: ^Context) -> (i32, bool) {
+ if v > r.high {
+ return 0, false;
+ } else {
+ defer v += r.step;
+ return v, true;
+ }
+ }
- arr := array.make(T, allocator=allocator);
- for v: it do array.push(^arr, v);
+ close :: (c: ^Context) {
+ cfree(c);
+ }
- return arr;
+ c := new(Context);
+ c.r = r;
+ c.v = r.low;
+
+ return .{
+ data = c,
+ next = next,
+ close = close,
+ };
}
set cpo&vim
syn keyword onyxKeyword package struct enum proc use global macro
-syn keyword onyxKeyword if elseif else
+syn keyword onyxKeyword if elseif else where interface
syn keyword onyxKeyword for while do
syn keyword onyxKeyword switch case
syn keyword onyxKeyword break continue return defer fallthrough
</dict>
<dict>
<key>match</key>
- <string>\b(use|macro|package)\b</string>
+ <string>\b(use|macro|package|where)\b</string>
<key>name</key>
<string>keyword.control.onyx</string>
</dict>
</dict>
<dict>
<key>match</key>
- <string>\b(struct|enum)\b</string>
+ <string>\b(struct|enum|interface)\b</string>
<key>name</key>
<string>storage.type.onyx</string>
</dict>
<array>
<dict>
<key>match</key>
- <string>\b(struct|enum)\b(?:(\{)(\}))?</string>
+ <string>\b(struct|enum|interface)\b(?:(\{)(\}))?</string>
<key>name</key>
<string>storage.type.onyx</string>
</dict>
</dict>
<dict>
<key>match</key>
- <string>\b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum)</string>
+ <string>\b(\b[[:alpha:]_]+[[:alnum:]_]*\b)\s*[:]\s*[:]\s*(struct|enum|interface)</string>
<key>captures</key>
<dict>
<key>1</key>