Skip to content

Instantly share code, notes, and snippets.

@hgbrown
hgbrown / danger-will-robinson.kts
Created October 27, 2018 04:21
Demonstrates the dangers of mutability in Kotlin. Why you should prefer val to var
#!/usr/bin/env kscript
var factor = 2
fun doubleIt(n: Int) = n * factor
var message = "The factor is $factor"
factor = 0
println(doubleIt(2))
println(message)
@hgbrown
hgbrown / LimitingScopeOfVariables.kt
Created August 27, 2019 11:20
Demonstrates the importance of limiting the scope of variables to the smallest possible scope.
package dev.hbrown
/**
* Demonstrates the importance of limiting the scope of a variable using the Sieve of Eratosthenes to find prime numbers
* using a sequence builder. The algorithm is conceptually very easy:
*
* 1. take a list of numbers starting at 2.
* 2. remove the first number, it is prime.
* 3. from the rest of the numbers, remove the first number as well as all the numbers divisible by this prime number.
*