Skip to content

Instantly share code, notes, and snippets.

View eiriklv's full-sized avatar

Eirik L. Vullum eiriklv

View GitHub Profile

@kangax's ES6 quiz, explained

@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).

Here we go with the explanations:

Question 1:
(function(x, f = () => x) {
@eiriklv
eiriklv / avoiding-exceptions.js
Last active February 2, 2019 12:13
Exception free JavaScript?
/**
* WHY? - BECAUSE EXCEPTIONS/TRY/CATCH IS A GLOBAL HORRIBLE MESS :-(
* Check out error handling in golang: https://blog.golang.org/error-handling-and-go
*/
/**
* Wrap an "unsafe" promise
*/
function safePromise(promise) {
return promise
@eiriklv
eiriklv / firebase_workers.js
Created January 2, 2017 11:02 — forked from anantn/firebase_workers.js
Firebase: Implementing a worker queue pattern using firebase_queue_pop.js
var Firebase = require("./firebase-node.js");
function Queue(ref) {
this._ref = ref;
}
Queue.prototype.pop = function(cb) {
this._ref.startAt().limit(1).once("child_added", this._pop.bind(this, cb));
}
@eiriklv
eiriklv / renameToHash.sh
Created November 27, 2016 09:19 — forked from SimplGy/renameToHash.sh
Rename files with a hash based on their contents. eg: `abc.jpg` to `3101ace8db9f.jpg`. Useful for detecting duplicates.
#!/bin/bash
# TODO: skip tiny files (so small they couldn't be photos)
# TODO: make sure sym links and other file system oddities are handled
#
# Constants
#
CHAR_COUNT=12
BLOCK_COUNT=6
SKIP_SIZE=3 # Every new block is sampled by skipping this amount of blocks to the next position
@eiriklv
eiriklv / compose.js
Created October 24, 2016 21:46
Compose
function composeLeft(...funcs) {
return function(input) {
return funcs.reduce((result, func) => {
return func(result);
}, input);
}
}
function composeRight(...funcs) {
return function(input) {
@eiriklv
eiriklv / main.js
Last active October 25, 2016 21:41
Handling effects declaratively with generators and custom runtimes (fun and learning inspired by redux-saga - but for broader use)
'use strict';
const { createStore, applyMiddleware } = require('./redux');
const { addActionTakersToStore, addLoggingToStore } = require('./middleware');
const { EventEmitter } = require('events');
const {
isPromise,
isFunction,
isObject,
@eiriklv
eiriklv / effects-and-descriptions.js
Last active October 21, 2016 14:25
Wrapping effects to make non-deterministic/non-pure function testable and predictable (for fun and learning)
'use strict';
/**
* Dependencies
*/
const expect = require('expect');
/**
* Factory for creating an effects handling "runtime"
* that stores a retrievable effects log
@eiriklv
eiriklv / index.html
Created October 15, 2016 21:02 — forked from anonymous/index.html
JS Bin // source https://jsbin.com/lolavi
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="root"></div>
<script id="jsbin-javascript">
@eiriklv
eiriklv / corrective-saga.js
Last active October 20, 2016 20:22
Corrective saga
// function that creates an async operation that can either fail or succeed based on the argument (bool)
const createAsyncOperation = (opName) => (succeeds) => !succeeds ? Promise.resolve(opName + ' success') : Promise.reject(new Error(opName + ' failure'));
// corrective operations are operations that
// sematically undo the original operation
//
// i.e - you cannot unsend an email or delete the booking at an hotel,
// but you can send an apology email and cancel a reservation
//
// to enable (unlimited) of both operations and corrective operations