Skip to content

Instantly share code, notes, and snippets.

View tobyweston's full-sized avatar

Toby tobyweston

View GitHub Profile
@tobyweston
tobyweston / Text.scala
Created April 15, 2018 19:54
Rainbow text
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 / gist:7784272
Created December 4, 2013 08:42
OAuth v1 example hashing
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 August 28, 2013 07:22
Standard Git config and aliases
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 August 9, 2013 09:41
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.
@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 December 19, 2015 22:29
Currying in Java (and partial application of function example)
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 July 17, 2013 09:55
Tail recursion in Sacala
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 December 19, 2015 09:19
Git rebase and IntelliJ

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"]
@tobyweston
tobyweston / gist:3733592
Created September 16, 2012 18:17
Example of sales rate and manual tax amount (WTF)
<expenses type="array">
<expense>
<nominal-code>285</nominal-code>
<expense-type>Accommodation and Meals</expense-type>
<dated-on type="date">2011-12-05T00:00:00Z</dated-on>
<gross-value type="decimal">-8.85</gross-value>
<sales-tax-rate type="decimal">20.0</sales-tax-rate>
<description>Subsistence</description>
<manual-sales-tax-amount type="decimal">0.09</manual-sales-tax-amount>
</expense>
@tobyweston
tobyweston / gist:3733479
Created September 16, 2012 17:57
Sales tax calculations
public class TaxRate extends AbstractValueType<Double> {
public static TaxRate taxRate(Double value) {
return new TaxRate(value);
}
private TaxRate(Double value) {
super(value);
}
@tobyweston
tobyweston / gist:3362116
Created August 15, 2012 18:19
c3p0 configuration settings
class ProductionHibernateConfiguration implements HibernateConfiguration {
@Override
public Properties getProperties(String connectionUrl, String userName, String password) {
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "net.bull.javamelody.JdbcDriver");
properties.setProperty("hibernate.connection.driver", "oracle.jdbc.driver.OracleDriver");
properties.setProperty("hibernate.connection.url", connectionUrl);
properties.setProperty("hibernate.connection.username", userName);
properties.setProperty("hibernate.connection.password", password);
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");