-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e0471f4
Add wrapper for Set
victorg1991 de3036b
Add Set instances
victorg1991 4aaf9c7
Add SetKW tests
victorg1991 b53332c
SetKW can’t have Monad nor Traverse
victorg1991 aac6e67
Update MonoidK and SemigroupK laws to use a HK creation function
victorg1991 fcc4f46
Update SetKW tests
victorg1991 e6d152b
Merge branch 'master' into victorg-setkw
raulraja d88e4f5
Make SetWK covariant
victorg1991 4bd6e0f
Merge branch 'master' into victorg-setkw
victorg1991 fb0c924
Merge branch 'master' into victorg-setkw
victorg1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kategory | ||
|
||
@higherkind | ||
@deriving(Foldable::class, MonoidK::class) | ||
data class SetKW<out A>(val set: Set<A>) : SetKWKind<A>, Set<A> by set { | ||
|
||
fun <B> foldL(b: B, f: (B, A) -> B): B = this.fold(b, f) | ||
|
||
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()) }) | ||
} | ||
return Eval.defer { loop(this) } | ||
} | ||
|
||
companion object { | ||
|
||
fun <A> pure(a: A): SetKW<A> = setOf(a).k() | ||
|
||
fun <A> empty(): SetKW<A> = emptySet<A>().k() | ||
|
||
fun <A> semigroup(): SetKWMonoid<A> = object : SetKWMonoid<A> {} | ||
|
||
fun semigroupK(): SetKWHKMonoidKInstance = SetKW.monoidK() | ||
} | ||
} | ||
|
||
fun <A> SetKW<A>.combineK(y: SetKWKind<A>): SetKW<A> = (this.set + y.ev().set).k() | ||
|
||
fun <A> Set<A>.k(): SetKW<A> = SetKW(this) |
8 changes: 8 additions & 0 deletions
8
kategory/src/main/kotlin/kategory/instances/SetKWInstances.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package kategory | ||
|
||
interface SetKWMonoid<A> : Monoid<SetKW<A>> { | ||
override fun combine(a: SetKW<A>, b: SetKW<A>): SetKW<A> = (a + b).k() | ||
|
||
override fun empty(): SetKW<A> = emptySet<A>().k() | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package kategory | ||
|
||
import io.kotlintest.KTestJUnitRunner | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(KTestJUnitRunner::class) | ||
class SetKWTest : UnitSpec() { | ||
|
||
init { | ||
testLaws(SemigroupKLaws.laws(SetKW.semigroupK(), { SetKW.pure(it) }, Eq.any())) | ||
testLaws(MonoidKLaws.laws(SetKW.monoidK(), { SetKW.pure(it) }, Eq.any())) | ||
testLaws(FoldableLaws.laws(SetKW.foldable(), { SetKW.pure(it) }, Eq.any())) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
SetKW.foldR
has the same problem likeListKW.foldR
related in #240, seems it's a problem of Eval #252