-
Notifications
You must be signed in to change notification settings - Fork 454
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
Add wrapper class for Set #247
Conversation
Codecov Report
@@ Coverage Diff @@
## master #247 +/- ##
=========================================
Coverage ? 41.69%
Complexity ? 259
=========================================
Files ? 136
Lines ? 3149
Branches ? 410
=========================================
Hits ? 1313
Misses ? 1710
Partials ? 126
Continue to review full report at Codecov.
|
package kategory | ||
|
||
@higherkind | ||
@deriving(Monad::class, Traverse::class, MonoidK::class) |
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.
Set
can't have neither Monad
nor Traverse
instances. Just Foldable
and MonoidK
because sets don't guarantee order of iteration and that is why flatMap
gives you back an Iterable. We should only have those two mentioned instances
@deriving(Monad::class, Traverse::class, MonoidK::class) | ||
data class SetKW<A>(val set: Set<A>) : SetKWKind<A>, Set<A> by set { | ||
|
||
fun <B> flatMap(f: (A) -> SetKWKind<B>): SetKW<B> = this.set.flatMap { f(it).ev().set }.toSet().k() |
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.
🔥 remove as this would yield nondeterministic results.
return Eval.defer { loop(this) } | ||
} | ||
|
||
fun <G, B> traverse(f: (A) -> HK<G, B>, GA: Applicative<G>): HK<G, SetKW<B>> = |
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.
🔥 remove as this would yield nondeterministic results.
|
||
fun <B> flatMap(f: (A) -> SetKWKind<B>): SetKW<B> = this.set.flatMap { f(it).ev().set }.toSet().k() | ||
|
||
fun <B> map(f: (A) -> B): SetKW<B> = this.set.map(f).toSet().k() |
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.
🔥
GA.map2Eval(f(a), eval) { (setOf(it.a) + it.b).k() } | ||
}.value() | ||
|
||
fun <B, Z> map2(fb: SetKWKind<B>, f: (Tuple2<A, B>) -> Z): SetKW<Z> = |
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.
🔥
} | ||
} | ||
|
||
fun <A, B> tailRecM(a: A, f: (A) -> HK<SetKWHK, Either<A, B>>): SetKW<B> { |
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.
🔥
return SetKW(buf.toSet()) | ||
} | ||
|
||
fun functor(): SetKWHKMonadInstance = SetKW.monad() |
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.
🔥
|
||
fun functor(): SetKWHKMonadInstance = SetKW.monad() | ||
|
||
fun applicative(): SetKWHKMonadInstance = SetKW.monad() |
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.
🔥
I've updated the PR with the comments :) |
fun <B> foldR(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> { | ||
fun loop(fa_p: SetKW<A>): Eval<B> = when { | ||
fa_p.set.isEmpty() -> lb | ||
else -> f(fa_p.set.first(), Eval.defer { loop(fa_p.set.drop(1).toSet().k()) }) |
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.
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.
Awesome work, Let's make A
covariant in SetKW
if possible in the same way is covariant in Set
|
||
@higherkind | ||
@deriving(Foldable::class, MonoidK::class) | ||
data class SetKW<A>(val set: Set<A>) : SetKWKind<A>, Set<A> by set { |
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.
Can A
be covariant here?
done @raulraja :D thanks for the help |
@victorg1991 can you remerge master, we changed the modules location |
Done @raulraja 👍 |
Addressing #203