Skip to content

Instantly share code, notes, and snippets.

View tobyweston's full-sized avatar

Toby tobyweston

View GitHub Profile
View irrefutable pattern.md

inspired by a question my colleague asked me.

Given a list of option of Ints:

val list = List(Some(1),None,Some(2))

desugar the for comprehension give us: nothing special here

@ desugar{ for { x <- list } yield x } 
res5: Desugared = ammonite.$sess.cmd4.list.map[Option[Int], Any](((x: Option[Int]) => x))(scala.collection.immutable.List.canBuildFrom[Option[Int]])
@tobyweston
tobyweston / gist:7784272
Created December 4, 2013 08:42
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
@bih
bih / apple-university-stores.json
Last active November 19, 2023 22:36
List of Apple's UK university education stores w/ web addresses
View apple-university-stores.json
[{"region":"East Midlands","university":"De Montfort University","url":"http://store.apple.com/uk_edu_5000657"},{"region":"East Midlands","university":"Derby University","url":"http://store.apple.com/uk_edu_5000659"},{"region":"East Midlands","university":"Jisc Collections and Janet Limited","url":"http://store.apple.com/uk_edu_5004321"},{"region":"East Midlands","university":"Leicester University","url":"http://store.apple.com/uk_edu_5000710"},{"region":"East Midlands","university":"Lincoln University","url":"http://store.apple.com/uk_edu_5000712"},{"region":"East Midlands","university":"Loughborough College","url":"http://store.apple.com/uk_edu_5000723"},{"region":"East Midlands","university":"Loughborough University","url":"http://store.apple.com/uk_edu_5002639"},{"region":"East Midlands","university":"Nottingham Trent University","url":"http://store.apple.com/uk_edu_5000747"},{"region":"East Midlands","university":"Nottingham University","url":"http://store.apple.com/uk_edu_5000748"},{"region":"East Midla
@ms-tg
ms-tg / jdk8_optional_monad_laws.java
Created November 11, 2013 21:14
Does JDK8's Optional class satisfy the Monad laws? Yes, it does.
View jdk8_optional_monad_laws.java
/**
* ```
* Does JDK8's Optional class satisfy the Monad laws?
* =================================================
* 1. Left identity: true
* 2. Right identity: true
* 3. Associativity: true
*
* Yes, it does.
* ```
@PaulSandoz
PaulSandoz / Life.java
Last active December 12, 2015 05:28
This is an example of Conway's Game of Life using the OpenJDK lambda Stream API. This code works with build from the tip of the lambda repository as of the 1st February 2013. Since the API is a moving this code may not compile with future builds.
View Life.java
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
@tobyweston
tobyweston / gist:2841959
Created May 31, 2012 08:38
Avoid finalizer problems with JMocks SingleThreadedPolicy
View gist:2841959
public static class SingleThreadedPolicyAvoidingFinaliseProblems extends SingleThreadedPolicy {
@Override
public Invokable synchroniseAccessTo(final Invokable mockObject) {
final Invokable synchronizedMockObject = super.synchroniseAccessTo(mockObject);
return new Invokable() {
@Override
public Object invoke(Invocation invocation) throws Throwable {
if (Thread.currentThread().getName().equalsIgnoreCase("Finalizer"))
return mockObject.invoke(invocation);
return synchronizedMockObject.invoke(invocation);