package org.metaborg.meta.lang.dynsem.interpreter.utils;

import java.util.LinkedList;

import com.github.krukow.clj_lang.IPersistentStack;
import com.github.krukow.clj_lang.PersistentList;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

public class ListUtils {

	@TruffleBoundary
	public static <T> IPersistentStack<T> reverse(IPersistentStack<T> list) {
		if (list.count() == 0) {
			return list;
		}
		final PersistentList<T> plist = (PersistentList<T>) list;

		final LinkedList<T> temp = new LinkedList<>();
		for (T t : plist) {
			temp.add(t);
		}
		return PersistentList.create(temp);
	}

}

@@ -68,26 +68,55 @@
 va.util.
-LinkedList
+ArrayList;%0Aimport java.util.Collections
 ;%0A%0Aimpor
@@ -445,24 +445,25 @@
 st;%0A%09%09%7D%0A
+%0A
 %09%09final 
 Persiste
@@ -458,26 +458,21 @@
 nal 
-Persistent
+Array
 List%3CT%3E 
 plis
@@ -471,16 +471,33 @@
 %3CT%3E 
-p
+m
 list = 
+new ArrayList%3CT%3E(
 (Per
@@ -520,101 +520,42 @@
 list
+)
 ;%0A%0A%09%09
-final LinkedList%3CT%3E temp = new LinkedList%3C%3E();%0A%09%09for (T t : plist) %7B%0A%09%09%09temp.add(t);%0A%09%09%7D
+Collections.reverse(mlist);%0A
 %0A%09%09r
@@ -586,12 +586,13 @@
 ate(
-temp
+mlist
 );%0A%09

Fix incorrect implementation of list reversal