You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The methods of Kilo's Optionals class are provided as a less verbose alternative to similar methods defined by java.util.Optional. For example, where you might write something like this using Optional:
it could be written more simply using Optionals as follows (similar to the safe call feature of other languages):
varresult = map("hello", String::length); // 5
Similarly, Kilo's Iterables class offers a less complex alternative to the methods of java.util.stream.Stream. For example, where you might write something like this using Stream:
For a slightly more involved example, consider this code that groups a list of strings by length and then collects the result to a sorted map of string length to string count:
With the Stream version, you need to go to the end of the (very confusing) call chain to even see what type result will be. With the Iterables approach, it's immediately obvious that result will be a sorted map of length values to size values.
Another advantage of Iterables is that, unlike a Stream, an Iterable can be traversed using a traditional for-each loop. Developers are not forced to choose between one paradigm or the other.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The methods of Kilo's
Optionalsclass are provided as a less verbose alternative to similar methods defined byjava.util.Optional. For example, where you might write something like this usingOptional:with
Optionalsit can be written as follows (similar to the null-coalescing or "Elvis" operator found in other languages):Whereas this code could be used to determine the length of an optional string:
it could be written more simply using
Optionalsas follows (similar to the safe call feature of other languages):Similarly, Kilo's
Iterablesclass offers a less complex alternative to the methods ofjava.util.stream.Stream. For example, where you might write something like this usingStream:it could be written as follows using
Iterables:Whereas this code could be used to transform list of strings to a list of string lengths:
it could be written more simply using
Iterablesas follows:For a slightly more involved example, consider this code that groups a list of strings by length and then collects the result to a sorted map of string length to string count:
With
Iterables, it could be written as follows:With the
Streamversion, you need to go to the end of the (very confusing) call chain to even see what typeresultwill be. With theIterablesapproach, it's immediately obvious thatresultwill be a sorted map of length values to size values.Another advantage of
Iterablesis that, unlike aStream, anIterablecan be traversed using a traditional for-each loop. Developers are not forced to choose between one paradigm or the other.For more information, see the README.
Beta Was this translation helpful? Give feedback.
All reactions