Exemple #1
0
def n_ary_curry_test():
    assert_equal(n_args(takes_one_arg), 1)
    assert_equal(n_args(takes_three_args), 3)
    assert_equal(n_args(takes_two_args), 2)

    assert_equal(takes_one_arg(1), [1, None])
    try:
        takes_one_arg(1, 2, 3)
        assert False, "takes_one_arg(1,2,3) should throw"
    except TypeError:
        pass
    try:
        takes_one_arg()
        assert False, "takes_one_arg() should throw"
    except TypeError:
        pass

    assert_equal(takes_no_arg(), [None, None])

    try:
        takes_no_arg(1)
        assert False, "takes_no_arg(1) should throw"
    except TypeError:
        pass

    try:
        n_ary(-1)(lambda: 1)
        assert False, "n_ary(-1)(print) should throw"
    except ValueError:
        pass

    assert_equal(n_ary(20)(lambda: 1)(*range(0, 20)), 1)
Exemple #2
0
def curry_n(n, f):
    """Returns a curried equivalent of the provided function, with the specified
    arity. The curried function has two unusual capabilities. First, its
    arguments needn't be provided one at a time. If g is R.curryN(3, f), the
    following are equivalent:
    g(1)(2)(3)
    g(1)(2, 3)
    g(1, 2)(3)
    g(1, 2, 3)
    Secondly, the special placeholder value R.__ may be used to specify
    "gaps", allowing partial application of any combination of arguments,
    regardless of their positions. If g is as above and _ is R.__,
    the following are equivalent:
    g(1, 2, 3)
    g(_, 2, 3)(1)
    g(_, _, 3)(1)(2)
    g(_, _, 3)(1, 2)
    g(_, 2)(1)(3)
    g(_, 2)(1, 3)
    g(_, 2)(_, 3)(1)"""
    return curry(n_ary(n, f))
Exemple #3
0
def unary(function):
    """Wraps a function of any arity (including nullary) in a function that accepts
    exactly 1 parameter. Any extraneous parameters will not be passed to the
    supplied function"""
    return n_ary(1, function)
Exemple #4
0
import inspect
from ramda.private.asserts import assert_equal
from ramda.n_ary import n_ary


def takes_two_args(a, b):
    return [a, b]


takes_no_arg = n_ary(0, takes_two_args)
takes_one_arg = n_ary(1, takes_two_args)


@n_ary(3)
def takes_three_args(a, b):
    return [a, b]


def n_args(f):
    return len(inspect.signature(f).parameters)


def n_ary_curry_test():
    assert_equal(n_args(takes_one_arg), 1)
    assert_equal(n_args(takes_three_args), 3)
    assert_equal(n_args(takes_two_args), 2)

    assert_equal(takes_one_arg(1), [1, None])
    try:
        takes_one_arg(1, 2, 3)
        assert False, "takes_one_arg(1,2,3) should throw"