Skip to content

Instantly share code, notes, and snippets.

View xrrocha's full-sized avatar

Ricardo Rocha xrrocha

View GitHub Profile
@xrrocha
xrrocha / README-20
Created December 16, 2020 17:26
code block for README-20
// File: Echo.kt
package example
fun main(args: Array<String>) {
println(args.joinToString(" "))
}
@xrrocha
xrrocha / README-21
Created December 16, 2020 17:26
code block for README-21
$ kotlin example.EchoKt Testing 1 2 3...
Testing 1 2 3...
@xrrocha
xrrocha / README-22
Created December 16, 2020 17:26
code block for README-22
val words = arrayOf(
"out", "of", "mind",
"back", "in", "five")
println(words[2]) // prints "mind"
@xrrocha
xrrocha / README-23
Created December 16, 2020 17:26
code block for README-23
fun main() = println("Greetings Earth!")
@xrrocha
xrrocha / README-24
Created December 16, 2020 17:26
code block for README-24
// Scramble from files/stdin to stdout
fun main(args: Array<String>) {
// Collect readers from args/stdin
val readers =
if (args.isNotEmpty())
args.map { File(it).reader() }
else
// in: quoted because reserved
@xrrocha
xrrocha / README-25
Created December 16, 2020 17:26
code block for README-25
wscrambler.scrambleWords("Hey there!")
@xrrocha
xrrocha / README-26
Created December 16, 2020 17:26
code block for README-26
package wscrambler
object WordScrambler {
// Regex compiled only once:
// at object initialization.
// Inaccessible to others,
// even in the same package.
private val WORD_REGEX =
"""\p{IsLatin}(\p{IsLatin})\1*(?!\1)\p{IsLatin}\p{IsLatin}+"""
@xrrocha
xrrocha / README-27
Created December 16, 2020 17:26
code block for README-27
// Nullable constructor arg w/default
class Numberer(pattern: String? = null) {
companion object {
// Public constant
const val DEF_PATTERN = "000,000"
}
// Private field w/fallback value (?:)
@xrrocha
xrrocha / README-28
Created December 16, 2020 17:26
code block for README-28
package wscrambler
object WordScrambler {
private val WORD_REGEX =
"""\p{IsLatin}(\p{IsLatin})\1*(?!\1)\p{IsLatin}\p{IsLatin}+""".toRegex()
@JvmStatic // JVM static method
// CLI FQN: wscrambler.Scrambler
fun main(args: Array<String>) {
do {
// ... shuffling stuff...
// Ensure shuffling took place!
} while (range.all {
result[it] == text[it] })