Skip to content

Instantly share code, notes, and snippets.

@MrAlexLau
MrAlexLau / pattern_matching.md
Last active January 8, 2018 16:33
Elixir for Rubyists: Pattern Matching

Pattern Matching Tuples

In Ruby, although it's not a particularly common pattern, you can assign multiple variables on the left hand side of an assignment:

irb> a, b, c = [:hello, "world", 42]
irb> a
:hello
irb> b
"world"
irb> c
42
@MrAlexLau
MrAlexLau / list.ex
Last active December 18, 2017 17:02
adding to elixir lists
# See https://hexdocs.pm/elixir/List.html for reference
iex> list = [1, 2, 3]
iex> [list | 4] # fast, but probably not what you want
[[1, 2, 3] | 4]
iex> [0 | list] # fast
[0, 1, 2, 3]
@MrAlexLau
MrAlexLau / object assign
Last active March 2, 2017 18:59
object.assign vs the spread operator
const a = { foo: 'foo'};
const b = { bar: 'bar'};
// the wrong way
const result = Object.assign(a, b);
console.log(a)
// { foo: 'foo', bar: 'bar' }
// this is usually bad, we usually don't want to mutate `a` in most cases
// The right ways:
@MrAlexLau
MrAlexLau / proc-block-lambda.rb
Created March 6, 2016 23:20
Exploring Procs, blocks, and lambda expressions
# these notes walk 2 excellent blogs posts:
# 1. http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
# 2. http://mixandgo.com/blog/mastering-ruby-blocks-in-less-than-5-minutes
arr = [1, 2, 3]
########################################################
# example of a block
puts "block syntax demo"
arr.each do |element|
@MrAlexLau
MrAlexLau / d3.min.js
Last active August 29, 2015 14:23
hover lock
!function(){function n(n){return n&&(n.ownerDocument||n.document||n).documentElement}function t(n){return n&&(n.ownerDocument&&n.ownerDocument.defaultView||n.document&&n||n.defaultView)}function e(n,t){return t>n?-1:n>t?1:n>=t?0:0/0}function r(n){return null===n?0/0:+n}function u(n){return!isNaN(n)}function i(n){return{left:function(t,e,r,u){for(arguments.length<3&&(r=0),arguments.length<4&&(u=t.length);u>r;){var i=r+u>>>1;n(t[i],e)<0?r=i+1:u=i}return r},right:function(t,e,r,u){for(arguments.length<3&&(r=0),arguments.length<4&&(u=t.length);u>r;){var i=r+u>>>1;n(t[i],e)>0?u=i:r=i+1}return r}}}function o(n){return n.length}function a(n){for(var t=1;n*t%1;)t*=10;return t}function c(n,t){for(var e in t)Object.defineProperty(n.prototype,e,{value:t[e],enumerable:!1})}function l(){this._=Object.create(null)}function s(n){return(n+="")===pa||n[0]===va?va+n:n}function f(n){return(n+="")[0]===va?n.slice(1):n}function h(n){return s(n)in this._}function g(n){return(n=s(n))in this._&&delete this._[n]}function p(){var n=[]
@MrAlexLau
MrAlexLau / d3.min.js
Created June 9, 2015 21:25
d3 boilerplate
!function(){function n(n){return n&&(n.ownerDocument||n.document||n).documentElement}function t(n){return n&&(n.ownerDocument&&n.ownerDocument.defaultView||n.document&&n||n.defaultView)}function e(n,t){return t>n?-1:n>t?1:n>=t?0:0/0}function r(n){return null===n?0/0:+n}function u(n){return!isNaN(n)}function i(n){return{left:function(t,e,r,u){for(arguments.length<3&&(r=0),arguments.length<4&&(u=t.length);u>r;){var i=r+u>>>1;n(t[i],e)<0?r=i+1:u=i}return r},right:function(t,e,r,u){for(arguments.length<3&&(r=0),arguments.length<4&&(u=t.length);u>r;){var i=r+u>>>1;n(t[i],e)>0?u=i:r=i+1}return r}}}function o(n){return n.length}function a(n){for(var t=1;n*t%1;)t*=10;return t}function c(n,t){for(var e in t)Object.defineProperty(n.prototype,e,{value:t[e],enumerable:!1})}function l(){this._=Object.create(null)}function s(n){return(n+="")===pa||n[0]===va?va+n:n}function f(n){return(n+="")[0]===va?n.slice(1):n}function h(n){return s(n)in this._}function g(n){return(n=s(n))in this._&&delete this._[n]}function p(){var n=[]
@MrAlexLau
MrAlexLau / data.json
Last active August 29, 2015 14:21
arc demo
{
"nodes":[
{
"name": "Julie",
"gender": "female"
},
{
"name": "Matt",
"gender": "male"
},
@MrAlexLau
MrAlexLau / robots.rb
Created August 10, 2013 17:37
A simple solution to the "robots vs lasers" puzzle
#a simple solution to the "robots vs lasers" puzzle
#see http://www.puzzlenode.com/puzzles/4-robots-vs-lasers
#usage:
#simply run this program with ruby and pass the input file name as the sole argument
#example: ruby robots.rb sample-input.txt
#########################################################
#just use a structure since we only need to hold data
@MrAlexLau
MrAlexLau / airbrake.rb
Created June 27, 2013 13:25
How to ignore Sidekiq exceptions in Airbrake for a rails project.
#config/initializers/airbrake.rb
Airbrake.configure do |config|
config.api_key = 'mySecretKeyGoesHere'
config.ignore << "CustomSidekiqException" #do not report our custom exceptions to Airbrake
end