Skip to content

Instantly share code, notes, and snippets.

View renaudcerrato's full-sized avatar

Renaud Cerrato renaudcerrato

View GitHub Profile
@renaudcerrato
renaudcerrato / dyndns.ovh
Last active December 26, 2021 13:33
POSIX Shell Script to Update ovh dyndns.
#!/bin/sh
error () {
echo "error: $1"
usage
}
usage () {
echo "usage: $0 -u <username> -p <password> -d <domain> -i <seconds> <subdomain>";
exit 1
@renaudcerrato
renaudcerrato / kotlin-stats.sh
Last active August 26, 2019 08:56
Kotlin vs Java Statistics
#!/usr/bin/env bash
KOTLIN=$(find . -name "*.kt" -exec cat {} \; | wc -l)
JAVA=$(find . -name "*.java" -exec cat {} \; | wc -l)
echo Kotlin: $KOTLIN lines \($(echo scale=2\; 100 \* $KOTLIN / \( $KOTLIN + $JAVA \) | bc -l)%\)
echo Java : $JAVA lines \($(echo scale=2\; 100 \* $JAVA / \( $KOTLIN + $JAVA \) | bc -l)%\)

Keybase proof

I hereby claim:

  • I am renaudcerrato on github.
  • I am renaudcerrato (https://keybase.io/renaudcerrato) on keybase.
  • I have a public key ASABbJwczM3qvM1NIE5Nv6myqn1Ou-ypR6p5XFAcsvCf-Qo

To claim this, I am signing this object:

@renaudcerrato
renaudcerrato / snippet.kt
Last active March 12, 2019 12:20
Kotlin Return with Labels
val list = listOf("Kotlin", "", "Java", "Groovy")
fun main() {
list.forEach {
if(it.isNullOrEmpty()) return@forEach // return with implicit label
println(it)
}
println("Done!")
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 12, 2019 12:20
Kotlin Local Returns
// Kotlin's standard library extension
inline fun Array<String>.forEach(action: (String) -> Unit) {
for(str in this) {
action(str)
}
}
val list = listOf("Kotlin", "Java", "Groovy")
// a bare return statement in a lambda called from
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 9, 2019 06:52
Kotlin Inline Function
inline fun greeter(action: () -> Unit) {
try {
println("Hello!")
action()
}finally{
println("Goodbye!")
}
}
greeter {
@renaudcerrato
renaudcerrato / snippet.kt
Created February 24, 2019 08:56
Kotlin Try/Catch expression
// the returned value is the last expression of the try or catch block
val a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }
fun printNumber(reader: BufferedReader) {
val number = try {
parseInt(reader.readLine())
} catch (e: NumberFormatException) {
return
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 9, 2019 06:20
Kotlin Enum
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 15, 2019 18:50
Kotlin Infix Functions (Standard Library)
// until and step are infix functions
for(i in 0 until 100 step 2) println("$i")
// equivalent to:
for(i in 0.until(100).step(2)) println("$i")
// downTo is an infix function
for(i in 100 downTo 0) println("$i")
// shl, and, xor are infix functions
val i = (0x65acf9 shl 6) and 0x55 xor 0x80
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 23, 2019 06:22
Kotlin Infix Functions
class Price(val value: Double, val currency: Currency)
infix fun Int.euro(cents: Int): Price {
return Price(toDouble() + cents / 100.0, Currency.EURO)
}
val price = 1 euro 42
// equivalent to:
val price = 1.euro(42)