Skip to content

Commit

Permalink
ops: Version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Olian04 committed Jul 11, 2024
1 parent b68c93d commit 3d118fe
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 64 deletions.
63 changes: 43 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,91 @@
# recursive

[![Package Version](https://img.shields.io/hexpm/v/recursive)](https://hex.pm/packages/recursive)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/recursive/)

# recursive

This library provides user/developer friendly Fixed-point combinator functions.

```sh
gleam add recursive
```

## Recursive block

```gleam
import gleam/io
import recursive
pub fn main() {
// Factorial
io.debug({
use it, f <- recursive.use_fix(10)
use it, f <- recursive.block(10)
case it {
0 -> 1
_ -> it * f(it - 1)
}
})
let fac = recursive.fix(fn (it, f) {
case it {
0 -> 1
_ -> it * f(it - 1)
}
})
// Tail recursive factorial
io.debug({
use it, res, f <- recursive.use_fix2(10, 1)
use it, res, f <- recursive.block2(10, 1)
case it {
0 -> res
_ -> f(it - 1, res * it)
}
})
let memo_fac = recursive.fix2(fn (it, res, f) {
case it {
0 -> res
_ -> f(it - 1, res * it)
}
})
// Tail recursive fibonacci
io.debug({
use it, curr, prev, f <- recursive.use_fix3(9, 1, 0)
use it, curr, prev, f <- recursive.block3(9, 1, 0)
case it {
0 -> curr
_ -> f(it - 1, curr + prev, curr)
}
})
let fib = recursive.fix3(fn (it, curr, prev, f) {
}
```

## Recursive anonymous function

```gleam
import gleam/io
import recursive
pub fn main() {
// Factorial
let fac = recursive.func(fn (it, f) {
case it {
0 -> 1
_ -> it * f(it - 1)
}
})
io.debug(fac(10))
// Tail recursive factorial
let memo_fac = recursive.func(fn (it, res, f) {
case it {
0 -> res
_ -> f(it - 1, res * it)
}
})
io.debug(fac(10, 1))
// Tail recursive fibonacci
let fib = recursive.func(fn (it, curr, prev, f) {
case it {
0 -> curr
_ -> f(it - 1, curr + prev, curr)
}
})
io.debug(fac(9, 1, 0))
}
```

Further documentation can be found at <https://hexdocs.pm/recursive>.

## Development

```sh
gleam test
```

---
Further documentation can be found at <https://hexdocs.pm/recursive>.
4 changes: 2 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "recursive"
version = "1.0.0"
version = "2.0.0"

description = "A Fixed-point combinator implementation & utility functions for better DX when using it"
description = "User friendly Fixed-point combinator functions. Any block or anonymous function can run recursively!"
licences = ["MIT"]
repository = { type = "github", user = "olian04", repo = "gleam_recursive" }
gleam = ">= 1.0.0"
Expand Down
44 changes: 22 additions & 22 deletions src/recursive.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn rec(f) {
/// Fixed-point combinator.
/// Allows anonymous functions to run recursively
/// ```gleam
/// let factorial = recursive.fix(fn (it, f) {
/// let factorial = recursive.func(fn (it, f) {
/// case it {
/// 0 -> 1
/// 1 -> 1
Expand All @@ -16,14 +16,14 @@ pub fn rec(f) {
/// })
/// io.debug(factorial(10))
/// ```
pub fn fix(cb) {
pub fn func(cb) {
rec(fn(f) { fn(a) { cb(a, cb(_, f())) } })
}

/// Two argument Fixed-point combinator.
/// Allows anonymous functions to run recursively
/// ```gleam
/// let factorial = recursive.fix2(fn (it, res, f) {
/// let factorial = recursive.func2(fn (it, res, f) {
/// case it {
/// 0 -> res
/// 1 -> res
Expand All @@ -32,101 +32,101 @@ pub fn fix(cb) {
/// })
/// io.debug(factorial(10, 1))
/// ```
pub fn fix2(cb) {
pub fn func2(cb) {
rec(fn(f) { fn(a, b) { cb(a, b, fn(a, b) { cb(a, b, f()) }) } })
}

/// Three argument Fixed-point combinator.
/// Allows anonymous functions to run recursively
/// ```gleam
/// let fibonacci = recursive.fix3(fn (it, curr, prev, f) {
/// let fibonacci = recursive.func3(fn (it, curr, prev, f) {
/// case it {
/// 0 -> curr
/// _ -> f(it - 1, curr + prev, curr)
/// }
/// })
/// io.debug(fibonacci(9, 1, 0))
/// ```
pub fn fix3(cb) {
pub fn func3(cb) {
rec(fn(f) { fn(a, b, c) { cb(a, b, c, fn(a, b, c) { cb(a, b, c, f()) }) } })
}

/// Four argument Fixed-point combinator.
/// Allows anonymous functions to run recursively
pub fn fix4(cb) {
pub fn func4(cb) {
rec(fn(f) {
fn(a, b, c, d) { cb(a, b, c, d, fn(a, b, c, d) { cb(a, b, c, d, f()) }) }
})
}

/// Five argument Fixed-point combinator.
/// Allows anonymous functions to run recursively
pub fn fix5(cb) {
pub fn func5(cb) {
rec(fn(f) {
fn(a, b, c, d, e) {
cb(a, b, c, d, e, fn(a, b, c, d, e) { cb(a, b, c, d, e, f()) })
}
})
}

/// Usable Fixed-point combinator.
/// Fixed-point combinator.
/// Allows any block to run recursively
/// ```gleam
/// // Factorial
/// io.debug({
/// use it, f <- recursive.use_fix(10)
/// use it, f <- recursive.block(10)
/// case it {
/// 0 -> 1
/// 1 -> 1
/// _ -> it * f(it - 1)
/// }
/// })
/// ```
pub fn use_fix(initial, cb) {
fix(cb)(initial)
pub fn block(initial, cb) {
func(cb)(initial)
}

/// Two argument Fixed-point combinator.
/// Allows any block to run recursively
/// ```gleam
/// // Tail recursive factorial
/// io.debug({
/// use it, res, f <- recursive.use_fix2(10, 1)
/// use it, res, f <- recursive.block2(10, 1)
/// case it {
/// 0 -> res
/// 1 -> res
/// _ -> f(it - 1, res * it)
/// }
/// })
/// ```
pub fn use_fix2(a, b, cb) {
fix2(cb)(a, b)
pub fn block2(a, b, cb) {
func2(cb)(a, b)
}

/// Three argument Fixed-point combinator.
/// Allows any block to run recursively
/// ```gleam
/// // Fibonacci
/// io.debug({
/// use it, curr, prev, f <- recursive.use_fix3(9, 1, 0)
/// use it, curr, prev, f <- recursive.block3(9, 1, 0)
/// case it {
/// 0 -> curr
/// _ -> f(it - 1, curr + prev, curr)
/// }
/// })
/// ```
pub fn use_fix3(a, b, c, cb) {
fix3(cb)(a, b, c)
pub fn block3(a, b, c, cb) {
func3(cb)(a, b, c)
}

/// Four argument Fixed-point combinator.
/// Allows any block to run recursively
pub fn use_fix4(a, b, c, d, cb) {
fix4(cb)(a, b, c, d)
pub fn block4(a, b, c, d, cb) {
func4(cb)(a, b, c, d)
}

/// Five argument Fixed-point combinator.
/// Allows any block to run recursively
pub fn use_fix5(a, b, c, d, e, cb) {
fix5(cb)(a, b, c, d, e)
pub fn block5(a, b, c, d, e, cb) {
func5(cb)(a, b, c, d, e)
}
40 changes: 20 additions & 20 deletions test/recursive_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -49,66 +49,66 @@ fn memo_sum(a, b, c, d, res, f) {
}
}

pub fn fix_test() {
recursive.fix(fac)(10)
pub fn func_test() {
recursive.func(fac)(10)
|> should.equal(3_628_800)
}

pub fn fix2_test() {
recursive.fix2(memo_fac)(10, 1)
pub fn func2_test() {
recursive.func2(memo_fac)(10, 1)
|> should.equal(3_628_800)
}

pub fn fix3_test() {
recursive.fix3(fib)(9, 1, 0)
pub fn func3_test() {
recursive.func3(fib)(9, 1, 0)
|> should.equal(55)
}

pub fn fix4_test() {
recursive.fix4(sum)(1, 2, 3, 4)
pub fn func4_test() {
recursive.func4(sum)(1, 2, 3, 4)
|> should.equal(10)
}

pub fn fix5_test() {
recursive.fix5(memo_sum)(1, 2, 3, 4, 0)
pub fn func5_test() {
recursive.func5(memo_sum)(1, 2, 3, 4, 0)
|> should.equal(10)
}

pub fn use_fix_test() {
pub fn block_test() {
{
use it, f <- recursive.use_fix(10)
use it, f <- recursive.block(10)
fac(it, f)
}
|> should.equal(3_628_800)
}

pub fn use_fix2_test() {
pub fn block2_test() {
{
use it, res, f <- recursive.use_fix2(10, 1)
use it, res, f <- recursive.block2(10, 1)
memo_fac(it, res, f)
}
|> should.equal(3_628_800)
}

pub fn use_fix3_test() {
pub fn block3_test() {
{
use it, curr, prev, f <- recursive.use_fix3(9, 1, 0)
use it, curr, prev, f <- recursive.block3(9, 1, 0)
fib(it, curr, prev, f)
}
|> should.equal(55)
}

pub fn use_fix4_test() {
pub fn block4_test() {
{
use a, b, c, d, f <- recursive.use_fix4(1, 2, 3, 4)
use a, b, c, d, f <- recursive.block4(1, 2, 3, 4)
sum(a, b, c, d, f)
}
|> should.equal(10)
}

pub fn use_fix5_test() {
pub fn block5_test() {
{
use a, b, c, d, res, f <- recursive.use_fix5(1, 2, 3, 4, 0)
use a, b, c, d, res, f <- recursive.block5(1, 2, 3, 4, 0)
memo_sum(a, b, c, d, res, f)
}
|> should.equal(10)
Expand Down

0 comments on commit 3d118fe

Please sign in to comment.