Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrea Cioccarelli cioccarellia

View GitHub Profile
View apply-vs-standard.kt
// Standard
val bob = Person("Bob", 19, "Texas")
println(bob)
bob.moveTo("Florida")
bob.incrementAge()
bob.alias = "Bravo"
println(bob)
// Using apply
val alice = Person("Alice", 19, "Italy").apply {
@cioccarellia
cioccarellia / ANSI-color-codes.h
Created Feb 4, 2021 — forked from RabaDabaDoba/ANSI-color-codes.h
The entire table of ANSI color codes working in C!
View ANSI-color-codes.h
/*
* This is free and unencumbered software released into the public domain.
*
* For more information, please refer to <https://unlicense.org>
*/
//Regular text
#define BLK "\e[0;30m"
#define RED "\e[0;31m"
#define GRN "\e[0;32m"
View scope_functions_let_also.kt
val engineCount = 2
val planeEngines = EngineFactory.generateFaultyEngines(engineCount).also {
println(it)
}
planeEngines.getOrNull(engineCount - 1)?.let {
println(it.cruiseSpeed)
}
View scope_functions_run_vs_let.kt
val mountain = Mountain().apply {
treeCount = randomBetween(2000, 3000)
houseCount = randomBetween(5, 10)
pendence = 20/100
roadKind = RoadKind.DIRT
}
val leavesToHouseRatio: Float = mountain.run {
val totalLeaves = treeCount * TREE_LEAVES_COUNT
val houseSurface = houseList.map {
View scope_functions_shadowing_do.kt
val wall = PaintedWall()
wall.run {
val color = this.wallColor
paint.let {
println(it.color)
}
}
View scope_functions_shadowing_dont.kt
val wall = PaintedWall()
wall.run {
val color = this.wallColor
paint.run {
println(color)
}
}
View scope_functions_do_3.kt
paintBrush.run {
rollAndClean()
dipInto(yellowPaintBucket)
}
View scope_functions_dont_3.kt
paintBrush.let {
it.rollAndClean()
it.dipInto(yellowPaintBucket)
}
View scope_functions_do_1.kt
paintBucket.apply {
fillWith(Color.LIME)
mix()
println(currentPaintColor)
}
View scope_functions_donts_1.kt
paintBucket.run {
fillWith(Color.LIME)
mix()
println(currentPaintColor)
this
}