Skip to content

Instantly share code, notes, and snippets.

@gavinking
Last active March 22, 2016 10:22
Show Gist options
  • Save gavinking/6da253657110076bac4c to your computer and use it in GitHub Desktop.
Save gavinking/6da253657110076bac4c to your computer and use it in GitHub Desktop.
reduceByKey() and foldByKey()
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