Skip to content

Instantly share code, notes, and snippets.

View Integralist's full-sized avatar
🎯
Making an impact

Mark McDonnell Integralist

🎯
Making an impact
View GitHub Profile
@Integralist
Integralist / 0. Clojure scratch pad.md
Last active August 29, 2015 14:00
Expanding scratch pad of Clojure code

What we cover:

  • defining functions
  • anonymous functions
  • short hand anonymous functions
  • complex reducing
  • alternatives to the reduce example
  • demonstrate how vectors and lists are different
  • custom method that separates the specified predicate from a sequence
  • destructuring with let bindings
@Integralist
Integralist / 1. TCO description.md
Last active January 20, 2020 13:32
JS Tail Call Optimisation

The problem

If a function calls itself recursively then the JavaScript engine has to create a new 'stack' (i.e. allocate a chunk of memory) to keep track of the function's arguments.

Let's look at an example of this happening:

function sum(x, y) {
    if (y > 0) {
      return sum(x + 1, y - 1);
@Integralist
Integralist / Ruby Splats.rb
Last active October 8, 2015 12:40
Ruby Splat (single and double)
# When calling a method with a splat then the parameters are passed as a comma separated list
# When receiving arguments with a splat then they are converted into an Array
# When using a double splat (2.0+) then the argument is converted into a Hash (i.e. named parameters)
def foo(*args)
p args
end
foo 'a', 'b', 'c'
# => ["a", "b", "c"]
@Integralist
Integralist / 1. Typical Example.rb
Last active January 29, 2016 08:51
Ruby: Symbol class to_proc and custom class object to_proc (see also https://gist.github.com/Integralist/8079e79c5eb4e7b88183 to see how to use `&` with `method()`)
strs = ['foo', 'bar', 'baz']
# standard long form way
caps = strs.map { |str| str.upcase }
# short hand
caps = strs.map(&:upcase)
# The & takes any object and calls to_proc on it
# In the above example we're using a Symbol and not an object
@Integralist
Integralist / Custom Ruby Error Handling.rb
Created April 8, 2014 14:42
Custom Ruby Error Handling
class MyCustomError < StandardError
attr_reader :object
def initialize(object)
@object = object
end
end
begin
raise MyCustomError.new("an object"), "a message"
@Integralist
Integralist / vim-plugins.sh
Last active August 29, 2015 13:58
For up to date list of plugins please see my Fresh Install repository: https://github.com/Integralist/Fresh-Install/
cd "$HOME/.vim/bundle"
plugins=( airblade/vim-gitgutter \
ap/vim-css-color \
bling/vim-airline \
edkolev/tmuxline.vim \
ervandew/supertab \
gcmt/wildfire.vim \
godlygeek/tabular \
kien/ctrlp.vim \
@Integralist
Integralist / Ruby Lambdas.md
Last active August 8, 2023 05:10
Ruby lambdas

Lambda: standard

# Creating a lambda
l = lambda { |name| "Hi #{name}!" }

# Executing the lambda
l.call("foo") # => Hi foo!
@Integralist
Integralist / Bad Design.rb
Last active August 29, 2015 13:57
Refactoring Ruby -> not all conditionals can be removed, and those that can can't necessarily use the standard refactoring methods such as "Replace Type Code with Module Extension", "Replace Type Code with Polymorphism" or "Replace Type Code with State/Strategy". The below examples demonstrate this.
class Foo
def initialize(a=[], b=[])
@a = a
@b = b
end
def add(item)
if item.is_a? A
@a.push item
else
@Integralist
Integralist / git reset explained.md
Last active March 14, 2022 10:25
git reset --soft/--mixed/--hard

UPDATE

Every time I accidentally git commit --amend instead of a normal git commit I have to google git reset --soft HEAD@{1} to save the day.


Imagine you have a file called foo.txt and your Git history looked like this:

A -&gt; B -&gt; C (HEAD)
@Integralist
Integralist / AOP.md
Last active August 29, 2015 13:57
AOP (Aspect-Oriented Programming)

What is AOP?

Aspect Oriented Programming is a means to change the behaviour of – or add behaviour to – methods and functions (including constructors) non-invasively. The added behaviour is called “advice” and can be added before, after, or around the function it advises.

This description is similar to the Extract Surrounding refactoring method. The difference is in the direction of the change. It seems AOP is more focused at modifying existing behaviour non-invasively; where as the Extract Surrounding Method actually changes the source code to allow this type of behavioural modification.

Libraries