comparison src/luan/modules/TableLuan.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents b89212fd04b5
children c922446f53aa
comparison
equal deleted inserted replaced
1562:b89212fd04b5 1563:8fbcc4747091
13 13
14 public final class TableLuan { 14 public final class TableLuan {
15 15
16 public static String concat(Luan luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { 16 public static String concat(Luan luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException {
17 int first = i==null ? 1 : i; 17 int first = i==null ? 1 : i;
18 int last = j==null ? list.length() : j; 18 int last = j==null ? list.length(luan) : j;
19 StringBuilder buf = new StringBuilder(); 19 StringBuilder buf = new StringBuilder();
20 for( int k=first; k<=last; k++ ) { 20 for( int k=first; k<=last; k++ ) {
21 Object val = list.get(luan,k); 21 Object val = list.get(luan,k);
22 if( val==null ) 22 if( val==null )
23 break; 23 break;
24 if( sep!=null && k > first ) 24 if( sep!=null && k > first )
25 buf.append(sep); 25 buf.append(sep);
26 String s = Luan.luanToString(val); 26 String s = luan.luanToString(val);
27 buf.append(s); 27 buf.append(s);
28 } 28 }
29 return buf.toString(); 29 return buf.toString();
30 } 30 }
31 31
44 44
45 private static interface LessThan { 45 private static interface LessThan {
46 public boolean isLessThan(Object o1,Object o2); 46 public boolean isLessThan(Object o1,Object o2);
47 } 47 }
48 48
49 public static void sort(LuanTable list,final LuanFunction comp) throws LuanException { 49 public static void sort(Luan luan,LuanTable list,final LuanFunction comp) throws LuanException {
50 if( list.getMetatable() != null ) 50 if( list.getMetatable() != null )
51 throw new LuanException("can't sort a table with a metatable"); 51 throw new LuanException("can't sort a table with a metatable");
52 final LessThan lt; 52 final LessThan lt;
53 if( comp==null ) { 53 if( comp==null ) {
54 lt = new LessThan() { 54 lt = new LessThan() {
55 public boolean isLessThan(Object o1,Object o2) { 55 public boolean isLessThan(Object o1,Object o2) {
56 try { 56 try {
57 return Luan.isLessThan(o1,o2); 57 return luan.isLessThan(o1,o2);
58 } catch(LuanException e) { 58 } catch(LuanException e) {
59 throw new LuanRuntimeException(e); 59 throw new LuanRuntimeException(e);
60 } 60 }
61 } 61 }
62 }; 62 };
63 } else { 63 } else {
64 lt = new LessThan() { 64 lt = new LessThan() {
65 public boolean isLessThan(Object o1,Object o2) { 65 public boolean isLessThan(Object o1,Object o2) {
66 try { 66 try {
67 return Luan.checkBoolean(Luan.first(comp.call(o1,o2))); 67 return Luan.checkBoolean(Luan.first(comp.call(luan,o1,o2)));
68 } catch(LuanException e) { 68 } catch(LuanException e) {
69 throw new LuanRuntimeException(e); 69 throw new LuanRuntimeException(e);
70 } 70 }
71 } 71 }
72 }; 72 };
93 int to; 93 int to;
94 if( iTo != null ) { 94 if( iTo != null ) {
95 to = iTo; 95 to = iTo;
96 } else { 96 } else {
97 Integer n = Luan.asInteger( tbl.get(luan,"n") ); 97 Integer n = Luan.asInteger( tbl.get(luan,"n") );
98 to = n!=null ? n : tbl.length(); 98 to = n!=null ? n : tbl.length(luan);
99 } 99 }
100 List<Object> list = new ArrayList<Object>(); 100 List<Object> list = new ArrayList<Object>();
101 for( int i=from; i<=to; i++ ) { 101 for( int i=from; i<=to; i++ ) {
102 list.add( tbl.get(luan,i) ); 102 list.add( tbl.get(luan,i) );
103 } 103 }