Skip to content

Instantly share code, notes, and snippets.

View rsms's full-sized avatar

Rasmus rsms

View GitHub Profile
struct Timer {
dispatch_source_t source;
dispatch_time_t interval = DISPATCH_TIME_FOREVER;
dispatch_time_t delay = DISPATCH_TIME_FOREVER; // == same as interval
dispatch_time_t tolerance = DISPATCH_TIME_FOREVER; // == 10% of interval
Timer(dispatch_queue_t queue, dispatch_time_t interval, dispatch_block_t block) : source{dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)} {
dispatch_source_set_event_handler(source, block);
}
Timer(dispatch_time_t interval, dispatch_block_t block) : Timer{dispatch_get_main_queue(), interval, block} {}
@rsms
rsms / index.md
Last active August 29, 2015 14:03
The Definition of Design, in the Words of Charles Eames

The Definition of Design

Questions by Mme. L. Amic, Answers by Charles Eames.

What is your definition of “Design”, Monsieur Eames?
One could describe design as a plan for arranging elements to accomplish a particular purpose.

Is Design an expression of art?
I would rather say it’s an expression of purpose. It may, if it’s good enough, later be judged as art.

@rsms
rsms / cx_basename.hh
Created August 4, 2014 21:30
constexpr basename
constexpr const char* _cx_basename(const char* dp, const char* p) {
return *p == '\0' ? dp : *p == '/' ? basename(p+1, p+1) : basename(dp, p+1);
}
constexpr const char* cx_basename(const char* s) { return _cx_basename(s,s); }
cx_basename(__FILE__) // == "bar.cc" when __FILE__ == "/foo/bar.cc"
let pathString = "foo/bar/baz"
let path = split(pathString, {(c:Character) -> Bool in return c == "/"}, maxSplit: 90, allowEmptySlices: false)
println("path: \(path)") // [foo, bar, baz]
@rsms
rsms / !main.cc
Created August 9, 2014 18:27
rx async group break-out
#include "asyncgroup.hh"
using namespace rx;
AsyncCanceler DoThingAsync(Thing);
AsyncCanceler DoManyThingsAsync(Things things, func<void(Error)> cb) {
AsyncGroup G{cb};
for (auto& thing : things) {
// Capture `job` in closure and assign DoThingAsync canceler to the job
auto* job = G.begin();
@rsms
rsms / text.hh
Created August 11, 2014 21:14
rx::text v2
#pragma once
namespace rx {
using UChar = uint32_t;
using Text = std::basic_string<UChar>; // Unicode text
static const UChar UCharMax = UINT32_MAX;
namespace text {
using std::string;
@rsms
rsms / RShiftSlice.go
Last active August 29, 2015 14:12
shift a slice rightwards w/o allocating memory
func RShiftSlice(b []byte, n uint, padb byte) {
if n != 0 {
bz := uint(len(b))
wi := bz-1
for ; wi >= n; wi-- {
b[wi] = b[wi-n]
b[wi-n] = padb
}
zn := bz-n-1
for ; wi > zn; wi-- {
@rsms
rsms / ghetto_class_dump.mm
Created February 26, 2015 00:34
class-dump is awesome, but sometimes it crashes (i.e. when trying to dump WebKit.dylib) and sometimes you need some programmatic action. Really hacky and incomplete "class dump"
#import <objc/runtime.h>
static NSString* ObjcTypeToCode(const char* t) {
switch (*t) {
case 'c': return @"BOOL"; // A char
case 'i': return @"int"; // An int
case 's': return @"short"; // A short
case 'l': return @"int32_t"; // A long (l is treated as a 32-bit quantity on 64-bit programs)
case 'q': return @"int64_t"; // A long long
case 'C': return @"unsigned char"; // An unsigned char
animal = { type: "an animal",
toString: ^{ "I'm " + @type }
}
cat = create animal, { type: "furry" }
print "Cat: " + cat # --> "Cat: I'm furry"
@rsms
rsms / results.md
Last active August 29, 2015 14:27
JS object creation benchmark
  1. new_cons2 — o = new Foo(10, 20, 3, 40) (78ms)
  2. new_setprop — o = new Foo; o.a = 10 ... (81ms)
  3. new_cons — o = new Foo(10, ...) (88ms)
  4. objlit — o = {a:10, ...} (126ms)
  5. new_cons_props — o = new Foo({a:10, ...}) (481ms)
  6. objlit_proto2 — o = {a:10, ...}; o.__proto__ = Foo.prototype (1.4s)
  7. objlit_proto — o = {__proto__:Foo.prototype, a:10, ...} (2.0s)
  8. object_create — o = Object.create(Foo.prototype, a:{value:10}, ...) (34.1s)

From running ./test.js 10000000 on node/iojs v3.1.0