aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.sloth
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/fib.sloth
parent28e0b95d8ecbbc44ef81069ad122a88b2a64c74e (diff)
downloadsloth-f9d13f3098b2a5984f59d612be87c184aba0b2c7.tar.gz
Stuff and things
Diffstat (limited to 'examples/fib.sloth')
-rw-r--r--examples/fib.sloth57
1 files changed, 25 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;
+}