Skip to content

Instantly share code, notes, and snippets.

View gsinclair's full-sized avatar

Gavin Sinclair gsinclair

  • Sydney, Australia
View GitHub Profile
@gsinclair
gsinclair / triangle_test.rb
Created October 15, 2009 13:26
Demonstrates use of Object#tap in unit testing. Small gain in neatness.
class TestTriangle < Test::Unit::TestCase
def test_isosceles_given_base_and_height
triangle(:ABC, :isosceles, :base => 5, :height :=> 9).tap do |t|
assert_point_equal p(2.5,9), t.apex
assert_point_equal p(0,0), @register[:A]
assert_point_equal p(5,0), @register[:B]
assert_point_equal p(2.5,9), @register[:C]
assert_equal t, @register.retrieve(:triangle, :ABC)
end
@gsinclair
gsinclair / gist:479572
Created July 17, 2010 15:22
Ruby 1.9.2-rc1: instance_eval can't handle a lambda (needs a proc)
class InstanceEval
def initialize(code)
@code = code
@context = Object.new
end
def run
code = @code
result = @context.instance_eval(&code)
"* run --> #{result.inspect}"
@gsinclair
gsinclair / gist:6268353
Last active December 21, 2015 06:59
Clojure code to create a balanced draw for ~30 schools each competing sequentially in (up to) 9 events. "Balanced" in the sense that schools each receive allotted positions throughout the day; no school has all their events clustered at any particular time. Contains rough test code throughout. If run, will output some test results, and write to …
; Ways in which I want to improve this code:
; * less explicit looping; more higher-order functions
; * use records or types as appropriate to capture the structure of the data
; that flows through the functions
; * break a couple of functions into smaller ones
; * extract a general data type for a 2D table that is iterable by rows or columns
; * implement organised unit tests instead of the ad-hoc ones spread throughout
; * use namespaces and private/public functions idiomatically
(ns gs.ahigs.draw
@gsinclair
gsinclair / gist:347ac51f0713251efe21
Created June 11, 2014 08:22
Detailed error message when running Rubygems
/usr/lib/ruby/1.9.1/net/http.rb:763: [BUG] rb_sys_fail(connect(2)) - errno == 0
ruby 1.9.3p545 (2014-02-24) [i386-cygwin]
-- Control frame information -----------------------------------------------
c:0037 p:---- s:0223 b:0223 l:000222 d:000222 CFUNC :initialize
c:0036 p:---- s:0221 b:0221 l:000220 d:000220 CFUNC :open
c:0035 p:0029 s:0216 b:0216 l:0023d0 d:000215 BLOCK /usr/lib/ruby/1.9.1/net/http.rb:763
c:0034 p:0031 s:0214 b:0214 l:000213 d:000213 METHOD /usr/lib/ruby/1.9.1/timeout.rb:55
c:0033 p:0026 s:0202 b:0202 l:000201 d:000201 METHOD /usr/lib/ruby/1.9.1/timeout.rb:100
c:0032 p:0038 s:0196 b:0196 l:0023d0 d:0023d0 METHOD /usr/lib/ruby/1.9.1/net/http.rb:763
@gsinclair
gsinclair / gist:39d848df1439ba86c02355a0376fa51f
Created December 14, 2016 01:50
A problem I'm having setting a contract on a class-level method. The problem may be related to that or to something else.
I know class-method contracts have been discussed (and fixed -- #7 ) before, but I have occasional problems with contracts on class methods. Here is the latest one, for example.
#<ParamContractError: Contract violation for argument 3 of 3:
Expected: nil,
Actual: #<Proc:0x80354744@.../lib/scm_roster/report/roster.rb:19>
Value guarded in: SCMRoster::Report::Util::process_data_and_notify_change
With Contract: Module, Func => None
At: .../lib/scm_roster/report/util.rb:22 >
[I have snipped the paths for readability.]
"
Usage:
In code:
(tag> value) or (t> form arg1 arg2 ...) to tag a value to stdout.
(tagseq> 20 x) or (ts> 20 form arg1 arg2 ...) to tag a sequence, printing 20 items.
Note: the value of a (tag>) or (t>) or (tagseq>) or (ts>) form is always the complete
value, so it can be inserted into code without a problem.
At the REPL:
(ns aoc.day13
(:use aoc.common)
(:require [clojure.set :as set]
[clojure.string :as str]
[clojure.test :refer [testing is]]
[clojure.pprint :as pp]))
(defn y-x-comparator
"Compares location vectors in a way that enables sorting from top-left to bottom-right."
(ns aoc.day16
(:use aoc.common)
(:require [clojure.set :as set]
[clojure.string :as str]
[clojure.test :refer [testing is]]
[clojure.pprint :as pp]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]
[better-cond.core :as b]))
@gsinclair
gsinclair / factorisation.tex
Created February 14, 2019 06:49
A factorisation worksheet tracing a straight line from simple common-factor stuff to quadratic trinomials.
\documentclass[10pt,a4paper]{article}
% This LaTeX file is tested with xelatex. Note 'microtype'.
%
% Copyright (c) 2019 Gavin Sinclair, released under the Do What You Want With It licence.
% If you think of any improvements, let me know so I can learn more LaTeX.
\usepackage{microtype}
\usepackage{setspace}
\usepackage{parskip}
@gsinclair
gsinclair / week2.py
Created March 15, 2019 05:44
Notes and code for Week 2 of Informatics at PLC Sydney
# -----------------------------------------------------------------------------
# Informations 2019 Week 2
# -----------------------------------------------------------------------------
#
# After Week 1 we had the following functions:
# is_divisible(number, factor) --> True/False
# factors(n) --> list
# is_prime(n) --> True/False
#
# This week we calculate the HIGHEST COMMON FACTOR of two numbers.