All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.basking2.sdsai.itrex.functions.Functions Maven / Gradle / Ivy

There is a newer version: 1.1.23
Show newest version
package com.github.basking2.sdsai.itrex.functions;

import com.github.basking2.sdsai.itrex.SExprRuntimeException;

import java.util.Iterator;
import java.util.function.BiFunction;

/**
 * A collection of utilities for functions.
 */
public class Functions {
    
    public static  AbstractAggregatingFunction aggregator(
        final R initial,
        final BiFunction f
    ) {
        return new AbstractAggregatingFunction(initial) {

            @Override
            public Result applyT(R r, T t) {
                return new Result(true, f.apply(r, t));
            }

        };
    }

    public static  T getArgument(final Iterator iterator, final String argumentName) {
        if (!iterator.hasNext()) {
            throw new SExprRuntimeException("Function missing required argument "+argumentName+".");
        }

        final Object o = iterator.next();

        @SuppressWarnings("unchecked")
        final T arg = (T) o;

        return arg;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy