Skip to content

Instantly share code, notes, and snippets.

@infusion
Last active February 2, 2020 22:10
Show Gist options
  • Save infusion/dac3147a13a8ad6ee0e1 to your computer and use it in GitHub Desktop.
Save infusion/dac3147a13a8ad6ee0e1 to your computer and use it in GitHub Desktop.
An implementation of primitive operations like addition, multiplication and so on - recursively!
/**
* @license Recursive-Primitive.js
*
* Copyright (c) 2014, Robert Eisele ([email protected])
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
const add = (a, b) => b === 0 ? a : add(a + 1, b - 1);
const sub = (a, b) => b === 0 ? a : sub(a - 1, b - 1);
const mul = (a, b) => b === 1 ? a : add(mul(a, b - 1), a);
const div = (a, b) => b === 0 || a < b ? 0 : add(div(sub(a, b), b), 1);
const mod = (a, b) => a < b ? a : mod(sub(a, b), b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment