Last active
August 9, 2018 05:01
-
-
Save Tzrlk/538eab1616a2152892d2574837cf1bca to your computer and use it in GitHub Desktop.
A function for splitting an iterable into multiple filtered iterables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrayUtils | |
def group(groupings) | |
return groupings.transform_values { |predicate| self.filter(predicate) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public fun <T> Iterable<T>.group(groupings: Map<String, (T) -> Boolean>): Map<String, Iterable<T>> | |
= groupings.mapValues { ( _, predicate ) -> filter(predicate) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.collect.Iterables; | |
import com.google.common.collect.Maps; | |
public final class IterableUtils { | |
public static <T> Map<String, Iterable<T>> group(Iterable<T> input, Map<String, Predicate<T>> groupings) { | |
return Maps.transformValues(groupings, (predicate) -> Iterables.filter(input, predicate)); | |
} | |
private IterableUtils() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment