-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arrow Collectors #3280
Arrow Collectors #3280
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collectors.kt
Outdated
Show resolved
Hide resolved
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collectors.kt
Outdated
Show resolved
Hide resolved
Great. Arrow will be more popular 😂. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome 🙌 👏 👏 The implementation of Blocking
is incorrect and has some issues. If it's not clear what I meant, I would be more happy to jump on a quick call, make a commit on this branch, or clear up any doubts you have in this PR
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collectors.kt
Outdated
Show resolved
Hide resolved
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collectors.kt
Outdated
Show resolved
Hide resolved
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Flow.kt
Outdated
Show resolved
Hide resolved
* so it's potential faster than [Collectors.map]. | ||
*/ | ||
@Suppress("UnusedReceiverParameter") | ||
public fun <K, V> Collectors.concurrentMap(): Collector<Pair<K, V>, ConcurrentMap<K, V>> = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice JVM specific addition! 🙌
public fun <T, R> java.util.stream.Collector<T, *, R>.asCollector(): Collector<T, R> = | ||
Collectors.jvm(this) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collect.kt
Outdated
Show resolved
Hide resolved
…lectors/Collect.kt Co-authored-by: Petrus Nguyễn Thái Học <hoc081098@gmail.com>
Ready for review! If this gets merged, I’ll happily write docs for the Arrow Website. And even some sort of blog post, if you want! |
arrow-libs/fx/arrow-collectors/src/commonMain/kotlin/arrow/collectors/Collector.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me @serras!! Thank you for the amazing work 🙌 👏 🥳
nodejs { | ||
testTask { | ||
useMocha { | ||
timeout = "60s" | ||
} | ||
} | ||
} | ||
browser { | ||
testTask { | ||
useKarma { | ||
useChromeHeadless() | ||
timeout.set(Duration.ofMinutes(1)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off-topic: I think would be good if we can put this in some common configuration at some point. Still not sure what's the best way to do that in Gradle 🙃
* Java's [`Collector`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html) | ||
* and Haskell's [`foldl` library](https://hackage.haskell.org/package/foldl/docs/Control-Foldl.html). | ||
*/ | ||
public interface CollectorI<InternalAccumulator, in Value, out Result> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Not a huge fan of CollectorI
but not sure if something else makes sense.. I guess InternalAccumulator
is State
? What about StateCollector<State, in Value, out Result>
, or StatefulCollector
? Not sure if this makes sense though, since all Collector
have State
. We had a similar issue with Schedule when we originally designed it, but don't think we can take the same/similar approach to solve this issue.
This is just a nit, feel free to ignore. For me it's also fine without any changes, just wanted to leave some thoughts.
Going to merge. Feel free to discuss further on my nit remark, or to ignore completely |
Documentation for arrow-kt/arrow#3280
This is an idea for a new library heavily influenced by Java's
Collector
and Haskell'sfoldl
library. The goal is to build complex computations over sequences of values, guaranteeing that those values are consumed only once.For example, if you want to compute the average of a sequence in one go, you can write:
The "trick" is that collectors have a nice interface based on
map
/contramap
/zip
(they are applicative functors and profunctors, if we were to use fancy words). Combined with Kotlin's coldFlow
s, they provide a nice combination.Since this is a pattern coming from functional programming, I thought that it may be a good fit for Arrow.