temp_man
11/19/2018, 8:59 PMSteven McLaughlin
11/19/2018, 9:01 PMmyObjects.filter { it.name == "foo" }.forEach{ //do something }
You could potentially use map instead of forEach too?serebit
11/19/2018, 9:02 PMfoo
, you can do this:
myObjects.find { it.name == "foo" }?.//do something
temp_man
11/19/2018, 9:03 PMSteven McLaughlin
11/19/2018, 9:04 PMserebit
11/19/2018, 9:05 PMtemp_man
11/19/2018, 9:07 PMserebit
11/19/2018, 9:10 PMfor (obj in myObjects) {
if (obj == "bar") {
print(obj)
}
}
and it compiles down to this code in Java:
Iterator var1 = myObjects.iterator();
while(var1.hasNext()) {
String obj = (String)var1.next();
if (Intrinsics.areEqual(obj, "bar")) {
System.out.print(obj);
}
}
myObjects.asSequence().filter { it == "bar" }.forEach { println(it) }
and compiles down to this code in Java:
Sequence $receiver$iv = SequencesKt.filter(CollectionsKt.asSequence((Iterable)myObjects), (Function1)null.INSTANCE);
var1 = $receiver$iv.iterator();
while(var1.hasNext()) {
Object element$iv = var1.next();
String it = (String)element$iv;
System.out.println(it);
}
temp_man
11/19/2018, 9:17 PMserebit
11/19/2018, 9:18 PMSteven McLaughlin
11/19/2018, 9:26 PMasSequence()
actually make that more performant for smaller sets? The performance there depends on the size doesn't it? I think the sequence evaluates it lazily as opposed to eagerly if I remember correctlyserebit
11/19/2018, 9:28 PMasSequence()
, the bytecode will generate a while
loop for each operation performed on the set, and copy the result of each into a destination
list. With asSequence()
, each operation is performed in sequence on each individual member of the list, and so asSequence()
does incur a significant performance benefit with multiple operations chained together.asSequence()
regardless.Steven McLaughlin
11/19/2018, 9:30 PMserebit
11/19/2018, 9:31 PMSteven McLaughlin
11/19/2018, 9:34 PMAndreas Sinz
11/19/2018, 9:40 PMSteven McLaughlin
11/19/2018, 9:41 PMAndreas Sinz
11/19/2018, 9:44 PMList<T>
is the "default" because it is used everywhere, but its just a matter of sequenceOf(...)
vs listOf(...)