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

com.github.basking2.sdsai.itrex.functions.functional.CallFlattenedFunction Maven / Gradle / Ivy

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

import com.github.basking2.sdsai.itrex.EvaluationContext;
import com.github.basking2.sdsai.itrex.functions.AbstractFunction1;
import com.github.basking2.sdsai.itrex.functions.FunctionInterface;
import com.github.basking2.sdsai.itrex.iterators.IteratorIterator;

import java.util.Arrays;
import java.util.Iterator;

import static com.github.basking2.sdsai.itrex.iterators.Iterators.mapIterator;

/**
 * Call a function with the remaining arguments flattened.
 *
 * So the given code fragment:
 *
 * 
 *     {@code
 *     [callFlattened f [list a b] [list c d]]
 *     }
 * 
* * will result in calling {@code [f a b c d]}. */ public class CallFlattenedFunction extends AbstractFunction1, Object> { @Override protected Object applyImpl( final FunctionInterface objectFunctionInterface, final Iterator rest, final EvaluationContext context ) { /** * Create an iterator that flattens the `rest` argument. */ @SuppressWarnings("unchecked") final Iterator itrItrObj = new IteratorIterator(mapIterator(rest, e -> { if (e instanceof Iterator) { return (Iterator) e; } if (e instanceof Iterable) { return ((Iterable)e).iterator(); } if (e instanceof Object[]) { return Arrays.asList((Object[])e).iterator(); } return Arrays.asList((Object)e).iterator(); })); // Call the function object against the flattened arguments. return objectFunctionInterface.apply(itrItrObj, context); } }