Created
July 26, 2019 20:53
-
-
Save caseykulm/6fb432430febf3231e0ff47e922bb7cd to your computer and use it in GitHub Desktop.
Arrow Optics Example - https://arrow-kt.io/docs/optics/lens/
This file contains hidden or 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
plugins { | |
id 'org.jetbrains.kotlin.jvm' version '1.3.41' | |
id "org.jetbrains.kotlin.kapt" version "1.3.41" | |
} | |
group 'com.caseykulm.learnyouanarrow' | |
version '1.0-SNAPSHOT' | |
def arrow_version = "0.10.0-SNAPSHOT" | |
repositories { | |
mavenCentral() | |
maven { url "https://dl.bintray.com/arrow-kt/arrow-kt/" } | |
maven { url 'https://oss.jfrog.org/artifactory/oss-snapshot-local/' } | |
} | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | |
implementation "io.arrow-kt:arrow-core:$arrow_version" | |
implementation "io.arrow-kt:arrow-syntax:$arrow_version" | |
implementation "io.arrow-kt:arrow-optics:$arrow_version" | |
kapt "io.arrow-kt:arrow-meta:$arrow_version" | |
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1') | |
testImplementation "org.jetbrains.kotlin:kotlin-test" | |
testImplementation "org.jetbrains.kotlin:kotlin-test-junit" | |
} | |
compileKotlin { | |
kotlinOptions.jvmTarget = "1.8" | |
} | |
compileTestKotlin { | |
kotlinOptions.jvmTarget = "1.8" | |
} |
This file contains hidden or 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
import arrow.optics.Lens | |
import arrow.optics.optics | |
@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 } | |
fun main() { | |
withCopy() | |
// withArrowOptics() | |
} | |
private fun createEmployee(): Employee { | |
return Employee( | |
name = "John Doe", | |
company = Company( | |
name = "Arrow", | |
address = Address( | |
city = "Functional city", | |
street = Street( | |
number = 23, | |
name = "lambda street" | |
) | |
) | |
) | |
) | |
} | |
private fun withCopy() { | |
var employee = createEmployee() | |
println("Employee Before: $employee") | |
employee = employee.copy( | |
company = employee.company.copy( | |
address = employee.company.address.copy( | |
street = employee.company.address.street.copy( | |
name = employee.company.address.street.name.capitalize() | |
) | |
) | |
) | |
) | |
println("Employee After: $employee") | |
} | |
private fun withArrowOptics() { | |
var employee = createEmployee() | |
println("Employee Before: $employee") | |
val employeeStreetName: Lens<Employee, String> = | |
Employee.company compose Company.address compose Address.street compose Street.name | |
employee = employeeStreetName.modify(employee, String::capitalize) | |
println("Employee After: $employee") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment