Collections
In this section we'll explore ways to use Marcel Collections APIs enhancements
Literals
You can declare collections using the literal array syntax as shown below
List<int> ints = [1, 2, 3, 4]
List list = [1 new Object(), 1..2]
Set<char> chars = [`A`, `C`, `B`, `D`]
Operate on Collections and Arrays
Collections and Arrays have useful functions allowing you to operate on them
map
This operator allows you to map each element of the collection to another.
List<int> ints = [1, 2, 3]
List<float> floats = ints.mapToFloat { it + 1f } // mapping to another collection of primitive
int[] intArray = [3, 4, 5, 6]
List<int> otherInts = intArray.mapToInt { it - 1 }
List list = ints.map { new Foo(list) }
// specified type explicitely
List list2 = list.map { Foo foo -> foo.bar }
Note that for collections of primitive you don't have to specify explicitly your parameter's type. But for non-primitive lists, as Marcel doesn't support generic types, you'll have to explicitly declare your lambda's arguments.
find, findAll
The find
operator finds and return the first element of the collection/array matching the provided predicate (or null if no element matched).
findAll
returns a list of all elements matching the given predicate
Set<int> ints = [1, 2, 3]
int myInt = ints.find { it == 2 }
int[] intArray = [3, 4, 5, 6]
int myInt2 = intArray.find { it == 5 }
List<int> myInts = ints.findAll { it >= 2 }
List list = [1..1, 2..3, 3..4]
IntRange myRange = list.find { IntRange range -> range.contains(4) }
List myRanges = list.findAll { IntRange range -> range.contains(2) }
any, all, none
Those operations return booleans.
- any returns true if at least one element of the collection/array matches the given predicate
- all returns true if all elements of the collection/array matche the given predicate
- none returns true if all elements of the collection/array does not match the given predicate