Skip to content

Instantly share code, notes, and snippets.

View edmore's full-sized avatar

Edmore Moyo edmore

  • Cape Town, South Africa
View GitHub Profile
@edmore
edmore / github.rb
Created September 28, 2011 19:40
GitHub Commits Wrapper
class Github
require 'open-uri'
attr_accessor :user_id, :repo, :branch, :api
def initialize(user, repo, branch, api = "http://github.com/api/v2/json")
@user_id = user
@repo = repo
@branch = branch
@api = api
@edmore
edmore / tail_recursion.erl
Created September 28, 2011 19:47
Tail Recursion
-module(tail_recursion).
-export([tail_bump/1, tail_sum/1, tail_fact/1, tail_len/1]).
tail_bump(List) -> tail_bump(List,[]).
tail_bump([], Acc) -> lists:reverse(Acc);
tail_bump([H|T], Acc) -> tail_bump(T, [H+1|Acc]).
tail_sum(List) -> tail_sum(List, 0).
tail_sum([],Acc) -> Acc;
tail_sum([H|T], Acc) -> tail_sum(T, H + Acc).
@edmore
edmore / direct_recursion.erl
Created September 28, 2011 19:51
Direct Recursion
-module(direct_recursion).
-export([bump/1, average/1, sum/1, len/1, factorial/1, even/1, member/2, sum_boundary/1, sum_interval/2, reverse_create/1]).
% Add 1 to each value in a List, we could use list comprehensions of course but this is fun!!
bump([]) -> [];
bump([H|T]) -> [H + 1| bump(T)].
average([])-> 0;
average(List) -> sum(List)/len(List).
@edmore
edmore / closure.rb
Created September 28, 2011 19:53
Ruby closure
def multiplier(x)
lambda { |n| x * n }
end
# mult2 = multiplier(2) # returns Proc object
# mult2.call(3) # 6
@edmore
edmore / closure.js
Created September 28, 2011 19:55
JavaScript closure
// usage : mult2 = multiplier(2) ; mult2(3) gives you 6
// usage : mult3 = multiplier(3)
var multiplier = function(x){
return function(y){
return x * y;
}
};
@edmore
edmore / duck_typing.rb
Created September 28, 2011 20:31
Duck Typing
def make_a_sound(obj)
obj.sound!
end
def catch_make_a_sound(obj)
return obj.sound! if obj.respond_to? sound!
"#{obj.name} does not make a sound!!"
end
class Mouse
@edmore
edmore / patterns.js
Created October 21, 2011 09:16
Understanding the differences between Private, Public and Privileged
//Based on Crockford's Private Members in JavaScript post..
var myObject = {
name : "Edmore",
age : 28,
next_year : function(){ // method
return this.age += 1;
}
};
@edmore
edmore / functional.js
Created October 24, 2011 11:30
Functional Inheritance
var person = function(spec){
var that = {};
that.getName = function(){
return spec.name;
};
that.getSex = function(){
return spec.sex;
};
@edmore
edmore / block_refactoring02.rb
Created January 13, 2012 06:57
After refactoring
def last_commit_message
with_commits_list { |o| o["commits"] ? o["commits"][0]["message"] : o }
end
def last_commit_date
with_commits_list do |o|
o["commits"] ? DateTime.parse(o["commits"][0]["committed_date"]).strftime("%d %B %Y %I:%M%p") : o
end
end
@edmore
edmore / block_refactoring01.rb
Created January 13, 2012 06:58
Before rafactoring
def last_commit_message
output = parsed_json(list_commits)
output["commits"] ? output["commits"][0]["message"] : output
end
def last_commit_date
output = parsed_json(list_commits)
output["commits"] ? DateTime.parse(output["commits"][0]["committed_date"]).strftime("%d %B %Y %I:%M%p") : output
end