updated: examples
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 28 Mar 2024 01:49:41 +0000 (20:49 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 28 Mar 2024 01:49:41 +0000 (20:49 -0500)
.gitmodules [new file with mode: 0644]
examples
src/app.onyx
src/cached_resource.onyx
www/examples/files.html
www/examples/stdin.html

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..03a1d40
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "examples"]
+       path = examples
+       url = https://github.com/onyx-lang/onyx-examples
index 1df16a331e64f15a5095d2e79dbc2dcece461382..45ff12e45a39d7b0f6afff99cf8101e1a35d6c73 160000 (submodule)
--- a/examples
+++ b/examples
@@ -1 +1 @@
-Subproject commit 1df16a331e64f15a5095d2e79dbc2dcece461382
+Subproject commit 45ff12e45a39d7b0f6afff99cf8101e1a35d6c73
index 389ff7a4e05aaa321c045880011bea8343f93fee..f82e43bfc6d03a15462a4b74d4517c8149bdd55c 100644 (file)
@@ -12,21 +12,18 @@ use core.encoding.json
 }
 
 reg: otmp.TemplateRegistry;
-#inject Res {
-    render :: (r: &Res, template: str, vars: any) {
-        m := misc.any_to_map(vars) ?? make(Map(str, any));
-        defer delete(&m);
+Res.render :: (r: &Res, template: str, vars: any) {
+    use m := misc.any_to_map(vars) ?? make(Map(str, any));
 
-        s := reg->render_template(template, &r.writer, &m);
+    s := reg->render_template(template, &r.writer, &m);
 
-        if s != .None {
-            log(.Warning, "Template Renderer", tprintf("{}", s));
-        }
-
-        r.headers["Content-Type"] = "text/html";
-        r->status(200 if s == .None else 400);
-        r->end();
+    if s != .None {
+        log(.Warning, "Template Renderer", tprintf("{}", s));
     }
+
+    r.headers->put("Content-Type", "text/html");
+    r->status(200 if s == .None else 400);
+    r->end();
 }
 
 main :: () {
index c36498922649d9f22e357a35735d78907cefc871..d9d98e58ca9d3f91ab211be6794d6d46c3740654 100644 (file)
@@ -12,18 +12,16 @@ Cached_Resource :: struct (Res: type_expr) {
     release_resource: (res: &Res) -> void;
 }
 
-#inject Cached_Resource {
-    get :: (use self: &#Self) -> ? self.Res {
-        if !resource {
-            update_resource(self);
-        }
-
-        if time.duration(time.now(), last_retrieved) > max_age {
-            update_resource(self);
-        }
+Cached_Resource.get :: (use self: &#Self) -> ? self.Res {
+    if !resource {
+        update_resource(self);
+    }
 
-        return self.resource;
+    if time.duration(time.now(), last_retrieved) > max_age {
+        update_resource(self);
     }
+
+    return self.resource;
 }
 
 #local
@@ -39,11 +37,9 @@ update_resource :: (self: &Cached_Resource($T)) {
 
 
 
-#inject time {
-    duration :: (t2, t1: time.Timestamp) -> i32 {
-        t1_ := t1;
-        t2_ := t2;
-        return ~~(t2_->to_epoch() - t1_->to_epoch());
-    }
+time.duration :: (t2, t1: time.Timestamp) -> i32 {
+    t1_ := t1;
+    t2_ := t2;
+    return ~~(t2_->to_epoch() - t1_->to_epoch());
 }
 
index 1f54a5015843dc77745318ec48f5495bc11f4691..bf2895a38faaa64d13c260f29163875236cb48d3 100644 (file)
@@ -20,41 +20,18 @@ write_data_into_file :: (filename: str) {
     // Also, free it later by defering io.writer_free. This also flushes
     // the internal buffer of io.Writer to make sure everything is written
     // to the file.
-    file_writer := io.writer_make(&file);
-    defer io.writer_free(&file_writer);
+    file_writer := io.Writer.make(&file);
+    defer io.Writer.free(&file_writer);
 
     // The simplest way of writing a string to the file.
-    io.write(&file_writer, "This is the first line of text.\n");
+    file_writer->write("This is the first line of text.\n");
 
     // io.write_format can be used to "printf" into a file.
     // printf can be thought of as io.write_format(&stdio.stream, ...).
-    io.write_format(&file_writer, "This is a {} line of text.\n", "formatted");
+    file_writer->write_format("This is a {} line of text.\n", "formatted");
 
     for i in 0 .. 5 {
-        io.write_format(&file_writer, "Another line numbered {}.\n", i);
-    }
-}
-
-#doc "Reads example text from a file."
-read_data_from_file :: (filename: str) {
-    // This is another way of opening a file. Because of the semantics
-    // of `for` loops over Iterators, they can behave like `with`
-    // statements in Python or `using` statements in C#.
-    for file in os.with_file(filename, .Read) {
-        // Create a io.Reader over the file stream.
-        file_reader := io.reader_make(file);
-        defer io.reader_free(&file_reader);
-
-        // Read a single line.
-        first_line := io.read_line(&file_reader);
-        printf("First line: {}\n", first_line);
-
-        // Use io.lines to create an iterator over the remaining
-        // lines in the file.
-        printf("Remaining lines:\n");
-        for line, index in io.lines(&file_reader) {
-            printf("{}: {}\n", index, line);
-        }
+        file_writer->write_format("Another line numbered {}.\n", i);
     }
 }
 
@@ -73,6 +50,5 @@ main :: () {
     filename := "test.txt";
 
     write_data_into_file(filename);
-    read_data_from_file(filename);
     read_whole_file(filename);
 }</code></pre>
index e9ad9dca24ec9bd4dfb6f2bc6feb5d990c618e21..f90b53ff0b1848e1548c6acf419f8226cd67c8ee 100644 (file)
@@ -2,29 +2,28 @@
 <p class='author'>Brendan Hansen</p>
 <div class='description'>This example reads a single line of input from <a href="https://en.wikipedia.org/wiki/Standard_streams">standard input</a>, splits it in half on the first space using <a href="https://docs.onyxlang.io/packages/core.string.html#bisect">string.bisect</a>, converts both parts to integers using <a href="https://docs.onyxlang.io/packages/core.conv.html#parse">conv.parse</a>, then prints the results using <a href="https://docs.onyxlang.io/packages/core.html#printf">printf</a> for formatted printing.</div>
 <pre class='hljs'><code class='language-onyx'>// Use the necessary core libraries
-use core.io
-use core.string
+use core.io { Reader }
 use core.conv
 
 // Use the printf function that lives in the core package.
 // This cannot be `use core.printf`, because that will look
 // for a package called `printf` in `core`.
 use core {
-    printf
+    printf, stdio
 }
 
 main :: () {
     // Create a io.Reader over the stdio.stream to be able scan
     // the input in parts. Also, defer freeing the reader until
     // the end of `main`.
-    stdin_reader := io.reader_make(&stdio.stream);
-    defer io.reader_free(&stdin_reader);
+    stdin_reader := Reader.make(&stdio.stream);
+    defer Reader.free(&stdin_reader);
 
     // Read a single line of input.
-    line := io.read_line(&stdin_reader);
+    line := stdin_reader->read_line();
 
     // Split the line on the first space.
-    a_str, b_str := string.bisect(line, " ");
+    a_str, b_str := line->bisect(" ");
 
     // Parse and convert both parts to i32s, with a default value
     // of 0 if it fails to parse as an i32.