Skip to content

Instantly share code, notes, and snippets.

Avatar

Toby tobyweston

View GitHub Profile
View Text.scala
import Console._
object Text {
private val colours = List(RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA)
def bonza(string: String): String = GREEN + string + RESET
def alter(string: String): String = RED + string + RESET
@tobyweston
tobyweston / FunctionInterfaceOps.scala
Created Jul 21, 2015
Java 8 to Scala bridge methods
View FunctionInterfaceOps.scala
package bad.robot.radiate
import java.util.concurrent.Callable
import java.util.function.{Consumer, Supplier}
object FunctionInterfaceOps {
implicit def toConsumer[A](function: A => Unit): Consumer[A] = new Consumer[A]() {
override def accept(arg: A): Unit = function.apply(arg)
}
View gist:2c7075e5e3311faca5ef
def maprec[B](f: A => B): List[B] = {
def recur(head: A, tail: List[A]): List[B] = {
tail match {
case Nil => List(f.apply(head))
case _ => f.apply(head) +: recur(tail.head, tail.tail)
}
}
recur(elements.head, elements.tail)
@tobyweston
tobyweston / gist:caefc3b5ec36348387e5
Created Sep 22, 2014
Naive implementation of Either in JAva
View gist:caefc3b5ec36348387e5
package bad.robot;
import fj.F;
import java.util.function.Function;
import java.util.stream.Stream;
public interface Either<A, B> {
static <A, B> Either<A, B> left(final A a) {
@tobyweston
tobyweston / gist:7784272
Created Dec 4, 2013
OAuth v1 example hashing
View gist:7784272
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import rtx.forest.web.PercentEncoder
import com.googlecode.utterlyidle.{RequestBuilder, Request}
object OAuth {
type ApiKey = String
type SharedSecret = String
@tobyweston
tobyweston / gist:6363048
Created Aug 28, 2013
Standard Git config and aliases
View gist:6363048
git config --global http.proxy myproxy:8080
git config --global user.email me@email.com
git config --global user.name me
git config --global color.ui true
git config --global alias.last "log -1 HEAD"
git config --global alias.st "status -sb"
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset abbrev-commit --date=relative"
@tobyweston
tobyweston / gist:6192446
Created Aug 9, 2013
Work out the size of a grid required to display n tiles whilst keeping some sort of visual balance. The physical dimensions of the display region and tiles are unimportant.
View gist:6192446
@Test
public void nTilesRequireGridToDisplay() {
assertThat(tiles(1), requiresGridOf(1, 1));
assertThat(tiles(2), requiresGridOf(1, 2));
assertThat(tiles(3), requiresGridOf(2, 3));
assertThat(tiles(4), requiresGridOf(2, 4));
assertThat(tiles(5), requiresGridOf(2, 3));
assertThat(tiles(6), requiresGridOf(2, 3));
assertThat(tiles(7), requiresGridOf(3, 3));
assertThat(tiles(8), requiresGridOf(3, 3));
@tobyweston
tobyweston / CurryingExample.java
Last active Dec 19, 2015
Currying in Java (and partial application of function example)
View CurryingExample.java
import com.googlecode.totallylazy.Function1;
import java.util.Arrays;
import static java.lang.String.*;
public class CurryingExample {
static abstract class Function1<A, B> extends com.googlecode.totallylazy.Function1<A, B> {
@Override
@tobyweston
tobyweston / RecursionTest.scala
Created Jul 17, 2013
Tail recursion in Sacala
View RecursionTest.scala
import org.scalatest.FunSuite
class RecursionTest extends FunSuite {
case class Explosion(message: String) extends RuntimeException
object Bomb {
def tick(countdown: Int): Int = {
println("hello")
if (countdown == 0) throw new Explosion("BOOOOM!")
@tobyweston
tobyweston / gist:5932618
Last active Dec 19, 2015
Git rebase and IntelliJ
View gist:5932618

To configure your repository to always rebase when pulling;

git config branch.master.rebase true

which turns the relevant section of your '.git/config' from

[branch "master"]