Last active
March 22, 2016 10:22
-
-
Save gavinking/6da253657110076bac4c to your computer and use it in GitHub Desktop.
reduceByKey() and foldByKey()
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
shared Map<Key,Item> reduceByKey<Key,Item>( | |
{<Key->Item>+} stream, | |
Item accumulating(Item x, Item y)) | |
given Key satisfies Object | |
given Item satisfies Object | |
=> stream.summarize(Entry.key, | |
(Item? partial, entry) | |
=> let (_->item = entry) | |
if (exists partial) | |
then accumulating(partial, item) | |
else item); | |
shared Map<Key,Result> foldByKey<Key,Item,Result> | |
({<Key->Item>+} stream, Result initial) | |
(Result accumulating(Result x, Item y)) | |
given Key satisfies Object | |
given Result satisfies Object | |
=> stream.summarize(Entry.key, | |
(Result? partial, entry) | |
=> let (_->item = entry) | |
if (exists partial) | |
then accumulating(partial, item) | |
else initial); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment