Skip to content

Instantly share code, notes, and snippets.

View gahfy's full-sized avatar

Gahfy gahfy

View GitHub Profile
@gahfy
gahfy / Form.kt
Last active December 11, 2020 08:31
class CheckBox(){
var isChecked: Boolean = false
fun click(){
isChecked = !isChecked
}
}
class TextView(private var text: String){
fun click(){
fun main(){
val mementoManager = LongMementoManager()
println("Start")
println("Fibonacci at rank 1000 is: ${fibonacci(1000, mementoManager)}")
}
fun fibonacci(rank: Int, mementoManager: LongMementoManager): Long{
val result = when(rank){
0 -> 0L
1 -> 1L
else -> {
val rankMinus1 = fibonacci(rank-1, mementoManager)
val rankMinus2 = mementoManager.getMemento(rank-2)?.restore()?:0L
rankMinus1 + rankMinus2
}
}
class LongMementoManager{
private val mementos = hashMapOf<Int, LongMemento>()
fun saveMemento(index: Int, memento: LongMemento){
mementos[index] = memento
}
fun getMemento(index: Int): LongMemento?{
return mementos[index]
}
@gahfy
gahfy / Fibonacci.kt
Last active December 10, 2020 04:58
class LongMemento{
private var numberToSave: Long = 0L
fun save(number: Long): LongMemento{
numberToSave = number
return this
}
fun restore(): Long = numberToSave
}
fun fibonacci(rank: Int): Long{
return when(rank){
0 -> 0L
1 -> 1L
else -> fibonacci(rank-1).toLong() + fibonacci(rank-2).toLong()
}
}
fun main(){
println("Start")
class Calculator{
private var result = 0
private var currentOperation = "0"
fun addNumber(number: Int){
result += number
currentOperation = "$currentOperation + $number"
}
fun getResult() = result
fun main(){
val thai = RadioButton("Are you thai?")
thai.radioButtonMediator = RadioButtonMediator { checked ->
if(checked){
println("ผมพูดภาษาไทยได้ครับ")
}
else{
println("I can speak english.")
}
}
class RadioButton(private val label: String){
var isChecked: Boolean = false
var radioButtonMediator: RadioButtonMediator? = null
fun click(){
isChecked = !isChecked
radioButtonMediator?.notify(isChecked)
}
}
fun interface RadioButtonMediator{
fun notify(checked: Boolean)
}