Skip to content

Instantly share code, notes, and snippets.

@grvcoelho
Last active December 5, 2016 19:43
Show Gist options
  • Save grvcoelho/29914e184075e7609c918cce2f37c400 to your computer and use it in GitHub Desktop.
Save grvcoelho/29914e184075e7609c918cce2f37c400 to your computer and use it in GitHub Desktop.
const curryN = (len, fn, received = []) => (...args) => {
let combined = [...received, ...args]
if (args.length === len) {
return fn(...combined)
}
return curryN(len - args.length, fn, combined)
}
const curry = fn => curryN(fn.length, fn)
const sum = curry((a, b) => a + b)
@grvcoelho
Copy link
Author

WIP

const P = Symbol();
const isPlaceholder = a => a === P


const merge = (dest, origin) => {
    const newArgs = dest.map(i => {
        let elem = i
        
        if (isPlaceholder(i) && origin[0]) {
            elem = origin.shift()
        }
        
        return elem;
    })
    
    return newArgs.concat(origin)
}

const actualLength = (arr = []) => 
    arr.reduce((len, curr) => isPlaceholder(curr) ? len : len + 1, 0);

const curry = (fn, arity = fn.length, received = []) => (...args) => {
    const combined = merge(received, args)
    
    return () => 123;
}

/**
 * Run
 */

const fn = curry((a, b) => a + b)
const a = fn(30)(20)

@grvcoelho
Copy link
Author

grvcoelho commented Dec 5, 2016

const P = Symbol();
const _ = require('lodash')
const isPlaceholder = a => a === P

const merge = (dest, origin) => {
    const newArgs = dest.map(i => {
        let elem = i
        
        if (isPlaceholder(i) && origin[0]) {
            elem = origin.shift()
        }
        
        return elem;
    })
    
    return newArgs.concat(origin)
}

const actualLength = (arr) => 
    arr.reduce((len, curr) => isPlaceholder(curr) ? len : len + 1, 0);

const curry = (fn, arity = fn.length, received = []) => (...args) => {
    const combined = merge(_.clone(received), _.clone(args))
    
    if (actualLength(args) >= arity) {
        return fn(...combined)
    }
    
    return curry(fn, arity - actualLength(args), combined)
}

/**
 * Run
 */

const fn = curry((a, b, c) => a - b * c)
const a = fn(10, P, 3)(5)

a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment