Skip to content

Instantly share code, notes, and snippets.

@lsiu
lsiu / changes_with_least_amount_of_coins.scala
Created November 9, 2012 06:50
Give change with the least amount of coins
object CoinMachine {
def changeWithBigCoin(accum: Change, changeOwing: Double, avaliableChange: Change): (Change, Double, Change) = {
if (changeOwing < 0) throw new Error("Cannot give change with existing coins")
else if (changeOwing == 0) return (accum, 0, avaliableChange)
else if (changeOwing >= 5 && avaliableChange.fiveDollarsLeft > 0) changeWithBigCoin(accum.changeFiveDollar(1), changeOwing - 5, avaliableChange.changeFiveDollar(-1))
else if (changeOwing >= 2 && avaliableChange.twoDollarsLeft > 0) changeWithBigCoin(accum.changeTwoDollar(1), changeOwing - 2, avaliableChange.changeTwoDollar(-1))
else if (changeOwing >= 1 && avaliableChange.dollarsLeft > 0) changeWithBigCoin(accum.changeDollar(1), changeOwing - 1, avaliableChange.changeDollar(-1))
else if (changeOwing >= 0.5 && avaliableChange.fiftycentsLeft > 0) changeWithBigCoin(accum.changeFiftyCent(1), changeOwing - 0.5, avaliableChange.changeFiftyCent(-1))
else throw new Error("Cannot give change on remainder: " + changeOwi
@lsiu
lsiu / ManualBootstrapMailSenderAutoConfiguration.groovy
Created November 7, 2015 00:55
Example of manually bootstraping SpringBoot AutoConfiguration with conditions depending on existance of spring boot properties
@Test
void "can send email without errors"() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()
StandardEnvironment environment = new StandardEnvironment()
InputStream ins = Resources.getResourceAsStream("/application.properties")
Properties props = new Properties()
props.load(ins as InputStream);
Closeables.closeQuietly(ins);
environment.propertySources.addFirst(new PropertiesPropertySource("application", props))
@lsiu
lsiu / WebApp.java
Created April 4, 2019 14:35
HttpServer from JDK since 1.6
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class WebApp {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(9394), 0);
server.createContext("/ping").setHandler(h -> {
final String response = "pong!";