Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

lite-pfix

A partially defined fixpoint combinator.

Maven Central

Install

Insert the following to your build.sbt.

libraryDependencies += "codes.quine.labo" %% "lite-pfix" % "<latest version>"

Usage

PFix is a partially defined fixpoint combinator.

Using this library, we can build a recursive function combining with multiple partial functions. The following is FizzBuzz example:

import codes.quine.labo.pfix.PFix

val fizzbuzz = PFix[Int, List[String]](rec => { case n if n > 0 && n % 15 == 0 => rec(n - 1) ++ List("FizzBuzz") })
val fizz = PFix[Int, List[String]](rec => { case n if n > 0 && n % 3 == 0 => rec(n - 1) ++ List("Fizz") })
val buzz = PFix[Int, List[String]](rec => { case n if n > 0 && n % 5 == 0 => rec(n - 1) ++ List("Buzz") })
val other = PFix[Int, List[String]](rec => { case n if n > 0 => rec(n - 1) ++ List(n.toString) })

val f = fizzbuzz.orElse(fizz).orElse(buzz).orElse(other).toFunction(_ => List.empty)
f(15)
// List("1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz")