Skip to content

Instantly share code, notes, and snippets.

require 'benchmark/ips'
ARRAY = (1..10).to_a
Benchmark.ips do |x|
x.report('each') { sum = 0; ARRAY.each { |n| sum += n }; sum }
x.report('reduce') { ARRAY.reduce(0, :+) }
end
Calculating -------------------------------------
@dskecse
dskecse / decorators.py
Last active November 4, 2015 13:25
Decorators in Python
def honorific(klass):
class HonorificClass(klass):
def full_name(self):
return 'Dr. ' + super(HonorificClass, self).full_name()
return HonorificClass
@honorific
class Person(object):
def __init__(self, first, last):
self.first = first
@dskecse
dskecse / no_double_raisable.rb
Last active November 8, 2015 06:23
Disallowing double-raise
require 'English'
module NoDoubleRaisable
def error_handled!
$ERROR_INFO = nil
end
def raise(*args)
if $ERROR_INFO && args.first != $ERROR_INFO
warn "Double raise at #{caller.first}, aborting."
@dskecse
dskecse / gist:2db57dcba0ff27305daa54eb3f42a6b1
Created May 11, 2016 15:47
extend function (aka augment)
function extend(destination, source) {
for (var meth in source) {
if (source.hasOwnProperty(meth)) {
destination[meth] = source[meth];
}
}
return destination;
}
function list() {
return [].slice.call(arguments, 0);
}
var list = list(1, 2, 3); // [1, 2, 3]
@dskecse
dskecse / gist:65da8e6ea849aade7c7f39c96adf96f1
Last active May 19, 2016 16:02
stateless React component
import React from "react"
import { render } from "react-dom"
const mountNode = document.createElement("div")
mountNode.id = "container"
document.body.appendChild(mountNode)
const name = "David"
@dskecse
dskecse / sample.js
Created May 19, 2016 14:49
es.next single-argument class method
class User {
// ...
handleChange = e => {
}
}
diff --git a/src/components/Article.js b/src/components/Article.js
new file mode 100644
index 0000000..fef5d8a
--- /dev/null
+++ b/src/components/Article.js
@@ -0,0 +1,55 @@
+import React, { PropTypes, Component } from 'react'
+
+// stateful component (w/ state)
+class Article extends Component {
@dskecse
dskecse / propTypesFailure.js
Created May 21, 2016 15:45
make your tests fail on any propTypes errors
beforeAll(() => {
console.error = error => (
throw new Error(error);
);
});
@dskecse
dskecse / Enhance.js
Last active May 28, 2016 12:46 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export const Enhance = ComposedComponent => class extends Component {
constructor() {
super()
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}