|
| 1 | +package kategory.optics |
| 2 | + |
| 3 | +import kategory.Applicative |
| 4 | +import kategory.Either |
| 5 | +import kategory.HK |
| 6 | +import kategory.Option |
| 7 | +import kategory.Tuple2 |
| 8 | +import kategory.flatMap |
| 9 | +import kategory.identity |
| 10 | +import kategory.left |
| 11 | +import kategory.right |
| 12 | +import kategory.toT |
| 13 | + |
| 14 | +/** |
| 15 | + * A [Prism] can be seen as a pair of functions: `reverseGet : B -> A` and `getOrModify: A -> Either<A,B>` |
| 16 | + * |
| 17 | + * - `reverseGet : B -> A` get the source type of a [Prism] |
| 18 | + * - `getOrModify: A -> Either<A,B>` get the target of a [Prism] or return the original value |
| 19 | + * |
| 20 | + * It encodes the relation between a Sum or CoProduct type (sealed class) and one of its element. |
| 21 | + * |
| 22 | + * @param A the source of a [Prism] |
| 23 | + * @param B the target of a [Prism] |
| 24 | + * @property getOrModify from an `B` we can produce an `A` |
| 25 | + * @property reverseGet get the target of a [Prism] or return the original value |
| 26 | + * @constructor Creates a Lens of type `A` with target `B` |
| 27 | + */ |
| 28 | +abstract class Prism<A, B> { |
| 29 | + |
| 30 | + abstract fun getOrModify(a: A): Either<A, B> |
| 31 | + abstract fun reverseGet(b: B): A |
| 32 | + |
| 33 | + companion object { |
| 34 | + operator fun <A,B> invoke(getOrModify: (A) -> Either<A, B>, reverseGet: (B) -> A) = object : Prism<A,B>() { |
| 35 | + override fun getOrModify(a: A): Either<A, B> = getOrModify(a) |
| 36 | + |
| 37 | + override fun reverseGet(b: B): A = reverseGet(b) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the target or nothing if `A` does not match the target |
| 43 | + */ |
| 44 | + fun getOption(a: A): Option<B> = getOrModify(a).toOption() |
| 45 | + |
| 46 | + /** |
| 47 | + * Modify the target of a [Prism] with an Applicative function |
| 48 | + */ |
| 49 | + inline fun <reified F> modifyF(FA: Applicative<F> = kategory.applicative(), crossinline f: (B) -> HK<F, B>, a: A): HK<F, A> = getOrModify(a).fold( |
| 50 | + { FA.pure(it) }, |
| 51 | + { FA.map(f(it), this::reverseGet) } |
| 52 | + ) |
| 53 | + |
| 54 | + /** |
| 55 | + * Modify the target of a [Prism] with a function |
| 56 | + */ |
| 57 | + inline fun modify(crossinline f: (B) -> B): (A) -> A = { |
| 58 | + getOrModify(it).fold(::identity, { reverseGet(f(it)) }) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Modify the target of a [Prism] with a function |
| 63 | + */ |
| 64 | + inline fun modifyOption(crossinline f: (B) -> B): (A) -> Option<A> = { getOption(it).map { b -> reverseGet(f(b)) } } |
| 65 | + |
| 66 | + /** |
| 67 | + * Set the target of a [Prism] with a value |
| 68 | + */ |
| 69 | + fun set(b: B): (A) -> A = modify { b } |
| 70 | + |
| 71 | + infix fun <C> composePrism(other: Prism<B, C>): Prism<A, C> = Prism( |
| 72 | + { a -> getOrModify(a).flatMap { b: B -> other.getOrModify(b).bimap({ set(it)(a) }, ::identity) } }, |
| 73 | + { reverseGet(other.reverseGet(it)) } |
| 74 | + ) |
| 75 | + |
| 76 | + /** |
| 77 | + * Set the target of a [Prism] with a value |
| 78 | + */ |
| 79 | + fun setOption(b: B): (A) -> Option<A> = modifyOption { b } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if there is a target |
| 83 | + */ |
| 84 | + fun isNotEmpty(a: A): Boolean = getOption(a).isDefined |
| 85 | + |
| 86 | + /** |
| 87 | + * Check if there is no target |
| 88 | + */ |
| 89 | + fun isEmpty(a: A): Boolean = !isNotEmpty(a) |
| 90 | + |
| 91 | + /** |
| 92 | + * Find if the target satisfies the predicate |
| 93 | + */ |
| 94 | + inline fun find(crossinline p: (B) -> Boolean): (A) -> Option<B> = { getOption(it).flatMap { if (p(it)) Option.Some(it) else Option.None } } |
| 95 | + |
| 96 | + /** |
| 97 | + * Check if there is a target and it satisfies the predicate |
| 98 | + */ |
| 99 | + inline fun exist(crossinline p: (B) -> Boolean): (A) -> Boolean = { getOption(it).fold({ false }, p) } |
| 100 | + |
| 101 | + /** |
| 102 | + * Check if there is no target or the target satisfies the predicate |
| 103 | + */ |
| 104 | + inline fun all(crossinline p: (B) -> Boolean): (A) -> Boolean = { getOption(it).fold({ true }, p) } |
| 105 | + |
| 106 | + /** |
| 107 | + * Convenience method to create a product of the target and a type C |
| 108 | + */ |
| 109 | + fun <C> first(): Prism<Tuple2<A, C>, Tuple2<B, C>> = Prism( |
| 110 | + { (a, c) -> getOrModify(a).bimap({ it toT c }, { it toT c }) }, |
| 111 | + { (b, c) -> reverseGet(b) toT c } |
| 112 | + ) |
| 113 | + |
| 114 | + /** |
| 115 | + * Convenience method to create a product of a type C and the target |
| 116 | + */ |
| 117 | + fun <C> second(): Prism<Tuple2<C, A>, Tuple2<C, B>> = Prism( |
| 118 | + { (c, a) -> getOrModify(a).bimap({ c toT it }, { c toT it }) }, |
| 119 | + { (c, b) -> c toT reverseGet(b) } |
| 120 | + ) |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Convenience method to create a sum of the target and a type C |
| 126 | + */ |
| 127 | +fun <A, B, C> Prism<A, B>.left(): Prism<Either<A, C>, Either<B, C>> = Prism( |
| 128 | + { it.fold({ a -> getOrModify(a).bimap({ it.left() }, { it.left() }) }, { c -> Either.Right(c.right()) }) }, |
| 129 | + { |
| 130 | + when (it) { |
| 131 | + is Either.Left<B, C> -> Either.Left(reverseGet(it.a)) |
| 132 | + is Either.Right<B, C> -> Either.Right(it.b) |
| 133 | + } |
| 134 | + } |
| 135 | +) |
| 136 | + |
| 137 | +/** |
| 138 | + * Convenience method to create a sum of a type C and the target |
| 139 | + */ |
| 140 | +fun <A, B, C> Prism<A, B>.right(): Prism<Either<C, A>, Either<C, B>> = Prism( |
| 141 | + { it.fold({ c -> Either.Right(c.left()) }, { a -> getOrModify(a).bimap({ it.right() }, { it.right() }) }) }, |
| 142 | + { it.map(this::reverseGet) } |
| 143 | +) |
0 commit comments