Skip to content

Instantly share code, notes, and snippets.

$ Number.prototype.divmod = function (n) { return [Math.floor(this / n), this % n]; }
=> function (n) {
return [Math.floor(this / n), this % n];
}
$ typeof 1
=> "number"
$ typeof 1.0
=> "number"
$ 1.0.divmod
=> embedded:16 function (n) {
@epitron
epitron / lugcast.js
Last active July 3, 2016 18:18
An audio player I wrote for the SteamLUG Podcast (https://steamlug.org/cast). It highlights the transcript as the audio is playing, and lets you jump around in the audio by clicking the transcript.
(function () {
"use strict";
function time_to_seconds(time) {
var s = time.attributes.datetime.value.split(":");
return parseInt(s[0] * 3600, 10) + parseInt(s[1] * 60, 10) + parseInt(s[2], 10);
}
var highlighter = {
@epitron
epitron / array_tweaks.coffee
Last active August 29, 2015 14:10
Extending JavaScript's Array object in CoffeeScript
class Array
all: (f)->
for e in this
return false unless f(e)
true
any: (f)->
for e in this
return true if f(e)
false
@epitron
epitron / bits.coffee
Last active August 29, 2015 14:08
Monkeypatching base JavaScript types (Array, Number) using CoffeeScript!
Number::log = (base)-> Math.log(this)/Math.log(base)
Number::pow = (exp)-> Math.pow(this, exp)
Number::ceil = -> Math.ceil(this)
Number::floor = -> Math.floor(this)
Array::first = -> this[0]
Array::last = -> this[@length-1]
Array::min = -> @sort().first()
Array::max = -> @sort().last()
Array::minmax = -> sorted = @sort(); [sorted.first(), sorted.last()]
@epitron
epitron / background.js
Created October 17, 2014 18:38
Sample Chrome Extension
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.commands.onCommand.addListener(function(command) {
console.log('onCommand event received for message: ', command);
});
# examples
spec = {
"apiKey" => String,
"language" => ["en", "ru"],
"clientData" => {
"userName" => String,
"userCity" => String,
"userPhoneNo" => String,
"userPassword" => String
def some_combination_of_the_numbers_adds_to_the_biggest_number(arr)
arr = arr.sort
biggest = arr.pop
(2..arr.size).each do |perm_size|
arr.permutation(perm_size).each do |nums|
sum = nums.reduce(:+)
return true if sum == biggest
end
end
@epitron
epitron / lstat_pyramid.txt
Last active August 29, 2015 14:07
Ruby's "lstat pyramid" (this is executed for every file it attempts to open)
lstat("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib", {st_mode=S_IFDIR|0755, st_size=200704, ...}) = 0
lstat("/usr/lib/ruby", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems/2.1.0", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems/2.1.0/gems", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems/2.1.0/gems/slop-3.6.0", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems/2.1.0/gems/slop-3.6.0/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib/ruby/gems/2.1.0/gems/slop-3.6.0/lib/slop.rb", {st_mode=S_IFREG|0644, st_size=19793, ...}) = 0
open("/usr/lib/ruby/gems/2.1.0/gems/slop-3.6.0/lib/slop/option.rb", O_RDONLY|O_CLOEXEC) = 7
@epitron
epitron / primes.py
Last active April 20, 2021 07:42
Fast prime number generators in Python
#!/usr/bin/env python3
from itertools import islice
from time import time
def time_generator(func, n=500000):
generator = func()
start = time()
islice(generator, n)
elapsed = time() - start
@epitron
epitron / kv_bench.rb
Last active May 18, 2020 05:21
Benchmark Ruby's various key/value databases (GDBM, SDBM, CDB)
require 'epitools'
%w[gdbm sdbm cdb].each { |lib| require lib }
DBS = [
[GDBM, GDBM],
[SDBM, SDBM],
[CDBMake, CDB ],
]
NUM = 100000