Skip to content

Instantly share code, notes, and snippets.

View jameswalton's full-sized avatar

James Walton jameswalton

View GitHub Profile
@jameswalton
jameswalton / gettysburg-address.txt
Last active May 22, 2020 19:42
Indexing a file - Functional Programming in Erlang assignment
Four score and seven years ago our fathers brought
forth on this continent, a new nation, conceived in Liberty,
and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether
that nation, or any nation so conceived and dedicated,
can long endure. We are met on a great battle-field of that war.
We have come to dedicate a portion of that field, as a final
resting place for those who here gave their lives that
that nation might live. It is altogether fitting and proper
@jameswalton
jameswalton / ex.erl
Created May 15, 2020 12:19
Week 2 "Pulling it all together"
-module(ex).
-export([area/1, perimeter/1, enclose/1, bits_tr/1, bits_dr/1]).
-export([test_perimeter/0, test_area/0, test_enclose/0, test_binary/0, test_all/0]).
% Ths shapes have the following patterns:
% {circle, {X, Y}, R}
% {rectangle, {X, Y}, H, W}
% {triangle, {X, Y}, A, B, C}
perimeter({circle, _, R}) -> 2 * math:pi() * R;
@jameswalton
jameswalton / variables_patterns_exercise.erl
Created May 7, 2020 16:00
Variables and patterns in practice
-module(variables_patterns_exercise).
-export([test/0]).
test() ->
0 = how_many_equal(34,25,36),
2 = how_many_equal(34,25,34),
2 = how_many_equal(34,34,25),
2 = how_many_equal(25,34,34),
3 = how_many_equal(34,34,34),
@jameswalton
jameswalton / first.erl
Last active May 6, 2020 09:43
Functional programming in Erlang course
-module(first).
-export([double/1, mult/2, area/3, treble/1, square/1]).
mult(X,Y) ->
X*Y.
square(X) ->
mult(X,X).
treble(X) ->
@jameswalton
jameswalton / node_temperature.js
Created April 30, 2013 13:13
Node application to serve temperature readings
var http = require('http');
var fs = require('fs');
var url = require('url');
var fileLocation = '/sys/bus/w1/devices/28-000003b74282/w1_slave';
http.createServer(function (req, res) {
var request_url = url.parse(req.url).pathname;
if (request_url == '/temperature.json') {
fs.readFile(fileLocation, 'utf8', function(err, data) {
@jameswalton
jameswalton / minimal-node.js
Created April 15, 2013 15:40
Minimal node application
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('It works!\n');
}).listen(9000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:9000/');
@jameswalton
jameswalton / index.rake
Last active December 14, 2015 23:49
Checks for database tables that have unindexed columns, and suggests how it can be remedied.
namespace :db do
desc "Output information about the indexes of a database"
task indexes: :environment do
connection = ActiveRecord::Base.connection
tables = {}
columns = []
indexed_columns = []
unindexed_columns = []
connection.tables.collect do |table|
@jameswalton
jameswalton / gist:2906986
Created June 10, 2012 19:13
Check controller spec
require 'spec_helper'
describe ChecksController do
before :each do
@user = create(:user)
@task = create(:task, user: @user)
@check = build(:check, task_id: @task.id)
sign_in @user
end
@jameswalton
jameswalton / gist:2906962
Created June 10, 2012 19:11
Check controller
class ChecksController < ApplicationController
before_filter :authenticate_user!, :load_task
respond_to :html
def create
@check = @task.checks.build(params[:check])
@check.save
respond_with @check, :location => [@task]
end