esperimenti funzionali e non con Kotlin

This commit is contained in:
Fabio Scotto di Santolo
2020-02-01 10:39:43 +01:00
parent 14c18d0b4b
commit cff8f9362c
6 changed files with 89 additions and 14 deletions

13
functional-kotlin/pom.xml Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>functional-kotlin</artifactId>
<parent>
<artifactId>fpgym</artifactId>
<groupId>org.gym.fp</groupId>
<version>1.0</version>
</parent>
</project>

View File

@@ -0,0 +1,54 @@
import java.time.LocalDate
data class Person(val name: String,
val lastname: String,
val birthday: LocalDate)
typealias Function<T, R> = (T) -> R
typealias BiFunction<T, U, R> = (T, U) -> R
inline class Amount(val quantity: Int)
inline class Weight(val quantity: Int)
inline class Password(val value: String)
class UInt(val value: Int) {
init {
require(value >= 0)
}
}
fun add(a: Int, b: Int): Int = a + b
fun main() {
val (a, _) = Pair(1, 2)
println(a)
val fabio = Person("Fabio", "Scotto", LocalDate.of(1988, 2, 17))
val (name, lastname, _) = fabio
println("${name}, ${lastname}")
when(fabio.name) {
"Fabio" -> println(fabio.name)
}
each(UInt(5)) {
println("Hello")
}
}
operator fun Int.compareTo(other: UInt): Int {
return this.compareTo(other.value)
}
fun each(times: UInt, block: () -> Unit) {
var i = 0
while(i <= times) {
block()
i++
}
}