curry function

Function curry(
  1. Function fn, {
  2. int argsLength = 2,
})

Implementation

Function curry(Function fn, {int argsLength = 2}) {
  assert(argsLength <= 5);
  return <T, E, A, B, C>([T? a, E? b, A? c, B? d, C? e]) {
    hasPlaceholder(value) => value == LodaConstants.placeholder;
    hasFullValue(args) => List.of(args).length == argsLength;
    hasNotPlaceholder(values) => List.of(values).isEmpty;
    valuesNotNull(args) => args.where(isNotNil);
    valuesHasPlaceholder(args) => args.where(hasPlaceholder);
    final args = valuesNotNull([a, b, c, d, e]).toList();
    final isEligible = flow([
      valuesNotNull,
      and([
        flow([hasFullValue]),
        flow([valuesHasPlaceholder, hasNotPlaceholder])
      ])
    ]);
    if (isEligible([a, b, c, d, e])) {
      return Function.apply(fn, args);
    }
    if (1 == argsLength) return curry1(fn)(a);
    if (2 == argsLength) return curry2(fn)(a, b);
    if (3 == argsLength) return curry3(fn)(a, b, c);
    if (4 == argsLength) return curry4(fn)(a, b, c, d);
    if (5 == argsLength) return curry5(fn)(a, b, c, d, e);
  };
}