diff options
| author | Cody <cody@codyq.dev> | 2023-03-10 02:37:30 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-03-10 02:37:30 -0600 |
| commit | 28e0b95d8ecbbc44ef81069ad122a88b2a64c74e (patch) | |
| tree | eb577f71546dcb67d16c6b35af2031fcbfae1752 /examples | |
| parent | 9d65e62bff41c62cb0daeb8a1934991af08dbbc1 (diff) | |
| download | sloth-28e0b95d8ecbbc44ef81069ad122a88b2a64c74e.tar.gz | |
🧠⛈️
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/fib.sloth | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/examples/fib.sloth b/examples/fib.sloth index 1031d1c..789ec5e 100644 --- a/examples/fib.sloth +++ b/examples/fib.sloth @@ -1,4 +1,8 @@ -fn fib(n: i32) -> i32 { +fn fib(n: i32): i32 { + val msg = if (n == 0) "No points" else "Some points" + val msg = if n == 0 { "No points" } else { "Some points" } + val msg = if n == 0: "No points" else: "Some points" + if n == 0 || n == 1 { return n; } @@ -16,9 +20,78 @@ fn fib(n: i32) -> i32 { return me; } +pub type Button = { + pub x: i32, + pub y: i32, + text: String, +} + +impl Button { + fn init(text) { + return Self( + x = 50, + y = 150, + text, # Pass in text + ) + } +} + +impl Constructor for Button { + fn init(text) { + Self( + x = 50, + y = 150, + text, # Pass in text + ) + } +} + +Button::init("Hello") +Button("Hello") + print(fib(0)); print(fib(1)); print(fib(2)); print(fib(3)); print(fib(4)); print(fib(5)); + +# Inferred as List +val nums = read("input.txt") + .lines() + .filter(-> /$[0-9]+^/ in it) + .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) + + lhs + rhs + +# Statically typed but with type inference +fn fib(n): + n + 1 |
