Skip to content

Instantly share code, notes, and snippets.

View amoilanen's full-sized avatar
💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra

Anton Moilanen amoilanen

💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra
View GitHub Profile
@amoilanen
amoilanen / generate.sequences.js
Created March 13, 2015 16:49
Generates sequences of a given length from specified elements, i.e. all binary numbers of fixed length
function generate(elements, len) {
if (len == 1) {
return elements.map(function(element) {
return [element];
});
}
var generated = [];
var subsequences = generate(elements, len - 1);
subsequences.forEach(function(subsequence) {
@amoilanen
amoilanen / unuglify.js
Created March 13, 2015 12:14
Command line tool to make readable the code that was minified with Uglify.js
var help = [
'Command line tool to unuglify a JavaScript file.',
'',
'Before using install js-beautify:',
'',
'npm install js-beautify',
'',
'To unuglify file code.js run:',
'',
'node unuglify code.js > code.unuglified.js'
@amoilanen
amoilanen / uuid.js
Last active September 26, 2018 13:17
Simple number string generation in JavaScript, both readable and simple code. UUID generation is one of the usages.
var numberStringGenerator = ({ template, base = 10 }) => () => {
function randomNumber() {
return (Math.floor(Math.random() * base)).toString(base);
}
return template.replace(/x/g, function(match) {
return randomNumber();
});
}
@amoilanen
amoilanen / merge_object.js
Created October 30, 2014 15:25
Merging several objects
function merge() {
var sources = [].slice.call(arguments);
return sources.reduce(function(previous, source) {
Object.keys(source).forEach(function(propertyName) {
previous[propertyName] = source[propertyName];
});
return previous;
}, {});
}
@amoilanen
amoilanen / levenshtein_distance.js
Created October 18, 2014 12:05
Computes Levenshtein distance between two strings http://en.wikipedia.org/wiki/Levenshtein_distance
/*
* Computing the Levenstein distance between strings.
* http://en.wikipedia.org/wiki/Levenshtein_distance
*/
/*
* Inefficient recursive algorithm.
*/
function stringDistance(str1, length1, str2, length2) {
@amoilanen
amoilanen / map_reduce.js
Created October 12, 2014 15:46
Simplistic illustration of the map-reduce computational model in JavaScript
/*
* Simplistic map-reduce like API in JavaScript.
*
* No support for scalability or distributed computations, just an illustration of the computational model.
*/
(function(host) {
function MapReduce(inputs, mapper, reducer) {
this.inputs = inputs;
this.mapper = mapper;
@amoilanen
amoilanen / python_selenium_minimalistic_scenario.py
Created October 1, 2014 09:14
Minimalistic Selenium scenario for Firefox and Python, can be used to check if the latest Firefox has any problems with Selenium integration
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
driver.quit()
@amoilanen
amoilanen / map_as_method.js
Last active August 29, 2015 14:05
Mapping function as method
if (!Function.prototype.asMethod) {
Function.prototype.asMethod = function() {
var self = this;
return function(first) {
var rest = [].slice.call(arguments, 1);
return self.apply(first, rest);
}
};
@amoilanen
amoilanen / promisify_require.js
Last active April 1, 2021 13:54
Enhances 'require' from RequireJS with Promises API while preserving its original semantics. Now 'require' calls can be chained.
/*
* Enhances 'require' from RequireJS with Promises API while preserving its original semantics.
*/
(function() {
if (!Promise || !require) {
return;
}
@amoilanen
amoilanen / bulk_download_Coursera_videos.js
Last active May 21, 2022 02:18
Bulk download of Coursera videos with wget
/*
* Bulk download of Coursera videos with wget.
*
* Copyright (c) 2014 Anton Ivanov [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is