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
fun toPet(values: List<String>) = Try.sequential { | |
val name = values[0] | |
val (age) = Try { values[1].trim().toInt() } | |
val (type) = PetType.lookup(values[2].trim()) | |
Pet(name, age, type) | |
} |
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
typealias ResultSet<T> = Pair<List<Success<T>>, List<Failure<T>>> | |
fun <T> List<Try<T>>.asResultSet(): ResultSet<T> = this.partition { it is Success } as ResultSet<T> | |
fun <T, R> ResultSet<T>.map(transform: (T) -> R): ResultSet<R> = | |
this.first.map { it.map(transform) }.asResultSet().let { result -> | |
Pair(result.first, this.second as List<Failure<R>> + result.second) | |
} |
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
fun main() { | |
val resultA : Try<String> = Try { "Hello, World" } | |
val resultB : Try<String> = Try { throw Exception("Not Today!") } | |
println(resultA) | |
println(resultB) | |
} |
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
fun main() { | |
val lines = Try { | |
File("./my-pets.csv").readLines().map { it.split(',') } | |
} | |
val pets : Try<List<Pet>> = lines.flatMap { Try.traverse(it, ::toPet) } | |
when (pets) { | |
is Success -> println(pets.value) |
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
fun <T, R> Try.Companion.traverse(list: List<T>, transform: (T) -> Try<R>): Try<List<R>> = Try { | |
val newList = mutableListOf<R>() | |
for (value in list) { | |
when(val result = transform(value)) { | |
is Success -> newList.add(result.value) | |
is Failure -> throw result.error | |
} | |
} | |
newList | |
} |
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
fun toPet(values: List<String>): Try<Pet> { | |
val name = values[0] | |
val ageTry = Try { values[1].toInt() } | |
val typeTry = PetType.lookup(values[2]) | |
return ageTry.flatMap { age -> typeTry.map { type -> Pet(name, age, type) } } | |
} | |
enum class PetType(val type: String) { |
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
fun main() { | |
val lines = Try { | |
File("./my-pets.csv").readLines().map { it.split(',') } | |
} | |
val pets : Try<List<Pet>> = lines.map { it.map(::toPet) } | |
when (pets) { | |
is Success -> println(pets.value) |
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
sealed class Try<T> { | |
companion object { | |
operator fun <T> invoke(func: () -> T): Try<T> = | |
try { | |
Success(func()) | |
} catch (error: Exception) { | |
Failure(error) | |
} | |
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
batches = 10000 | |
batch_size=64 | |
losses_disc = [] | |
losses_disc_cat = [] | |
losses_ae = [] | |
losses_val = [] | |
real = np.ones((batch_size, 1)) | |
fake = np.zeros((batch_size, 1)) |