Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / faster-unit-testing-in-rails-without-rails.rb
Last active November 20, 2017 10:17
Faster Unit Testing in Rails Without Loading Rails
# Running a single minimalistic unit test with Rails 4 and Ruby 2.1.1 is a lot faster
# if you avoid loading Rails:
# Running the test *with* Rails
time ruby -Itest test/models/my_model_test.rb # => real ~ 6s
# Running the test *without* Rails
time ruby -Itest/no_rails test/models/my_model_test.rb # => real ~ 0.6s
@peter
peter / recursive_struct.rb
Created March 26, 2014 16:09
Using a recursive struct in Ruby to honor the Uniform Access Principle when accessing data from hashes/structs/objects
# Simple wrapper to allow hashes to be accessed via dot notation recursively.
# Recurses over hashes and arrays. Works with string keys
# and symbol keys - other types of keys are not supported and
# all keys must be of the same type. Write access is only supported via
# []= Hash syntax. Supports accessing hash values with square bracket Hash syntax ([...])
# and access is indifferent to if the key is given as a string or a symbol.
# Supports JSON generation.
#
# Dependencies: Ruby.
#
@peter
peter / programming-languages.md
Created May 10, 2014 12:47
Programming Languages

Programming Languages

Lisp (1958)

Interpreted, dynamic typing, functional

C (1972)

Compiled, static typing, imperative (procedural)

@peter
peter / es6-uncensored.js
Last active August 29, 2015 14:01
ES6 Uncensored
// 1. Swith two variables
// ES5
var temp = a;
a = b;
b = temp;
// ES6 - destructuring assignment
[b, a] = [a, b];
@peter
peter / gist:75aa6cf67f370b5bae4e
Created May 11, 2015 08:46
JavaScript ramda example - imperative to functional refactoring
// BEFORE REFACTORING (IMPERATIVE)
var filteredLinks = [];
var weekdayCounts = {};
// Limit to 4 links per weekday.
// Loop through all links and keep track of
// how many are in each weekday.
widget.links.forEach(function(link) {
var weekday = link.group;
if (weekdayCounts[weekday] === undefined) {
weekdayCounts[weekday] = 0;
@peter
peter / gist:496e99b118e7688e2cab
Created May 25, 2015 18:54
Ramda mapObj example
'use strict';
var R = require('ramda');
var blank = function(value) {
return value == null || (typeof value === 'string' && value.trim().length === 0);
};
var present = function(value) {
return !blank(value);
'use strict';
var R = require('ramda');
var CATEGORIES = [
{category_type: 'cuisine', id: 1, name: 'French', type: 'categroy'},
{category_type: 'diets', id: 2, name: 'Vegetarian', type: 'category'}
];
var children = {
@peter
peter / clojure-derive-multimethods-example.clj
Last active January 15, 2024 18:45
Example of Clojure Derive/Multimethods for inheritance/polymorphism
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; BASE CLASS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; public class ContentItem {
; public String foobar() {
; return "content_item";
; }
; }
@peter
peter / synchronized_function.js
Last active September 3, 2015 16:06
Hack to prevent JavaScript function from executing concurrently (to avoid race conditions etc.)
'use strict';
// NOTE: this in memory locking approach will only work with a single process
// You might use something like Redis och memcachached for storing the lock if you need to lock
// across several processes.
var synchronizedFunction = function(lockIdFn, fn) {
var locks = {};
var synchronized = function() {
var args = Array.prototype.slice.call(arguments),
lockId = lockIdFn.apply(null, args);
@peter
peter / clojure_vs_java_8.md
Last active August 13, 2017 15:50
Readability of Clojure vs Java 8 Streams

Java:

public Optional<String> getType() {
    return this.types.isEmpty() ?
        Optional.empty() :
        Optional.of(String.join(",", (Iterable)this.types.stream().map(Enum::name).collect(Collectors.toList())));
}