-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add withEach and withEachIndexed Iterable extensions, prompt function (…
…#17) * add withEach and withEachIndexed Iterable extensions * add prompt function * added Prompt documentation * fixed detekt compliance
- Loading branch information
1 parent
566e300
commit 4491c9f
Showing
5 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pw.forst.katlib | ||
|
||
/** | ||
* Prints @param [promptText], reads input from console and applies @param [transform] to it. | ||
* If @param [transform] throws an exception on user input, @param [exceptionHandler] will be invoked, | ||
* and prompt will be repeated. | ||
* | ||
* Example: snippet below will ask user for input until given input can be parsed to Double | ||
* ``` | ||
* prompt( | ||
* "input number:", | ||
* { "this is not a number" }, | ||
* ) | ||
* { it.toFloat() } | ||
* ``` | ||
* @return user input with transform applied | ||
*/ | ||
fun <R> prompt(promptText: String, exceptionHandler: (e: Exception) -> String? = { null }, transform: (input: String) -> R): R { | ||
while (true) { | ||
print(promptText) | ||
try { | ||
return transform(readln()) | ||
} catch (e: Exception) { | ||
exceptionHandler(e)?.let { println(it) } | ||
} | ||
} | ||
} |
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