| ## sequence.av |
| |
| let a = seq.array(int, 1, 2, 3, 4); |
| let r = range(-5, 5); |
| let s = seq.set(99, 100, 101); |
| let m = seq.map("a", 1, "b", 2, "c", 3); |
| let n = seq.list("car", "bus", "bike"); |
| |
| ## iterate elements |
| let sum = 0 ; |
| for e in r { |
| sum = sum + e; |
| } |
| println("sum of range r: " + sum); |
| println("print map:"); |
| for e in m { |
| println(e.key + "=" + e.value); |
| } |
| |
| ## count |
| println("count of array: " + count(a)); |
| println("count of range: " + count(r)); |
| println("count of set: " + count(s)); |
| println("count of map: " + count(m)); |
| println("count of list: " + count(n)); |
| |
| ## is_empty |
| println("is_empty(array): " + is_empty(a)); |
| println("is_empty(seq.list()): " + is_empty(seq.list())); |
| println("is_empty(nil): " + is_empty(nil)); |
| |
| ## include |
| println("array has 3: " + include(a, 3)); |
| println("map has an entry ('b', 2): " + include(m, seq.entry("b", 2))); |
| println("range has 10: " + include(r, 10)); |
| |
| ## map |
| let new_range = map(r, lambda(x) -> x + 1 end); |
| print("new range is: "); |
| for x in new_range { |
| print(x); |
| print(", "); |
| } |
| println() |
| let new_map = map(m, lambda(e) -> e.value = e.value + 100; return e; end); |
| println("new map is: " + new_map + ", and type is: "+ type(new_map)); |
| |
| ## into |
| let new_map = into(seq.map(), new_map); |
| println("new map is: " + new_map + ", and type is: "+ type(new_map)); |
| let new_set = into(seq.set(), a); |
| println("new set is: " + new_set + ", and type is: "+ type(new_set)); |
| |
| ## reduce |
| let sum_of_a = reduce(a, +, 0); |
| let sum_of_r = reduce(r, +, 0); |
| println("some of array is: " + sum_of_a); |
| println("some of range is: " + sum_of_r); |
| let len = reduce(n, lambda(len, x) -> len + count(x) end, 0); |
| println("total string length in list is: " + len); |
| |
| fn mymap(seq, func) { |
| reduce(seq, |
| lambda(c, e) -> |
| seq.add(c, func(e)) |
| end, |
| seq.list()) |
| } |
| println("test mymap: " + mymap(a, lambda(x) -> x * 2 end)); |
| |
| ## sort |
| println("sort(list) is: " + sort(n)); |
| println("sort(set) is: " + sort(into(seq.list(), s))); |
| |
| let c = comparator(lambda(x, y) -> x > y end); |
| println("sort(list, c) is: " + sort(n, c)); |
| |
| ## filter |
| let es = filter(r, lambda(x) -> x %2 == 0 end); |
| println("filter even number in range:" + es); |
| let bs = filter(n, lambda(x) -> string.startsWith(x, "b") end); |
| println("bs is: " + bs); |
| |
| ## seq.every |
| println("every element in array is greater than zero: " |
| + seq.every(a, lambda(x) -> x > 0 end)); |
| println("every element in range is greater than zero: " |
| + seq.every(r, lambda(x) -> x > 0 end)); |
| |
| ## seq.not_any |
| println("There are not any elements in array is less than zero: " |
| + seq.not_any(a, lambda(x) -> x < 0 end)); |
| println("There are not any elements in range is less than zero: " |
| + seq.not_any(r, lambda(x) -> x < 0 end)); |
| |
| ## seq.some |
| println("Find a element in array is greater than zero: " |
| + seq.some(a, lambda(x) -> x > 0 end)); |
| println("Find a element in range is greater than zero: " |
| + seq.some(r, lambda(x) -> x > 0 end)); |
| println("Find a element in list is starting with 'c': " |
| + seq.some(n, lambda(x) -> string.startsWith(x, "c") end)); |