Java:``` import javax.script.*; import jdk.nashorn...
# announcements
d
Java:
Copy code
import javax.script.*;
import jdk.nashorn.api.scripting.*;
import java.util.*;
import java.util.function.*;

public class scratch_6 {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine e = m.getEngineByName("nashorn");

        // The following JSObject wraps this list
        List<Object> l = new ArrayList<>();
        l.add("hello");
        l.add("world");
        l.add(true);
        l.add(1);

        JSObject jsObj = new AbstractJSObject() {
            @Override
            public Object getMember(String name) {
                if (name.equals("map")) {
                    // return a functional interface object - nashorn will treat it like
                    // script function!
                    return (Function<JSObject, Object>)callback -> {
                        List<Object> res = new ArrayList<>();
                        for (Object obj : l) {
                            // call callback on each object and add the result to new list
                            res.add(callback.call(null, obj));
                        }

                        // return fresh list as result of map (or this could be another wrapper)
                        return res;
                    };
                } else {
                    // unknown property
                    return null;
                }
            }
        };

        e.put("obj", jsObj);
        // map each String to it's uppercase and print result of map
        e.eval("print(obj.map(function(x) '\"'+x.toString()+'\"'))");
    }
}
Kotlin:
Copy code
package com.jsonlogic

import javax.script.*
import jdk.nashorn.api.scripting.*
import java.util.*
import java.util.function.Function

fun main(args: Array<String>) {
    val m = ScriptEngineManager()
    val e = m.getEngineByName("nashorn")

    // The following JSObject wraps this list
    val l = mutableListOf<Any>()
    l.add("hello")
    l.add("world")
    l.add(true)
    l.add(1)

    val jsObj = object : AbstractJSObject() {
        override fun getMember(name: String?): Any? {
            if (name == "map") {
                // return a functional interface object - nashorn will treat it like
                // script function!
                return { callback: Function<Any?, Any?> ->
                    val res = ArrayList<Any?>()
                    for (obj in l) {
                        // call callback on each object and add the result to new list
                            res.add(callback.apply(obj))
                    }

                    // return fresh list as result of map (or this could be another wrapper)
                    res
                }
            } else {
                // unknown property
                return null
            }
        }
    }

    e.put("obj", jsObj)
    // map each String to it's uppercase and print result of map
    e.eval("print(obj.map(function(x) '\"'+x.toString()+'\"'))");
}