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 ++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 32 deletions(-) (limited to 'examples/fib.sloth') 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; +} -- cgit v1.2.3