Skip to content

Instantly share code, notes, and snippets.

@bgoetzmann
bgoetzmann / gist:8060ea316611d88a3b8d25290127fdca
Last active August 9, 2017 11:27 — forked from dodyg/gist:5823184
Kotlin Programming Language Cheat Sheet Part 1

Intro

Kotlin is a new programming language for the JVM. It produces Java bytecode, supports Android and generates JavaScript. The latest version of the language is Kotlin M5.3

Kotlin project website is at kotlin.jetbrains.org.

All the codes here can be copied and run on Kotlin online editor.

Let's get started.

@bgoetzmann
bgoetzmann / AxonTutorial.groovy
Created August 30, 2018 11:00
A Groovy script for playing with Axon Framework; it takes back the code presented in "A Guide to the Axon Framework", https://www.baeldung.com/axon-cqrs-event-sourcing.
// See "A Guide to the Axon Framework", https://www.baeldung.com/axon-cqrs-event-sourcing
@Grab('org.axonframework:axon-core:3.3.2')
import org.axonframework.commandhandling.AggregateAnnotationCommandHandler
import org.axonframework.commandhandling.TargetAggregateIdentifier
import org.axonframework.commandhandling.model.AggregateIdentifier
import org.axonframework.commandhandling.CommandHandler
import org.axonframework.eventhandling.EventHandler
import org.axonframework.commandhandling.SimpleCommandBus
import org.axonframework.commandhandling.gateway.DefaultCommandGateway
def _makeAddItemCommand(Item item) {
{ Cart cart -> cart.addItem(item) }
}
def _makeRemoveItemCommand(Item item) {
{ Cart cart -> cart.removeItem(item) }
}
def actions = [
_makeAddItemCommand(new Item(name: 'Item1', price: 1)),
def fpCart = new Cart()
// Create command
def addItem1 = makeAddItemCommand(fpCart, new Item(name: 'Item1', price: 1))
// Invoke command
addItem1()
assert fpCart.items.keySet() == ['Item1'] as Set
def makeAddItemCommand(Cart cart, Item item) {
{ -> cart.addItem(item) }
}
def makeRemoveItemCommand(Cart cart, Item item) {
{ -> cart.removeItem(item) }
}
class MacroCartCommand implements CartCommand {
def commands = []
MacroCartCommand leftShift(CartCommand command) {
commands << command
this
}
void setReceiver(Cart cart) {
commands*.cart = cart
class Item {
String name
Double price
}
class Cart {
Map items = [:]
Cart addItem(Item item) {
items[item.name] = item
@RegisterRestClient
@Produces(MediaType.APPLICATION_JSON)
public interface SwapiService {
@GET
@Path("/people/{id}")
JsonObject getCharacter(@PathParam("id") String id);
@GET
@Path("/people")
JsonObject getPeople();
@Inject
@RestClient
private SwapiService swapiService;
JsonObject character = swapiService.getCharacter("1");
String name = character.getString("name");