Skip to content

A Fixed-point combinator implementation & utility functions for better DX when using it

License

Notifications You must be signed in to change notification settings

Olian04/gleam_recursive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Package Version Hex Docs

recursive

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

gleam add recursive

Recursive block

import gleam/io
import recursive

pub fn main() {
   // Factorial
   io.debug({
    use it, f <- recursive.block(10)
    case it {
      0 -> 1
      _ -> it * f(it - 1)
    }
   })

   // Tail recursive factorial
   io.debug({
    use it, res, f <- recursive.block2(10, 1)
    case it {
      0 -> res
      _ -> f(it - 1, res * it)
    }
   })

   // Tail recursive fibonacci
   io.debug({
    use it, curr, prev, f <- recursive.block3(9, 1, 0)
    case it {
      0 -> curr
      _ -> f(it - 1, curr + prev, curr)
    }
   })
}

Recursive anonymous function

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.func2(fn (it, res, f) {
      case it {
        0 -> res
        _ -> f(it - 1, res * it)
      }
   })
   io.debug(fac(10, 1))

   // Tail recursive fibonacci
   let fib = recursive.func3(fn (it, curr, prev, f) {
      case it {
        0 -> curr
        _ -> f(it - 1, curr + prev, curr)
      }
   })
   io.debug(fac(9, 1, 0))
}

Development

gleam test

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

About

A Fixed-point combinator implementation & utility functions for better DX when using it

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages