Skip to content

Commit

Permalink
Merge pull request #8 from Rigidity/fp-utils
Browse files Browse the repository at this point in the history
Functional programming utilities
  • Loading branch information
Rigidity authored Jun 24, 2024
2 parents 68e495c + b8f5fed commit ab0a920
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/rue-compiler/src/database/type_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Database {
(Type::Alias(..), _) | (_, Type::Alias(..)) => unreachable!(),

// We need to infer `Generic` types, and return `Unrelated` if incompatible.
(_, Type::Generic) if ctx.infer_generics => {
(_, Type::Generic) => {
let mut found = None;

for generics in ctx.generic_type_stack.iter().rev() {
Expand All @@ -231,9 +231,11 @@ impl Database {

if let Some(found) = found {
self.compare_type_visitor(lhs, found, ctx)
} else {
} else if ctx.infer_generics {
ctx.generic_type_stack.last_mut().unwrap().insert(rhs, lhs);
Comparison::Assignable
} else {
Comparison::Unrelated
}
}

Expand Down Expand Up @@ -264,7 +266,7 @@ impl Database {

// `Generic` types are unrelated to everything else except their specific instance.
// The only exception is the `Any` type above.
(Type::Generic, _) | (_, Type::Generic) => Comparison::Unrelated,
(Type::Generic, _) => Comparison::Unrelated,

// You have to explicitly convert between other atom types.
// This is because the compiled output can change depending on the type.
Expand Down
24 changes: 24 additions & 0 deletions std/stdlib.rue
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,27 @@ export fun calculate_coin_id(
assert puzzle_hash is Bytes32;
sha256(parent_coin_id + puzzle_hash + amount as Bytes)
}

export fun map<T, U>(list: T[], fn: fun(T) -> U) -> U[] {
if list is Nil {
return nil;
}
[fn(list.first), ...map(list.rest, fn)]
}

export fun filter<T>(list: T[], fn: fun(T) -> Bool) -> T[] {
if list is Nil {
return nil;
}
if fn(list.first) {
return [list.first, ...filter(list.rest, fn)];
}
filter(list.rest, fn)
}

export fun fold<T, U>(list: T[], initial: U, fn: fun(U, T) -> U) -> U {
if list is Nil {
return initial;
}
fold(list.rest, fn(initial, list.first), fn)
}
7 changes: 7 additions & 0 deletions tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@ cost = 24974
input = "(0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 () (q . ((51 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000))) 1)"
output = "((g1_multiply 0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x3d7e1145b5969c12f4889f4f1f66bde0e4d3ba54b91784cf604294d162b44b69) (g1_negate 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000))"
hash = "670f06e052dedc68788dd17acb7d100203a1533d11f9c679d0f23bd3d555f122"

[fp_utils]
bytes = 873
cost = 92626
input = "()"
output = "()"
hash = "5f8d285dbefef4c4c7ccfcc4f204710c21f46ea45bfe6d5b4e81562ef183adde"
6 changes: 6 additions & 0 deletions tests/stdlib/fp_utils.rue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun main() -> Nil {
assert tree_hash(map([1, 2, 3], fun(num: Int) => num * 2)) == tree_hash([2, 4, 6]);
assert tree_hash(filter([1, 2, 3, 4, 5], fun(num: Int) => num < 4)) == tree_hash([1, 2, 3]);
assert tree_hash(fold([1, 2, 3], 0, fun(acc: Int, num: Int) => acc + num)) == tree_hash(6);
nil
}

0 comments on commit ab0a920

Please sign in to comment.