aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-03-24 17:33:44 -0500
committerCody <cody@codyq.dev>2023-03-24 17:33:44 -0500
commitf9d13f3098b2a5984f59d612be87c184aba0b2c7 (patch)
tree0a8059a4604026c3c0fcde587617507063cf7ccf /examples
parent28e0b95d8ecbbc44ef81069ad122a88b2a64c74e (diff)
downloadsloth-f9d13f3098b2a5984f59d612be87c184aba0b2c7.tar.gz
Stuff and things
Diffstat (limited to 'examples')
-rw-r--r--examples/fib.sloth57
-rw-r--r--examples/ui.sloth56
2 files changed, 81 insertions, 32 deletions
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<Self> = 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<Self>, # Type Constraints
+ 'a = List, # Generic Defaults
+ 'a : From<Self> = 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()
+}