Skip to content

Instantly share code, notes, and snippets.

View paldepind's full-sized avatar

Simon Friis Vindum paldepind

  • Copenhagen, Denmark
  • 16:21 (UTC +02:00)
View GitHub Profile
@paldepind
paldepind / index.js
Created September 7, 2016 10:58
requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var patch = require('snabbdom').init([
require('snabbdom/modules/attributes'),
]);
var h = require('snabbdom/h');
var elem = h('div', [
@paldepind
paldepind / record.sh
Created March 17, 2016 12:32
Record a selected window to a gif file. Remove `-draw_mouse 0` if you want the cursor included.
#!/bin/bash
TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi)
ffcast -w % ffmpeg -y -f x11grab -draw_mouse 0 -show_region 1 -framerate 15 \
-video_size %s -i %D+%c -codec:v huffyuv \
-vf crop="iw-mod(iw\\,2):ih-mod(ih\\,2)" $TMP_AVI \
&& convert -set delay 10 -layers Optimize $TMP_AVI out.gif
@paldepind
paldepind / form.hs
Last active February 5, 2016 01:56
Helper function for creating forms in Reflex. The default of forms is prevented. A submit event and child is returned.
import GHCJS.DOM.EventM (event, preventDefault)
import GHCJS.DOM.Element
form :: MonadWidget t m => m a -> m (Event t (), a)
form child = do
(form, ch) <- el' "form" child
submit <- wrapDomEvent (_el_element form) elementOnsubmit (void $ preventDefault)
performEvent_ (return () <$ submit)
return (submit, ch)
@paldepind
paldepind / script.js
Created February 7, 2015 21:27
Promise inside IndexedDB transactions test
var openRequest = indexedDB.open("library");
openRequest.onupgradeneeded = function() {
// The database did not previously exist, so create object stores and indexes.
var db = openRequest.result;
var store = db.createObjectStore("books", {keyPath: "isbn"});
var titleIndex = store.createIndex("by_title", "title", {unique: true});
var authorIndex = store.createIndex("by_author", "author");
// Populate with initial data.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Simon Friis Vindum <simon@vindum.io>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@paldepind
paldepind / LICENSE.txt
Last active February 1, 2018 13:47 — forked from 140bytes/LICENSE.txt
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Simon Friis Vindum <simon@vindum.io>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@paldepind
paldepind / promise.js
Last active August 29, 2015 14:10
Ugly promise
var validateUser = function(user) {
return new Promise(function(resolve, reject) {
var validationError = c.validateFields(user, newUserRequiredFields, newUserAllowedFields);
if (validationError) {
reject(validationError);
} else {
checkUserExistence(user.username).then(resolve).catch(reject);
}
});
};
@paldepind
paldepind / fold-vs-recursion.hs
Last active December 28, 2015 13:59
Two implementations of a `findKey` function from Learn You a Haskell for Great Good. One uses explicit recursion the other a fold. The book recommends using fold for readability. But wouldn't using recursion be more efficient since the recursion halts as soon as an element is found?
-- Recursion
findKey :: (Eq k) => k -> [(k,v)] -> Maybe v
findKey key [] = Nothing
findKey key ((k,v):xs) = if key == k
then Just v
else findKey key xs
-- Fold
findKey :: (Eq k) => k -> [(k,v)] -> Maybe v
findKey key = foldr (\(k,v) acc -> if key == k then Just v else acc) Nothin
@paldepind
paldepind / delete-view.js
Last active March 23, 2021 16:33
Delete all documents returned by a view in CouchDB
var couchUrl = "foobar";
$.getJSON(couch-url + "view-name-here", function(data) {
data.rows.forEach(function (doc) {
$.ajax({
url: couch-url + doc.value._id + '?rev=' + doc.value._rev,
type: 'DELETE',
success: function(result) {
console.log("Deleted document with id " + doc.value._id);
}
});
angular.module('myApp', []).directive('includeIfVisible', function () {
function isElementInViewport(el) {
var rect = el.getBoundingClientRect();
return (
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right >= 0 &&
rect.bottom >= 0 &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
}