Skip to content

Instantly share code, notes, and snippets.

View br3nt's full-sized avatar

br3nt

View GitHub Profile
@br3nt
br3nt / destructuring.js
Last active December 1, 2017 01:31
JS destructuring assignment syntax
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
// see: https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/
var a, b, c, x, y, z, key, foo, bar, baz;
// destructuring arrays (and default values)
[a, b, c=5] = [10, 20];
a // 10
b // 20
c // 5
@br3nt
br3nt / getting_data_from_openbank_api.js
Last active August 12, 2017 08:53
Open Bank Project API data
let base_url = 'https://apc.openbankproject.com';
let direct_login_endpoint = 'https://apc.openbankproject.com/my/logins/direct';
let api_base_url = 'https://apc.openbankproject.com/obp/v3.0.0';
let username = 'br3nt';
let password = 'password123456789';
let consumer_key = '4gvswztl1pbqyijiv4dffnhnvpyneiye43ekcno0';
let consumer_secret = 'ji5tnnxfywllhr24fgcdj3j5ycy4guywatflgtps';
var token = null;
// get token
@br3nt
br3nt / graphs.rb
Last active July 13, 2019 08:55
A simple graph implementation
class Graph
def initialize
@vertices = Set.new
@edges = []
end
def add_edge(origin, destination)
@vertices << origin << destination
@edges << [origin, destination]
end
@br3nt
br3nt / sequences_maps_filters.js
Last active June 7, 2017 06:38
Sequences, Maps and Filters in JavaScript
// see: https://ariya.io/2013/08/searching-with-array-prototype-some
// map function on an array (clasic syntax)
[1, 2, 3].map(function (x) { return x * x});
// map function on an array (new syntax)
[1, 2, 3].map((x) => x * x);
// ensure array elements are populated (with undefined)
Array.apply(0, Array(3)) // [undefined, undefined, undefined]
@br3nt
br3nt / exceptions_best_practice.md
Created February 28, 2017 22:29
Exceptions - Best Practice

This is copied from [this SO answer][1].

There is a difference between error codes and error return values. An error code is for the user and help desk. An error return value is a coding technique to indicate that your code has encountered an error.

One can implement error codes using error return values, but I would advice against that. Exceptions are the modern way to report errors, and there is no reason why they should not carry an error code within them.

This is how I would organize it (Note that points 2-6 are language agnostic):

  1. Use a custom exception type with an additional ErrorCode property. The catch in the main loop will report this field in the usual way (log file / error pop-up / error reply). Use the same exception type in all of your code.
  2. Do not start at 1 and don't use leading zeros. Keep all error codes to the same length, so a wrong error code is easy to spot. Starting at 1000 usually is good enough. Maybe add a leading 'E' to make them clearly identifiable for users (especia
@br3nt
br3nt / description.markdown
Created February 27, 2017 23:54 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@br3nt
br3nt / letterhead_example.xsl
Last active November 13, 2018 00:23
Letterhead XSL:FO example
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:hsqueue="xalan://com.helpers.xsl.MessageQueue"
xmlns:hshtml="xalan://com.helpers.xsl.HTML2FO">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="correspondence" select="document(concat('/correspondence/', /request/id, '.xml'))"/>
<xsl:variable name="letter" select="document(concat('/patients/', /request/patient_id, '/letters/', /request/letter_id, '.xml'))"/>
@br3nt
br3nt / SublimeShortcuts.md
Last active January 5, 2017 23:40
A handy list of Sublime Text 2 shortcuts that I want to use
Custom Default Description Plugin
ctrl+shift+t ctrl+shift+t Open previously closed tab Sublime
ctrl+/ ctrl+/ Toggle code commenting on current line or selection Sublime
ctrl+shift+v ctrl+shift+v Paste and indent contents of clipboard Sublime
ctrl+shift+d ctrl+shift+d Duplicate current line onto next line Sublime
ctrl+d ctrl+d Select current word or expand selection to include next occurence of word (miltiple word editing) Sublime
alt+F3 alt+F3 Select all occurences of current word or selection (multiple word editing) Sublime
ctrl+ku ctrl+ku Selected text to uppercase Sublime
@br3nt
br3nt / examples.rb
Created May 6, 2016 05:06
Augmented Interval Tree
# Example of an Augmented Tree
#
# I have added an additional `lower_limit` method, which enables testing of overlapping children.
#
# See:
# - https://en.wikipedia.org/wiki/Interval_tree#Augmented_tree
# - http://www.davismol.net/2016/02/07/data-structures-augmented-interval-tree-to-search-for-interval-overlapping/
# - http://www.bowdoin.edu/~ltoma/teaching/cs231/fall08/Lectures/10-augmentedTrees/augtrees.pdf
#