Skip to content

Instantly share code, notes, and snippets.

View Serchinastico's full-sized avatar
👋

Sergio Gutiérrez Serchinastico

👋
View GitHub Profile
From cf90b931886b5414bd7db6d14eb5f7aa9a3b7329 Mon Sep 17 00:00:00 2001
From: Sergio Gutierrez <sergio@gokarumi.com>
Date: Wed, 3 Oct 2018 10:27:23 +0200
Subject: [PATCH] Fix facebook version in test
---
core/src/test/scala/com/karumi/shot/domain/ConfigSpec.scala | 2 +-
shot-consumer/build.gradle | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
@Serchinastico
Serchinastico / groupby.kt
Created September 17, 2018 16:39
groupby koan
// Return a map of the customers living in each city
fun Shop.groupCustomersByCity(): Map<City, List<Customer>> =
customers.groupBy { it.city }
@Serchinastico
Serchinastico / fold.kt
Created September 17, 2018 16:27
fold koan
// Return the set of products that were ordered by every customer
fun Shop.getSetOfProductsOrderedByEveryCustomer(): Set<Product> {
val allProducts = customers.flatMap { it.orders.flatMap { it.products } }.toSet()
return customers.fold(allProducts) { acc, customer ->
acc.intersect(customer.orders.flatMap { it.products } )
}.toSet()
}
@Serchinastico
Serchinastico / flatmap.kt
Created September 17, 2018 16:00
flatmap koan
// Return all products this customer has ordered
val Customer.orderedProducts: Set<Product> get() {
return orders.flatMap { it.products }.toSet()
}
// Return all products that were ordered by at least one customer
val Shop.allOrderedProducts: Set<Product> get() {
return customers.flatMap { it.orderedProducts }.toSet()
}
@Serchinastico
Serchinastico / filtermap.kt
Created September 17, 2018 15:49
filter;map koan
// Return the set of cities the customers are from
fun Shop.getCitiesCustomersAreFrom(): Set<City> = customers.map { it.city }.toSet()
// Return a list of the customers who live in the given city
fun Shop.getCustomersFrom(city: City): List<Customer> = customers.filter { it.city == city }
@Serchinastico
Serchinastico / rocket_league.kt
Created September 14, 2018 14:19
Rocket League exercise
sealed class Vinyl {
object Skull : Vinyl()
data class Angels(val numberOfAngels: Int) : Vinyl()
data class Flowers(val numberOfFlowers: Int) : Vinyl()
data class Guns(val label: String) : Vinyl()
object Boots : Vinyl()
}
typealias Flag = Color
@Serchinastico
Serchinastico / interfaces.kt
Created September 12, 2018 13:28
Interfaces exercise in kotlin
interface Sellable {
var priceInEuros: Int
var priceInDollars: Int
get() = (priceInEuros * 1.06).toInt()
set(value) {
priceInEuros = (value / 1.06).toInt()
}
}
interface Describable {
@Serchinastico
Serchinastico / Product.kt
Created September 12, 2018 13:27
Classes exercise in kotlin
abstract class Product(
val id: String
) {
abstract val name: String
abstract var priceInEuros: Int
var priceInDollars: Int
get() = (priceInEuros * 1.06).toInt()
set(value) {
priceInEuros = (value / 1.06).toInt()
}
@Serchinastico
Serchinastico / sum.kt
Created September 11, 2018 13:07
Nullability exercise
fun sum1(a: Int?, b: Int?): Int {
b ?: return 0
return if (a != null) {
a + b
} else {
5 + b
}
}
@Serchinastico
Serchinastico / fizzbuzz.kt
Created September 11, 2018 09:13
Tail recursive FizzBuzz in kotlin
tailrec fun fizzbuzz(vararg args: Int) {
if (args.isEmpty()) return
val arg = args[0]
when {
arg % 15 == 0 -> print("FizzBuzz")
arg % 3 == 0 -> print("Fizz")
arg % 5 == 0 -> print("Buzz")
}