Mercurial > hg > Members > nobuyasu > jungle-network
changeset 54:3d78d438f97b
add ListTest
author | one |
---|---|
date | Sun, 14 Jul 2013 17:21:37 +0900 |
parents | f47a02368099 |
children | 25edf76b65dc |
files | src/jungle/test/functionalJava/ListTest.java |
diffstat | 1 files changed, 48 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/functionalJava/ListTest.java Sun Jul 14 17:21:37 2013 +0900 @@ -0,0 +1,48 @@ +package jungle.test.functionalJava; + +import java.util.Iterator; + +import fj.P2; +import fj.data.List; + +public class ListTest{ + + public static void main(String[] args) { + List<Integer> list = List.nil(); + list = list.cons(1); + list = list.cons(2); + list = list.cons(4); + System.out.println("list :"+list); + System.out.println("list.index(0) :"+list.index(0)); + System.out.println("list.head() :"+ list.head()); + System.out.println("list.tail() :"+list.tail()); + System.out.println("list.splitAt(2)"); + P2<List<Integer>, List<Integer>> p2 = list.splitAt(2); + List<Integer> list1 = p2._1(); + List<Integer> list2 = p2._2(); + System.out.println(list1); + System.out.println(list2); + + final int INSERT_NUM = 3; + list = insertInteger(list, INSERT_NUM); + System.out.println("newList :"+ list); + } + + public static List<Integer> insertInteger(List<Integer> list, final int insertNum) { + int count = 0; + for(Iterator<Integer> iter = list.iterator(); iter.hasNext();){ + int num = iter.next(); + if(insertNum > num ) { + break; + } + count++; + } + P2<List<Integer>, List<Integer>> p2 = list.splitAt(count); + List<Integer> newList = List.nil(); + newList = p2._2().cons(insertNum); + newList = p2._1().append(newList); + return newList; + } + + +}