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

com.github.basking2.sdsai.itrex.functions.DictFunction 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.EvaluationContext;

import java.util.*;

/**
 * A function that materializes an iterator into a dictionary and returns it.
 *
 * This is useful for passing arguments into java {@code FunctionInterface}s you write and bind as functions.
 *
 * This is not termed, "Map", as in this language "map" is a function performed on sequences.
 *
 * If an odd number of arguments is given, the last one is set to NULL.
 */
public class DictFunction implements FunctionInterface> {
    @Override
    public Map apply(Iterator objectIterator, final EvaluationContext evaluationContext) {
        final Map dict = new TreeMap();

        while (objectIterator.hasNext()) {
            final Object key = objectIterator.next();
            final Object val;
            if (objectIterator.hasNext()) {
                val = objectIterator.next();
            }
            else {
                val = null;
            }

            dict.put(key, val);
        }

        return dict;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy