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 / building-a-go-program-via-docker.sh
Last active August 28, 2015 08:28
Building a Go program via Docker
docker run -\
-rm=true \
-v $WORKSPACE/src:/gopath/src/github.com/foo/bar/src \
-v $WORKSPACE/src:/app \
-e "GOPATH=/gopath" \
-w /app golang:1.5 sh \
-c 'CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags=\"-s\" -o bar'
# Refactor:
# Set the working directory to `/gopath/src/github.com/foo/bar/src
{
"name": "WebScraping",
"main": "scrap.js",
"dependencies": {
"cheerio": "~0.13.1"
}
}
@Integralist
Integralist / bubbling errors.js
Last active August 29, 2015 13:56
Demonstrate how to handle bubbling errors by consolidating them (modified from: http://blog.ponyfoo.com/2013/07/12/teach-yourself-nodejs-in-10-steps)
function sum(a, b, done) {
// we convert this otherwise sync function into an async function
// note: this forces itself into the next event loop
// we should use setImmediate instead which places it at
// the bottom of the current event loop stack
process.nextTick(function() {
// `done` is the callback function passed into `sum`
done(null, a + b)
});
}
@Integralist
Integralist / 1. too much context.rb
Last active August 29, 2015 13:57
Ruby: reducing context
class Dependency
def initialize(foo, bar, baz)
@foo = foo
@bar = bar
@baz = baz
end
def data
{ :foo => @foo, :bar => @bar, :baz => @baz }
end
@Integralist
Integralist / AWS DynamoDB.rb
Last active August 29, 2015 13:57
Messing around with AWS and DynamoDB
require 'aws-sdk'
AWS.config(:region => 'eu-west-1')
dynamo_db = AWS::DynamoDB.new
dynamo_db.tables["mark_sequencer_test"].exists?
table = dynamo_db.tables["mark_sequencer_test"]
table.status #=> :active
@Integralist
Integralist / Ruby Queue.md
Created April 23, 2014 08:03
Ruby Data Structures: Queue

Another useful data structure is Queue. This is a synchronised, i.e. thread safe, first-in first-out queue. This makes it easy to coordinate work between threads. \

Below is a simple example of a program that lets you enter multiple URLs to download, then works through them one by one in the background:

require "thread" # Queue is part of the thread library
require "net/http"

to_do = Queue.new
@Integralist
Integralist / Ruby Set.md
Created April 23, 2014 08:04
Ruby Data Structures: Set

Set, included in the stdlib, is an un-ordered collection of elements with no duplicates. However you’ll find that Set’s most useful feature is that its #include? method is much faster than Array’s. In fact Set#include? will always take the same amount of time, regardless of how many elements it contains, whereas Array#include? takes longer the more elements in the array.

Sets do take longer to build than Arrays, so work best when they can be created just once and assigned to a constant, or instance variable of a long lived object.

require "set"

TEMP_EMAIL_PROVIDERS = Set["temp-email.tld", "throwaway-mail.tld", ...]

def temporary_email?(address)
@Integralist
Integralist / Ruby Around Alias Pattern.rb
Created May 9, 2014 07:10
Ruby's "Around Alias" pattern
class String
alias :orig_length :length
def length
"Length of string '#{self}' is: #{orig_length}"
end
end
"abc".length
#=> "Length of string 'abc' is: 3"
@Integralist
Integralist / .zshrc
Last active August 29, 2015 14:01
Pro Vim `.zshrc` configuration file
# Some of the Zsh awesomeness seen below was originally found here...
# http://zanshin.net/2013/02/02/zsh-configuration-from-the-ground-up/
# Exports {{{
export GITHUB_USER="your-username"
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin # Reorder PATH so local bin is first
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;32'
export MANPAGER="less -X" # Don’t clear the screen after quitting a manual page
export EDITOR="vim"
@Integralist
Integralist / Curry.rb
Created May 15, 2014 09:15
Why does this fail in MRI 2.0 but pass in JRuby (which is API compat with MRI 1.9.3)?
my_proc = proc { |x, y, z| x + y + z }
add_to_the_value_three = my_proc.curry(2)[1][2]
puts add_to_the_value_three[6] # => 9
=begin
test.rb:1:in `+': nil can't be coerced into Fixnum (TypeError)
from test.rb:1:in `block in <main>'
from test.rb:2:in `[]'
from test.rb:2:in `<main>'
=end