105
|
1 package alice.jungle.log.example;
|
54
|
2
|
|
3 import java.util.Iterator;
|
56
|
4 import java.util.concurrent.atomic.AtomicInteger;
|
54
|
5
|
|
6 import fj.P2;
|
|
7 import fj.data.List;
|
|
8
|
56
|
9 public class ListTest {
|
54
|
10
|
|
11 public static void main(String[] args) {
|
|
12 List<Integer> list = List.nil();
|
|
13 list = list.cons(2);
|
56
|
14 list = list.cons(3);
|
|
15 list = list.cons(5);
|
|
16 list = list.snoc(1);
|
54
|
17 System.out.println("list :"+list);
|
56
|
18 System.out.println("list.reverse() :"+list.reverse());
|
54
|
19 System.out.println("list.index(0) :"+list.index(0));
|
|
20 System.out.println("list.head() :"+ list.head());
|
|
21 System.out.println("list.tail() :"+list.tail());
|
|
22 System.out.println("list.splitAt(2)");
|
|
23 P2<List<Integer>, List<Integer>> p2 = list.splitAt(2);
|
|
24 List<Integer> list1 = p2._1();
|
|
25 List<Integer> list2 = p2._2();
|
|
26 System.out.println(list1);
|
|
27 System.out.println(list2);
|
|
28
|
56
|
29 final int INSERT_NUM = 4;
|
54
|
30 list = insertInteger(list, INSERT_NUM);
|
|
31 System.out.println("newList :"+ list);
|
56
|
32
|
|
33
|
|
34
|
54
|
35 }
|
|
36
|
|
37 public static List<Integer> insertInteger(List<Integer> list, final int insertNum) {
|
|
38 int count = 0;
|
|
39 for(Iterator<Integer> iter = list.iterator(); iter.hasNext();){
|
|
40 int num = iter.next();
|
|
41 if(insertNum > num ) {
|
|
42 break;
|
|
43 }
|
|
44 count++;
|
|
45 }
|
|
46 P2<List<Integer>, List<Integer>> p2 = list.splitAt(count);
|
|
47 List<Integer> newList = List.nil();
|
|
48 newList = p2._2().cons(insertNum);
|
|
49 newList = p2._1().append(newList);
|
|
50 return newList;
|
|
51 }
|
|
52
|
|
53
|
|
54 }
|