This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1) Domain entities: Bank and Account */ | |
typealias Amount = BigDecimal | |
| |
data class Bank(val accounts: MutableMap<String, Account> = HashMap()) | |
| |
data class Account(val id: String, val name: String) { | |
var balance: Amount by TxDelegate(initialValue = Amount.ZERO) { | |
// triggers rollback on validation failure | |
it >= Amount.ZERO | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Command { fun applyTo(system: Any) } | |
interface Query { fun extractFrom(system: Any): Any?} | |
interface EventSourcing { | |
fun append(event: Any) | |
fun <E> replay(eventConsumer: (E) -> Unit) | |
} | |
interface TxManager { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val bank1 = Bank() | |
val memimg1 = MemImg(bank1, eventSourcing) | |
| |
memimg1.execute(CreateAccount("janet", "Janet Doe")) | |
assertEquals(Amount.ZERO, bank1.accounts["janet"]!!.balance) | |
| |
memimg1.execute(Deposit("janet", Amount(100))) | |
assertEquals(Amount(100), bank1.accounts["janet"]!!.balance) | |
| |
memimg1.execute(Withdrawal("janet", Amount(10))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Skeletal Kotlin TL;DR for the truly impatient: | |
class MemoryImageProcessor(private val system: Any, | |
private val eventSourcing: EventSourcing) { | |
init { | |
synchronized(this) { | |
eventSourcing.replay<Command> { command -> command.applyTo(system) } | |
} | |
} | |
fun execute(command: Command): Unit = synchronized(this) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.util.regex.*; | |
import java.util.stream.*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val reader = File(filename).reader() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
// ... shuffling stuff... | |
// Ensure shuffling took place! | |
} while (range.all { | |
result[it] == text[it] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java | |
do { | |
// ... shuffling stuff... | |
// Ensure shuffling took place! | |
} while(IntStream.range(start, end) | |
.allMatch(i -> | |
result[i] == text.charAt(i) | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shuffle inner letter array | |
for (i in range) { | |
// Choose a random index in region | |
val rndIdx: Int = range.random() | |
// Swap current/random chars | |
result[rndIdx] = result[i].also { | |
result[i] = result[rndIdx] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shuffle inner letter array | |
for (var i = start; i < end; i++) { | |
// Choose a random index in region | |
final var rndIdx = | |
start + random.nextInt(length); | |
// Swap current/random chars | |
final var save = result[rndIdx]; | |
result[rndIdx] = result[i]; | |
result[i] = save; | |
} |
NewerOlder