Code Monkey home page Code Monkey logo

refunk's Introduction

REFUNK

License JitPack Contributions Coverage Build

REFUNK is small and lightweight library for studying and evaluating primitive recursive functions in Kotlin.
It provides a rich set of methods for defining functions in a more natural way than other functional frameworks and libraries.

An interactive playground is available at https://refunk.yeger.eu.

Installation

Gradle

Show instructions
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
dependencies {
  implementation 'eu.yeger:refunk:{version}'
}

Maven

Show instructions
<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>
<dependency>
  <groupId>eu.yeger</groupId>
    <artifactId>refunk</artifactId>
  <version>{version}</version>
</dependency>

Usage

Basic functions

  • val c = constant(value) with macros const(value), zero and one.
  • val s = Successor() with macro successor.
  • val p = projection(index) with macros first to tenth.

Composition

Function composition is handled by the Composition class and various wrapper methods.

val f = ... 
val g1 = ... 
...
val gn = ...
val myComposition = f of { g1 and ... and gn }
val myAltComp = f(g1, ..., gn)

The example below demonstrates simple composition using the, in this case optional, projections first and second.

// f(x, y) = 2 * (x + y)
val f = multiplication(const(2), addition(first, second)) // `addition(first, second)` is equal to `addition`
val result = f(10, 11) // = 42

Unary functions can be composed with Function::andThen.

val myComposition = myFunction andThen myUnaryFunction

Recursion

Recursions can be defined using multiple extension methods.

val myRecursion = recursive(myRecursiveCaseFunction) withBaseCase myBaseCaseFunction                
...             = recursive { 
                        aFunction of anotherFunction 
                  } withBaseCase { 
                        someFunction andThen someOtherFunction 
                  }

Named projections help using the recursion results, parameters and arguments as well.

val addition = recursive { successor of recursionResult } withBaseCase firstBaseCaseArgument
val predecessor = recursive { recursionParameter } withBaseCase zero

Invocation

The operator Function::invoke evaluates the function for the given arguments.

val addTwo = successor andThen successor
println(addTwo(0)) //prints 2
println(addTwo(40)) //prints 42

val myFunction = predecessor of addition
println(myFunction(3, 40)) //prints 42

Additional information

More examples and various macros can be found here.

Non-recursive implementations

REFUNK also includes non-recursive implementations of commonly used functions.
They are interchangeable with the recursive implementations and provide improved performance (and less StackOverflowErrors).
Using the non-recursive implementations of macros is highly recommended.

Exceptions and error handling

  • Invoking a function will throw an ArityException for invalid amounts of arguments.
  • Projecting a negative index will throw a ProjectionException.
  • Composing functions will throw a CompositionException if the arity of the evaluating function and the number of provided functions do not match.
  • Invoking functions or creating constants with negative values will throw a NaturalNumberException.
  • Any provided method will throw an OverflowException if an overflow occurs during evaluation.

Disclaimer

This library is intended as a tool for studying primitive recursive functions.
Because the implementation is based on experimental Kotlin features, using them in production is not recommended.

refunk's People

Contributors

deryeger avatar renovate-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

refunk's Issues

Rebranding

Change repository, package and project name to refunc. Update readme

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue contains a list of Renovate updates and their statuses.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • Update dependency com.pinterest:ktlint to v0.39.0
  • Update dependency org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin to v1.4.20
  • Update dependency org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable to v1.4.20
  • Update dependency org.jetbrains.kotlin:kotlin-stdlib to v1.4.20

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.