From f9d13f3098b2a5984f59d612be87c184aba0b2c7 Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 24 Mar 2023 17:33:44 -0500 Subject: Stuff and things --- examples/fib.sloth | 57 ++++++++++++++++++++++++------------------------------ examples/ui.sloth | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 examples/ui.sloth (limited to 'examples') diff --git a/examples/fib.sloth b/examples/fib.sloth index 789ec5e..9b0cee9 100644 --- a/examples/fib.sloth +++ b/examples/fib.sloth @@ -29,8 +29,8 @@ pub type Button = { impl Button { fn init(text) { return Self( - x = 50, - y = 150, + x: 50, + y: 150, text, # Pass in text ) } @@ -39,8 +39,8 @@ impl Button { impl Constructor for Button { fn init(text) { Self( - x = 50, - y = 150, + x: 50, + y: 150, text, # Pass in text ) } @@ -59,39 +59,32 @@ print(fib(5)); # Inferred as List val nums = read("input.txt") .lines() - .filter(-> /$[0-9]+^/ in it) + .filter(-> /$[0-9]+^/ in $0) .collect() fn T <- From = List collect(self): T { } -# Statically typed with no type inference -fn fib(n: i32) -> i32: - # ML - match n with - | 0 -> 0 - | 1 -> 1 - | n -> fib(n - 1) + fib(n - 2) - - # Python - match n: - case 0 | 1: n - case n: - val lhs = fib(n - 1) - val rhs = fib(n - 2) - - lhs + rhs - - # Idea - match n: - 0 | 1: n - n: - val lhs = fib(n - 1) - val rhs = fib(n - 2) +fn collect(self): 'a +where + 'a : From, # Type Constraints + 'a = List, # Generic Defaults + 'a : From = List, # Combined +{ + # +} - lhs + rhs +# This following code should +fn add(lhs, rhs) { + return lhs + rhs; +} -# Statically typed but with type inference -fn fib(n): - n + 1 +# Ideally infer to +fn add(lhs: 'a, rhs: 'b): 'c +where + 'a : Add<'b, 'c>, + 'b = 'a, +{ + return lhs + rhs; +} diff --git a/examples/ui.sloth b/examples/ui.sloth new file mode 100644 index 0000000..2f69825 --- /dev/null +++ b/examples/ui.sloth @@ -0,0 +1,56 @@ +use extern "ui" + +use ui::components::Button; +use ui::components::Label; +use ui::components::FlexView; + +type Action = + | Increment + | Decrement + +fn update(state, message) { + match message { + Action::Increment -> state + 1, + Action::Decrement -> state - 1, + } +} + +fn render(state, dispatch) { + return FlexView([ + Button("-", action: -> dispatch(Action::Decrement)), + Label(state), + Button("+", action: -> dispatch(Action::Increment)), + + FlexView( + height: 500, + width: 220, + body: [ + Label("List 1"), + Label("List 2"), + Label("List 3"), + ], + ) + + Button( + width: 100, + height: 20, + action: -> (), + body: Label("Sign up"), + ) + ]) +} + +fn app() { + # Creating our state + val state = 0 + + # Creating our app + val app = ui::App( + state, + on_update: update, + on_render: render, + ) + + # Starting our app + app.start() +} -- cgit v1.2.3