Skip to content

Instantly share code, notes, and snippets.

View cawhitworth's full-sized avatar

Chris Whitworth cawhitworth

View GitHub Profile
@cawhitworth
cawhitworth / csc.vim
Created February 8, 2016 10:06
CSC in vimrc
function! BuildOutput(output)
let winnum = bufwinnr('BUILD')
if winnum != -1
if winnr() != winnum
exec winnum . "wincmd w"
endif
else
belowright split BUILD
setlocal buftype=nofile
resize 15
@cawhitworth
cawhitworth / bezier.html
Last active August 29, 2015 14:27
Bezier curves (1st, 2nd and 3rd order) in JavaScript on an HTML canvas
<html>
<body>
<canvas id="screen" style="border:1px solid #000" width=640 height=480 />
</body>
<script type="text/javascript">
// Demonstration of bezier curves by recursive definition
// For the purposes of this, a point is an object with x and y members
var c = document.getElementById("screen");
@cawhitworth
cawhitworth / ConstructorLoop.cs
Last active August 29, 2015 14:23
Fun with constructors
namespace ConstructorFun
{
class ConstructorLoop
{
public ConstructorLoop(int a, int b) : this(a, b, 20)
{ }
public ConstructorLoop(int a, int b, int c) : this(a,b)
{ }
}
@cawhitworth
cawhitworth / countdown.py
Last active November 1, 2023 15:57
Countdown solver in Python
import random
add = lambda a,b: a+b
sub = lambda a,b: a-b
mul = lambda a,b: a*b
div = lambda a,b: a/b if a % b == 0 else 0/0
operations = [ (add, '+'),
(sub, '-'),
(mul, '*'),
@cawhitworth
cawhitworth / Countdown.cs
Last active May 30, 2018 01:38
Countdown solver in C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace Countdown
{
internal interface IExpression { };
class Op : IExpression
{
var counter = function(start) {
var c = start || 0;
return function(amount) {
c = c + (amount || 1);
return c;
}
}(42);
console.log(counter()); // 43
console.log(counter(5)); // 48
var counter = function() {
var c = 0;
return function() {
c = c + 1;
return c;
}
}();
console.log(counter()); // 1
console.log(counter()); // 2
@cawhitworth
cawhitworth / composition.py
Created May 15, 2014 14:50
Composition and inheritance in Python (Smalltalk style)
class Base:
def mul(self, a, b):
return a * b
def add(self,a,b):
return a + b
class Derived(Base):
def sub(self,a,b):
return a - b
@cawhitworth
cawhitworth / eddington.hs
Last active August 29, 2015 14:01
Calculate your Eddington number
-- Given this in Lisp:
-- (defn eddington [a] (count (filter true? (keep-indexed < (reverse (sort a))))))
-- do it in Haskell (thanks to @zimpenfish for the original)
import Data.List
-- keep-indexed f l applies f over l and keeps the result of anything that is
-- not nil. f takes two arguments, index of list item and list item
-- We can do this in Haskell by zipping the original list with the indices