June 2018
Intermediate to advanced
310 pages
6h 32m
English
What's the problem wehave with our snails? They're actually quite fat, heavyweight snails.
We would like to put them on a diet. Each snail stores eight images within its snaily body. But actually, those images are the same for each snail.
What if we extract those sprites:
val sprites = List(8) { i -> java.io.File(when { i == 0 -> "snail-left.jpg" i == 1 -> "snail-right.jpg" i in 2..4 -> "snail-move-left-${i-1}.jpg" else -> "snail-move-right${(4-i)}.jpg" })}
Then we pass this list each time to the getCurrentSprite() function:
fun TansanianSnail.getCurrentSprite(sprites: List<java.io.File>) : java.io.File { ... }
That way, we'll only consume 256 KB of memory, no matter how many snails we generate. We could generate millions of ...
Read now
Unlock full access