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 / grab_jsjabber_episodes.rb
Created February 16, 2014 21:55
Simple script that downloads all the JavaScript Jabber http://javascriptjabber.com/ episodes
#Downloads all the mp3 episode recordings from JavaScript Jabber http://javascriptjabber.com
#Assumes that the system 'wget' command is available which is the case on Linux
#Episodes will be downloaded to /home/user/JSJabber
require 'net/http'
def get(uri)
response = Net::HTTP.get_response(URI.parse(uri))
if response.code == "301"
response = Net::HTTP.get_response(URI.parse(response.header['location']))
end
@amoilanen
amoilanen / couchrest_api_example.rb
Created February 23, 2014 19:14
CouchDB example using couchrest and couchrest_model
require 'rubygems'
require 'couchrest'
require 'couchrest_model'
require 'json'
server = CouchRest.new
class JobOpening < CouchRest::Model::Base
use_database "job_openings"
@amoilanen
amoilanen / function.name.js
Last active August 29, 2015 13:59
Simple function to determine the name of a JavaScript function in case https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name is not defined
function functionName(func) {
var match = func.toString().match(/function\s+([^(?:\()\s]*)/);
return match ? match[1] : "";
}
/*
* Tests
*/
var func1 = function(x, y) {
@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 / 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_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 / 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 / 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 / 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 / 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) {