Created
February 26, 2021 09:06
-
-
Save ClausPolanka/c1efbd60617529e688dd9a923a74e60a to your computer and use it in GitHub Desktop.
IOSP
This file contains 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(args: Array<String>) { | |
errorHandled { | |
inCase(args.isEmpty(), | |
onEmpty = { | |
val peers = sortedPeers() | |
show(peers) | |
}, | |
onNonEmpty = { | |
updatePeer(args) | |
val peers = sortedPeers() | |
show(peers) | |
} | |
) | |
} | |
} | |
private fun errorHandled(fn: () -> Unit) { | |
try { | |
fn() | |
} catch (e: PeerNotFoundException) { | |
println("Sorry, couldn't find '${e.firstName}'") | |
} catch (e: MultipleEntriesFoundException) { | |
println("Multiple entries found for '${e.firstName}'. Please also provide last name.") | |
} | |
} | |
private fun inCase(argsAreEmpty: Boolean, onEmpty: () -> Unit, onNonEmpty: () -> Unit) { | |
when (argsAreEmpty) { | |
true -> onEmpty() | |
else -> onNonEmpty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment