init lux
This commit is contained in:
46
examples/datatypes.lux
Normal file
46
examples/datatypes.lux
Normal file
@@ -0,0 +1,46 @@
|
||||
// Demonstrating algebraic data types and pattern matching
|
||||
|
||||
// Define a binary tree
|
||||
type Tree =
|
||||
| Leaf(Int)
|
||||
| Node(Tree, Tree)
|
||||
|
||||
// Sum all values in a tree
|
||||
fn sumTree(tree: Tree): Int =
|
||||
match tree {
|
||||
Leaf(n) => n,
|
||||
Node(left, right) => sumTree(left) + sumTree(right)
|
||||
}
|
||||
|
||||
// Find the depth of a tree
|
||||
fn depth(tree: Tree): Int =
|
||||
match tree {
|
||||
Leaf(_) => 1,
|
||||
Node(left, right) => {
|
||||
let leftDepth = depth(left)
|
||||
let rightDepth = depth(right)
|
||||
1 + (if leftDepth > rightDepth then leftDepth else rightDepth)
|
||||
}
|
||||
}
|
||||
|
||||
// Example tree:
|
||||
// Node
|
||||
// / \
|
||||
// Node Leaf(5)
|
||||
// / \
|
||||
// Leaf(1) Leaf(2)
|
||||
|
||||
let myTree = Node(Node(Leaf(1), Leaf(2)), Leaf(5))
|
||||
let total = sumTree(myTree)
|
||||
let treeDepth = depth(myTree)
|
||||
|
||||
// Option type example
|
||||
fn safeDivide(a: Int, b: Int): Option<Int> =
|
||||
if b == 0 then None
|
||||
else Some(a / b)
|
||||
|
||||
fn showResult(result: Option<Int>): String =
|
||||
match result {
|
||||
None => "Division by zero!",
|
||||
Some(n) => "Result: " + n
|
||||
}
|
||||
35
examples/effects.lux
Normal file
35
examples/effects.lux
Normal file
@@ -0,0 +1,35 @@
|
||||
// Demonstrating algebraic effects in Lux
|
||||
|
||||
// Define a custom logging effect
|
||||
effect Logger {
|
||||
fn log(level: String, msg: String): Unit
|
||||
fn getLevel(): String
|
||||
}
|
||||
|
||||
// A function that uses the Logger effect
|
||||
fn processData(data: Int): Int with {Logger} = {
|
||||
Logger.log("info", "Processing data...")
|
||||
let result = data * 2
|
||||
Logger.log("debug", "Result computed")
|
||||
result
|
||||
}
|
||||
|
||||
// A handler that prints logs to console
|
||||
handler consoleLogger: Logger {
|
||||
fn log(level, msg) = Console.print("[" + level + "] " + msg)
|
||||
fn getLevel() = "debug"
|
||||
}
|
||||
|
||||
// A handler that ignores logs (for testing)
|
||||
handler nullLogger: Logger {
|
||||
fn log(level, msg) = ()
|
||||
fn getLevel() = "none"
|
||||
}
|
||||
|
||||
// Main function showing handler usage
|
||||
fn main(): Unit with {Console} = {
|
||||
let result = run processData(21) with {
|
||||
Logger = consoleLogger
|
||||
}
|
||||
Console.print("Final result: " + result)
|
||||
}
|
||||
12
examples/factorial.lux
Normal file
12
examples/factorial.lux
Normal file
@@ -0,0 +1,12 @@
|
||||
// Factorial function demonstrating recursion
|
||||
|
||||
fn factorial(n: Int): Int =
|
||||
if n <= 1 then 1
|
||||
else n * factorial(n - 1)
|
||||
|
||||
// Calculate factorial of 10
|
||||
let result = factorial(10)
|
||||
|
||||
// Print result
|
||||
fn main(): Unit with {Console} =
|
||||
Console.print("10! = " + result)
|
||||
42
examples/functional.lux
Normal file
42
examples/functional.lux
Normal file
@@ -0,0 +1,42 @@
|
||||
// Demonstrating functional programming features
|
||||
|
||||
// Higher-order functions
|
||||
fn apply(f: fn(Int): Int, x: Int): Int = f(x)
|
||||
|
||||
fn compose(f: fn(Int): Int, g: fn(Int): Int): fn(Int): Int =
|
||||
fn(x: Int): Int => f(g(x))
|
||||
|
||||
// Basic functions
|
||||
fn double(x: Int): Int = x * 2
|
||||
fn addOne(x: Int): Int = x + 1
|
||||
fn square(x: Int): Int = x * x
|
||||
|
||||
// Using apply
|
||||
let result1 = apply(double, 21) // 42
|
||||
|
||||
// Using compose
|
||||
let doubleAndAddOne = compose(addOne, double)
|
||||
let result2 = doubleAndAddOne(5) // 11
|
||||
|
||||
// Using the pipe operator
|
||||
let result3 = 5 |> double |> addOne |> square // ((5 * 2) + 1)^2 = 121
|
||||
|
||||
// Currying example
|
||||
fn add(a: Int): fn(Int): Int =
|
||||
fn(b: Int): Int => a + b
|
||||
|
||||
let add5 = add(5)
|
||||
let result4 = add5(10) // 15
|
||||
|
||||
// Partial application simulation
|
||||
fn multiply(a: Int, b: Int): Int = a * b
|
||||
|
||||
let times3 = fn(x: Int): Int => multiply(3, x)
|
||||
let result5 = times3(7) // 21
|
||||
|
||||
// Working with records
|
||||
let transform = fn(record: { x: Int, y: Int }): Int =>
|
||||
record.x + record.y
|
||||
|
||||
let point = { x: 10, y: 20 }
|
||||
let sum = transform(point) // 30
|
||||
5
examples/hello.lux
Normal file
5
examples/hello.lux
Normal file
@@ -0,0 +1,5 @@
|
||||
// Hello World in Lux
|
||||
// Demonstrates basic effect usage
|
||||
|
||||
fn main(): Unit with {Console} =
|
||||
Console.print("Hello, World!")
|
||||
Reference in New Issue
Block a user