Skip to content

Instantly share code, notes, and snippets.

View paldepind's full-sized avatar

Simon Friis Vindum paldepind

  • Copenhagen, Denmark
  • 04:48 (UTC +02:00)
View GitHub Profile
@paldepind
paldepind / gist:4662669
Last active December 11, 2015 21:28
C++ solution the problem "Balanced Smileys" in the Facebook Hackecup 2013. No stacks, no recursion. Complexity is O(n).
bool isBalanced(const string message) {
int paransOpen = 0;
int maybeOpen = 0;
int maybeClosed = 0;
char prevChar = 0;
string::const_iterator iter;
for (iter = message.begin(); iter < message.end(); iter++) {
if (*iter == '(') {
if (prevChar == ':') {
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int lamps, opsToDo;
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)
);
}
@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);
}
});
@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 / 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 / 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
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 / 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.
@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)