Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Deprecate @higherkind & @extension for ListK #276

Merged
merged 49 commits into from Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e47e961
WIP Iterable operators
nomisRev Dec 19, 2020
a239a44
ktlintFormat
nomisRev Dec 19, 2020
76c99d6
Move mapN to ListK.Companion
nomisRev Dec 20, 2020
328568c
Add tupledN
nomisRev Dec 20, 2020
836e4d5
Add Apply#product
nomisRev Dec 20, 2020
e701e61
Add Order & generated code
nomisRev Dec 20, 2020
cba844c
Applicative
nomisRev Dec 20, 2020
7c3bd94
Align
nomisRev Dec 20, 2020
6187960
Apply
nomisRev Dec 20, 2020
5964b9e
ktlintFormat
nomisRev Dec 20, 2020
6860fff
Alternative
nomisRev Dec 21, 2020
250bc84
Remove casting, and fix warnings
nomisRev Dec 21, 2020
13f94a5
Merge branch 'master' into sv-deprecate-extensions-list
nomisRev Dec 21, 2020
be7ea09
Crosswalk
nomisRev Dec 21, 2020
be0257a
Eq
nomisRev Dec 21, 2020
c55cb1e
EqK
nomisRev Dec 21, 2020
b24866f
Foldable
nomisRev Dec 21, 2020
aa44b1f
Functor
nomisRev Dec 21, 2020
cd2dcbe
FunctorFilter
nomisRev Dec 21, 2020
1a6aefc
Hash
nomisRev Dec 21, 2020
786b5b6
Monad
nomisRev Dec 21, 2020
29885ce
MonadCombine
nomisRev Dec 21, 2020
285c618
MonadFilter
nomisRev Dec 21, 2020
91e7e78
ListK & MonadLogic
nomisRev Dec 21, 2020
2b908d7
ktlintFormat
nomisRev Dec 21, 2020
0510ed3
MonadPlus
nomisRev Dec 21, 2020
ea71818
Monoid
nomisRev Dec 21, 2020
2723c31
Monoidal
nomisRev Dec 21, 2020
ffffcf4
MonoidK
nomisRev Dec 21, 2020
92fbfe0
Order
nomisRev Dec 21, 2020
40ca7ba
Semialign
nomisRev Dec 21, 2020
3c55792
ktlintFormat
nomisRev Dec 21, 2020
f6b617d
Merge branch 'sv-deprecate-extensions-list' of github.com:arrow-kt/ar…
nomisRev Dec 21, 2020
194db51
Semigroup
nomisRev Jan 4, 2021
3e963e6
Semigroupal
nomisRev Jan 4, 2021
116f059
SemigroupK
nomisRev Jan 4, 2021
41a3919
Show
nomisRev Jan 4, 2021
a25593f
Traverse
nomisRev Jan 4, 2021
de15da4
Unalign
nomisRev Jan 4, 2021
b5bb9ee
Unzip
nomisRev Jan 4, 2021
dffb31b
Zip
nomisRev Jan 4, 2021
53edea0
Merge remote-tracking branch 'origin/master' into sv-deprecate-extens…
nomisRev Jan 4, 2021
fb4e7a0
Fix docs
nomisRev Jan 4, 2021
d9a1eaf
Merge branch 'master' into sv-deprecate-extensions-list
raulraja Jan 4, 2021
350143e
ktlint
raulraja Jan 4, 2021
6bb6dec
Update arrow-core/src/main/kotlin/arrow/core/extensions/list/monad/Li…
nomisRev Jan 5, 2021
05fd42d
Deprecate List object
nomisRev Jan 5, 2021
703f540
Fix traverseEither_ short-circuit
nomisRev Jan 5, 2021
a5514ff
Kotlin Std doesn't use `With` postfix to denote lambda
nomisRev Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
781 changes: 781 additions & 0 deletions arrow-core-data/src/main/kotlin/arrow/core/Iterable.kt

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions arrow-core-data/src/main/kotlin/arrow/core/List.kt
@@ -0,0 +1,119 @@
package arrow.core

import arrow.typeclasses.Hash
import arrow.typeclasses.Monoid
import arrow.typeclasses.Order
import arrow.typeclasses.Show
import arrow.typeclasses.defaultSalt
import arrow.typeclasses.hashWithSalt

fun <A> listHash(HA: Hash<A>): Hash<List<A>> =
ListHash(HA)

private class ListHash<A>(private val HA: Hash<A>) : Hash<List<A>> {
override fun List<A>.hash(): Int = hash(HA)
override fun List<A>.hashWithSalt(salt: Int): Int = hashWithSalt(HA, salt)
}

fun <A> List<A>.hash(HA: Hash<A>): Int =
hashWithSalt(HA, defaultSalt)

fun <A> List<A>.hashWithSalt(HA: Hash<A>, salt: Int): Int = HA.run {
fold(salt) { hash, x -> x.hashWithSalt(hash) }.hashWithSalt(size)
}

fun <A> List<A>.compare(OA: Order<A>, b: List<A>): Ordering = OA.run {
align(b) { ior -> ior.fold({ GT }, { LT }, { a1, a2 -> a1.compare(a2) }) }
.fold(Ordering.monoid())
}

/**
* Check if [this@lt] is `lower than` [b]
*
* @receiver object to compare with [b]
* @param b object to compare with [this@lt]
* @returns true if [this@lt] is `lower than` [b] and false otherwise
*/
fun <A> List<A>.lt(OA: Order<A>, b: List<A>): Boolean =
compare(OA, b) == LT

/**
* Check if [this@lte] is `lower than or equal to` [b]
*
* @receiver object to compare with [b]
* @param b object to compare with [this@lte]
* @returns true if [this@lte] is `lower than or equal to` [b] and false otherwise
*/
fun <A> List<A>.lte(OA: Order<A>, b: List<A>): Boolean =
compare(OA, b) != GT

/**
* Check if [this@gt] is `greater than` [b]
*
* @receiver object to compare with [b]
* @param b object to compare with [this@gt]
* @returns true if [this@gt] is `greater than` [b] and false otherwise
*/
fun <A> List<A>.gt(OA: Order<A>, b: List<A>): Boolean =
compare(OA, b) == GT

/**
* Check if [this@gte] is `greater than or equal to` [b]
*
* @receiver object to compare with [b]
* @param b object to compare with [this@gte]
* @returns true if [this@gte] is `greater than or equal to` [b] and false otherwise
*/
fun <A> List<A>.gte(OA: Order<A>, b: List<A>): Boolean =
compare(OA, b) != LT

/**
* Determines the maximum of [this@max] and [b] in terms of order.
*
* @receiver object to compare with [b]
* @param b object to compare with [this@max]
* @returns the maximum [this@max] if it is greater than [b] or [b] otherwise
*/
fun <A> List<A>.max(OA: Order<A>, b: List<A>): List<A> =
if (gt(OA, b)) this else b

/**
* Determines the minimum of [this@min] and [b] in terms of order.
*
* @receiver object to compare with [b]
* @param b object to compare with [this@min]
* @returns the minimum [this@min] if it is less than [b] or [b] otherwise
*/
fun <A> List<A>.min(OA: Order<A>, b: List<A>): List<A> =
if (lt(OA, b)) this else b

/**
* Sorts [this@sort] and [b] in terms of order.
*
* @receiver object to compare with [b]
* @param b object to compare with [this@sort]
* @returns a sorted [Tuple2] of [this@sort] and [b].
*/
fun <A> List<A>.sort(OA: Order<A>, b: List<A>): Tuple2<List<A>, List<A>> =
if (gte(OA, b)) Tuple2(this, b) else Tuple2(b, this)

fun <A> listOrder(OA: Order<A>): Order<List<A>> =
ListOrder(OA)

private class ListOrder<A>(private val OA: Order<A>) : Order<List<A>> {
override fun List<A>.compare(b: List<A>): Ordering = compare(OA, b)
}

fun <A> listMonoid(): Monoid<List<A>> =
ListMonoid as Monoid<List<A>>

object ListMonoid : Monoid<List<Any?>> {
override fun empty(): List<Any?> = emptyList()
override fun List<Any?>.combine(b: List<Any?>): List<Any?> = this + b
}

fun <A> listShow(SA: Show<A>): Show<List<A>> =
object : Show<List<A>> {
override fun List<A>.show(): String =
show(SA)
}
233 changes: 227 additions & 6 deletions arrow-core-data/src/main/kotlin/arrow/core/ListK.kt
Expand Up @@ -472,6 +472,233 @@ data class ListK<out A>(private val list: List<A>) : ListKOf<A>, List<A> by list
rs.isEmpty() -> ls.map { it.leftIor() }
else -> listOf(Ior.Both(ls.first(), rs.first())) + alignRec(ls.drop(1), rs.drop(1))
}

inline fun <B, C, D> mapN(
b: Iterable<B>,
c: Iterable<C>,
map: (B, C) -> D
): List<D> =
mapN(b, c, unit, unit, unit, unit, unit, unit, unit, unit) { b, c, _, _, _, _, _, _, _, _ -> map(b, c) }

inline fun <B, C, D, E> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
map: (B, C, D) -> E
): List<E> =
mapN(b, c, d, unit, unit, unit, unit, unit, unit, unit) { b, c, d, _, _, _, _, _, _, _ -> map(b, c, d) }

inline fun <B, C, D, E, F> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
map: (B, C, D, E) -> F
): List<F> =
mapN(b, c, d, e, unit, unit, unit, unit, unit, unit) { b, c, d, e, _, _, _, _, _, _ -> map(b, c, d, e) }

inline fun <B, C, D, E, F, G> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
map: (B, C, D, E, F) -> G
): List<G> =
mapN(b, c, d, e, f, unit, unit, unit, unit, unit) { b, c, d, e, f, _, _, _, _, _ -> map(b, c, d, e, f) }

inline fun <B, C, D, E, F, G, H> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
map: (B, C, D, E, F, G) -> H
): List<H> =
mapN(b, c, d, e, f, g, unit, unit, unit, unit) { b, c, d, e, f, g, _, _, _, _ -> map(b, c, d, e, f, g) }

inline fun <B, C, D, E, F, G, H, I> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
map: (B, C, D, E, F, G, H) -> I
): List<I> =
mapN(b, c, d, e, f, g, h, unit, unit, unit) { b, c, d, e, f, g, h, _, _, _ -> map(b, c, d, e, f, g, h) }

inline fun <B, C, D, E, F, G, H, I, J> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>,
map: (B, C, D, E, F, G, H, I) -> J
): List<J> =
mapN(b, c, d, e, f, g, h, i, unit, unit) { b, c, d, e, f, g, h, i, _, _ -> map(b, c, d, e, f, g, h, i) }

inline fun <B, C, D, E, F, G, H, I, J, K> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>,
j: Iterable<J>,
map: (B, C, D, E, F, G, H, I, J) -> K
): List<K> =
mapN(b, c, d, e, f, g, h, i, j, unit) { b, c, d, e, f, g, h, i, j, _ -> map(b, c, d, e, f, g, h, i, j) }

inline fun <B, C, D, E, F, G, H, I, J, K, L> mapN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>,
j: Iterable<J>,
k: Iterable<K>,
map: (B, C, D, E, F, G, H, I, J, K) -> L
): List<L> =
b.flatMap { bb ->
c.flatMap { cc ->
d.flatMap { dd ->
e.flatMap { ee ->
f.flatMap { ff ->
g.flatMap { gg ->
h.flatMap { hh ->
i.flatMap { ii ->
j.flatMap { jj ->
k.map { kk ->
map(bb, cc, dd, ee, ff, gg, hh, ii, jj, kk)
}
}
}
}
}
}
}
}
}
}

fun <B, C> tupledN(
b: Iterable<B>,
c: Iterable<C>
): List<Tuple2<B, C>> =
mapN(b, c) { b, c ->
Tuple2(b, c)
}

fun <B, C, D> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>
): List<Tuple3<B, C, D>> =
mapN(b, c, d) { b, c, d ->
Tuple3(b, c, d)
}

fun <B, C, D, E> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>
): List<Tuple4<B, C, D, E>> =
mapN(b, c, d, e) { b, c, d, e ->
Tuple4(b, c, d, e)
}

fun <B, C, D, E, F> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>
): List<Tuple5<B, C, D, E, F>> =
mapN(b, c, d, e, f) { b, c, d, e, f ->
Tuple5(b, c, d, e, f)
}

fun <B, C, D, E, F, G> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>
): List<Tuple6<B, C, D, E, F, G>> =
mapN(b, c, d, e, f, g) { b, c, d, e, f, g ->
Tuple6(b, c, d, e, f, g)
}

fun <B, C, D, E, F, G, H> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>
): List<Tuple7<B, C, D, E, F, G, H>> =
mapN(b, c, d, e, f, g, h) { b, c, d, e, f, g, h ->
Tuple7(b, c, d, e, f, g, h)
}

fun <B, C, D, E, F, G, H, I> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>
): List<Tuple8<B, C, D, E, F, G, H, I>> =
mapN(b, c, d, e, f, g, h, i) { b, c, d, e, f, g, h, i ->
Tuple8(b, c, d, e, f, g, h, i)
}

fun <B, C, D, E, F, G, H, I, J> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>,
j: Iterable<J>
): List<Tuple9<B, C, D, E, F, G, H, I, J>> =
mapN(b, c, d, e, f, g, h, i, j) { b, c, d, e, f, g, h, i, j ->
Tuple9(b, c, d, e, f, g, h, i, j)
}

fun <B, C, D, E, F, G, H, I, J, K> tupledN(
b: Iterable<B>,
c: Iterable<C>,
d: Iterable<D>,
e: Iterable<E>,
f: Iterable<F>,
g: Iterable<G>,
h: Iterable<H>,
i: Iterable<I>,
j: Iterable<J>,
k: Iterable<K>
): List<Tuple10<B, C, D, E, F, G, H, I, J, K>> =
mapN(b, c, d, e, f, g, h, i, j, k) { b, c, d, e, f, g, h, i, j, k ->
Tuple10(b, c, d, e, f, g, h, i, j, k)
}
}
}

Expand All @@ -482,9 +709,3 @@ fun <A> List<A>.k(): ListK<A> = ListK(this)

fun <A> listKOf(vararg elements: A): ListK<A> =
listOf(*elements).k()

fun <A, B> Iterable<A>.mapConst(b: B): List<B> =
map { b }

fun <A> Iterable<A>.void(): List<Unit> =
mapConst(Unit)
2 changes: 1 addition & 1 deletion arrow-core-data/src/main/kotlin/arrow/core/Ordering.kt
Expand Up @@ -119,7 +119,7 @@ private object OrderingMonoid : Monoid<Ordering> {
override fun empty(): Ordering = EQ

override fun Ordering.combine(b: Ordering): Ordering =
this.combine(b)
this + b
}

@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
Expand Down
8 changes: 8 additions & 0 deletions arrow-core-data/src/main/kotlin/arrow/core/map.kt
Expand Up @@ -3,3 +3,11 @@ package arrow.core
object MapInstances

object SortedMapInstances

fun <K, A, B> Map<K, A>.align(b: Map<K, B>): Map<K, Ior<A, B>> =
(keys + b.keys).mapNotNull { key ->
Ior.fromNullables(this@align[key], b[key])?.let { key to it }
}.toMap()

fun <K, A, B, C> Map<K, A>.align(b: Map<K, B>, fa: (Ior<A, B>) -> C): Map<K, C> =
this.align(b).mapValues { (_, v) -> fa(v) }