Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
bkyrlach / Board.java
Created February 21, 2020 01:54
How to print the tic-tac-toe board...
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= 9; i++) {
Piece piece;
try {
piece = pieceAt(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
switch(piece) {
List<List<Piece>> board = new ArrayList<List<Piece>>();
for(int i = 0; i < 3; i++) {
List<Piece> row = new ArrayList<Piece>();
board.add(row);
for(int j = 0; j < 3; j++) {
row.add(Piece.SPACE);
}
}
char[][] board = new char[3][3];
// for(int i = 0; i < board.length; i++) {
// board[i] = new int[3];
// }
for(int i = 0; i < board.length; i++) {
char[] row = board[i];
for(int j = 0; j < row.length; j++) {
row[j] = 'a' + 3;
}
package com.kyrlach.hello;
public class BetAction {
private int _amount;
private Face _face;
public BetAction(Face face, int amount) {
_face = face;
_amount = amount;
}
package com.kyrlach.hello;
import java.util.Random;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Random r = new Random();
Scanner s = new Scanner(System.in);
Integer x = s.nextInt();
case class ComputerState(memory: Vector[Int], ptr: Int)
type IntCodeError[A] = Either[String,A]
type IntCodeProgram[A] = StateT[IntCodeError,ComputerState,A]
def pure[A](a: A): IntCodeProgram[A] = StateT.pure(a)
def inspect[A](f: ComputerState => A): IntCodeProgram[A] = StateT.inspect(f)
def mod(f: ComputerState => ComputerState): IntCodeProgram[Unit] = StateT.modify(f)
def error[A](msg: String): IntCodeProgram[A] = StateT.liftF(Left(msg))
object Program {
def main(args: Array[String]): Unit = {
val pgm1 = FrontEnd.clickMe
val pgm2 = FrontEnd.clickMe2
val init = AppState(-4)
val s1 = App.eval(pgm1, init)
println(s1)
@bkyrlach
bkyrlach / compose.js
Created September 10, 2019 17:53
Introduction to FP
function compose(f,g) {
return function (a) {
return g(f(a));
};
}
function add1(x) {
return x + 1;
}
@bkyrlach
bkyrlach / endo.hs
Created April 5, 2019 16:53
Function monoids...
{-# LANGUAGE FlexibleInstances #-}
module Intro where
class BenMonoid a where
benappend :: a -> a -> a
benempty :: a
instance BenMonoid (a -> a) where
benappend f g = f . g
object Proof1 {
def main(args: Array[String]): Unit = {
trait IsRaining
trait IsCloudy
def proveIt(p: IsRaining): IsCloudy = new IsCloudy {}
proveIt(new IsRaining{}) match {
case _: IsCloudy => println("It's cloudy!")
}