Skip to content

Commit

Permalink
Arrow Optics #6
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard committed Apr 22, 2021
1 parent b7ecca4 commit 5287654
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allprojects {
maven("https://dl.bintray.com/kotlin/kotlin-eap/")
maven("https://kotlin.bintray.com/kotlinx/")
maven("https://dl.bintray.com/kodein-framework/Kodein-DB") // TODO: Remove when Kodein DB exits beta
maven( "https://dl.bintray.com/arrow-kt/arrow-kt/")
}

tasks.withType<KotlinCompile> {
Expand Down
10 changes: 10 additions & 0 deletions kotlin-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ plugins {

}

repositories {
mavenCentral()
maven( "https://dl.bintray.com/arrow-kt/arrow-kt/")
}

dependencies {
// Keep dependencies sorted to minimize merge conflicts on pull-requests!
implementation("com.google.dagger:dagger:_")
kapt(Google.dagger.compiler)
implementation("com.squareup.moshi:moshi:_")
kapt(Square.moshi.kotlinCodegen)
implementation(platform("io.arrow-kt:arrow-stack:_"))
implementation("io.arrow-kt:arrow-core")
implementation("io.arrow-kt:arrow-fx")
implementation( "io.arrow-kt:arrow-syntax")
implementation( "io.arrow-kt:arrow-optics")
kapt("io.arrow-kt:arrow-meta:_")
// Keep dependencies sorted to minimize merge conflicts on pull-requests!
}

Expand Down
94 changes: 94 additions & 0 deletions kotlin-codegen/src/main/kotlin/playground/ArrowOptics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@file:Suppress("PackageDirectoryMismatch")

package playground.arrow.optics

import arrow.core.ListK
import arrow.core.Tuple2
import arrow.core.k
import arrow.core.toT
import arrow.optics.Iso
import arrow.optics.Optional
import arrow.optics.dsl.every
import arrow.optics.extensions.listk.each.each
import arrow.optics.optics
import playground.shouldBe

/**
* Λrrow - Functional companion to Kotlin's Standard Library
*
* [GitHub](https://github.com/arrow-kt/arrow)
* [Official Website](https://arrow-kt.io/docs/optics/dsl/)
*/
fun main() {
println()
println("# arrow-kt/arrow-core")
println("Λrrow - Functional companion to Kotlin's Standard Library")
println()


val john = Employee("John Doe", Company("Kategory", Address("Functional city", Street(42, "lambda street"))))
val optional: Optional<Employee, String> = Employee.company.address.street.name
val JOHN = optional.modify(john, String::toUpperCase)
JOHN shouldBe Employee(
name = "John Doe",
company = Company(
name = "Kategory",
address = Address(city = "Functional city", street = Street(number = 42, name = "LAMBDA STREET"))
)
)

val jane = Employee("Jane Doe", Company("Kategory", Address("Functional city", Street(42, "lambda street"))))
val employees = Employees(listOf(john, jane).k())
val transformed = Employees.employees.every(ListK.each())
.company.address.street.name
.modify(employees, String::toUpperCase)
transformed shouldBe Employees(
listOf(
Employee(
name = "John Doe",
company = Company(
name = "Kategory",
address = Address(city = "Functional city", street = Street(number = 42, name = "LAMBDA STREET"))
)
),
Employee("Jane Doe", Company("Kategory", Address("Functional city", Street(42, "LAMBDA STREET"))))
).k()
)

val pointIsoTuple: Iso<Point2D, Tuple2<Int, Int>> = Iso(
get = { point -> point.x toT point.y },
reverseGet = { tuple -> Point2D(tuple.a, tuple.b) }
)

val point = Point2D(6, 10)
pointIsoTuple.get(point) shouldBe Tuple2(6, 10)

}

data class Point2D(val x: Int, val y: Int)


@optics
data class Employees(val employees: ListK<Employee>) {
companion object
}

@optics
data class Street(val number: Int, val name: String) {
companion object
}

@optics
data class Address(val city: String, val street: Street) {
companion object
}

@optics
data class Company(val name: String, val address: Address) {
companion object
}

@optics
data class Employee(val name: String, val company: Company?) {
companion object
}
4 changes: 4 additions & 0 deletions versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ version.com.uchuhimo..konf=1.1.2

version.google.dagger=2.34.1

version.io.arrow-kt..arrow-meta=0.11.0

version.io.arrow-kt..arrow-stack=0.11.0

version.io.github.lucapiccinelli..konad=1.2.1

version.io.github.serpro69..kotlin-faker=1.7.0
Expand Down

0 comments on commit 5287654

Please sign in to comment.