/**
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2009 by Trifork
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/


package erjang.m.lists;

import erjang.BIF;
import erjang.ENative;
import erjang.EObject;
import erjang.ERT;
import erjang.ESeq;
import erjang.ESmall;
import erjang.ETuple;
import erjang.NotImplemented;

public class Native extends ENative {
	
	@BIF
	public static EObject keymember(EObject key, EObject nth_arg, EObject list_arg) {
		ESmall nth = nth_arg.testSmall();
		ESeq list = list_arg.testSeq();
		
		if (key == null || nth == null | list == null)
				throw ERT.badarg(key, nth_arg, list_arg);

		while (!list.isNil()) {
			EObject elm = list.head();
			ETuple tup = elm.testTuple();

			// test that it is a tuple of the right size
			if (tup != null && tup.arity() >= nth.value) {				
				EObject val = tup.elm(nth.value);
				if (val.equals(key)) { return ERT.TRUE; }
			}
			
			list = list.tail();
		}

		return ERT.FALSE;
	}
	
	@BIF
	public static EObject keyfind(EObject key, EObject nth_arg, EObject list_arg) {
		ESmall nth = nth_arg.testSmall();
		ESeq list = list_arg.testSeq();
		
		if (key == null || nth == null | list == null)
				throw ERT.badarg(key, nth_arg, list_arg);

		while (!list.isNil()) {
			EObject elm = list.head();
			ETuple tup = elm.testTuple();

			// test that it is a tuple of the right size
			if (tup != null && tup.arity() >= nth.value) {				
				EObject val = tup.elm(nth.value);
				if (val.equals(key)) { return tup; }
			}
			
			list = list.tail();
		}

		return ERT.FALSE;
	}
	
}

@@ -699,16 +699,37 @@
 ng.BIF;%0A
+import erjang.EAtom;%0A
 import e
@@ -2105,10 +2105,543 @@
 E;%0A%09%7D%0A%09%0A
+%09@BIF%0A%09public static ESeq reverse(EObject hd, EObject tl) %7B%0A%09%09ESeq res = tl.testSeq();%0A%09%09ESeq front = hd.testSeq();%0A%09%09%0A%09%09if (res == null) throw ERT.badarg(hd, tl);%0A%09%09%0A%09%09while (!front.isNil()) %7B%0A%09%09%09res = res.cons(front.head());%0A%09%09%09front = front.tail();%0A%09%09%7D%0A%09%09%0A%09%09return res;%0A%09%7D%0A%0A%09@BIF%0A%09public static EAtom member(EObject e, EObject l) %7B%0A%09%09ESeq list = l.testSeq();%0A%09%09if (list == null) throw ERT.badarg(e, l);%0A%0A%09%09while (!list.isNil()) %7B%0A%09%09%09if (e.equals(list.head())) return ERT.TRUE;%0A%09%09%09list = list.tail();%0A%09%09%7D%0A%0A%09%09return ERT.FALSE;%0A%09%7D%0A%0A%0A
 %7D%0A

add lists:reverse and lists:member BIFs