Rust-like syntax,
Python-like ease,
Very, very fast.
Keel is a statically-typed interpreted programming language. It has full type inference, pattern matching, FFI support, optional annotations, polymorphism, a built-in REPL, a (WIP) package manager, and it can be embedded in other programs through a C ABI.
- 8x faster than Python
- 2.7x faster than LuaJIT (-joff)
struct Point { x: int, y: int }
fn add(a, b) {
return a + b;
}
fn main() {
let p = Point { x: 3, y: 4 };
print(add(p.x, p.y)); // 7
print(add("Hello, ", "world!")); // Hello, world!
let nums = [4, 2, 6, 1, 7];
if nums[0] == 4 {
nums.sort();
print(if nums[0] == 1 { nums[0..3] } else { -1 }); // [1,2,4]
} else {
throw("Error!");
}
}
2 to 25 times faster than Python...
Some examples...
Write functions once, only annotate where you want to
fn condition(elem1, elem2, flag: bool) {
return if flag {elem1} else {elem2};
}
fn add(x, y) {
return x+y;
}
fn main() {
let result = add(1, 1);
let result2 = add(1.5, 1.5);
let result3 = add("Hello", ", world!");
let result4 = add([0,1], [2,3]);
}
Use an if block as an expression
fn main() {
let my_number = 42;
let the_answer =
if my_number == 42 { "It's the answer to the question of life!" }
else if my_number == 20 { "Nope" }
else { "It's not the answer..." };
print(the_answer);
}
Slice / iterate with ranges
fn quicksort(arr) {
if arr.len() <= 1 {
return arr;
}
let pivot = arr[0];
let left = [];
let right = [];
for x in arr[1..arr.len()] {
if x < pivot {
left.push(x);
} else {
right.push(x);
}
}
return quicksort(left) + [pivot] + quicksort(right);
}
Catch errors by name
try {
// error-prone code here
throw("my_own_error");
} catch "index_out_of_bounds" {
// this matches a specific error
// code here
} catch "slice_out_of_bounds" {
// code here
} catch e {
// this binds the error (a string) to a variable
// code here
}
Maps
fn main() {
let map = {"one": 1, "two": 2, "other": 42};
// Retrieve the value associated with the key "one": 1.
let k = map.get("one");
// This inserts a new key-value pair in the map.
map.insert("three", 3);
// This overwrites the value of the existing key.
map.insert("one", 0);
}
Match values and patterns
struct Point {
x: int,
y: int,
}
fn my_func(num: int | Point | int[]) {
match num {
0 => {return 0;}
Point {x: 0, y} => {return y;}
Point {x, ..} => {return x;}
num:int => { match num {
x if x < 0 => {return 1;}
42..67 => {return 2;}
something => {return something;}
}}
array: int[] => { match array {
[] => {return "empty";}
[.., last] => {return last;}
}}
}
}
fn main() {
print(my_func(-1)); // 1
print(my_func(0)); // 0
print(my_func(Point {x: 0, y: 42})); // 42
print(my_func(Point {x: 42, y: 0})); // 42
print(my_func(42)); // 2
print(my_func([])); // "empty"
print(my_func([1,2,3,4])); // 4
}
Awesome compile-time errors
struct Point {
x:float,
y:float,
z:float
}
fn main() {
let origin = Point {x: 0.0, y: 0.0, z: 0.0};
origin.x = 10;
}
[ERROR]: Incompatible types ╭─[ testing/error.kl:2:7 ] │ 2 │ x:float, │ ───┬── │ ╰──── Field x in struct Point expects type float │ 9 │ origin.x = 10; │ ─┬ │ ╰── This expression is of type int │ │ Help: Try using the float() function ───╯
Use functions as values
fn test(flag: bool, f1: fn(), f2: fn(bool, int) -> string) {
if flag {
f1();
} else {
print(f2(false,0));
}
}
fn main() {
test(false,
fn() {print("Hell yeah!");},
fn(flag, n) {return string(flag) + ": " + string(n);}
);
}
Call C directly...
Declare a dynamic library and call its functions as Keel functions. Keel can also be embedded through a C ABI.
Source
dylib "my_dynamic_library" {
int increment(int);
}
fn main() {
let x = 0;
for _ in 0..10000000 {
x = ffi::increment(x);
}
print(x);
}
Overhead per C call (lower is better).
12.6 ns
Keel
44.9 ns
LuaJIT (-joff)
284 ns
Python
Install Keel
macOS and Linux
curl -fsSL https://raw.githubusercontent.com/horacehoff/keel/main/install.sh | sh
Windows PowerShell
irm https://raw.githubusercontent.com/horacehoff/keel/main/install.ps1 | iex
