Skip to content

Instantly share code, notes, and snippets.

🤗
Writing codes

John Crepezzi seejohnrun

🤗
Writing codes
Block or report user

Report or block seejohnrun

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View map_count.rb
module Enumerable
def map_count
Hash.new(0).tap { |h| each { |e| h[yield e] += 1 } }
end
end
puts [1, 2, 3, 4, 5].map_count(&:odd?) # {true=>3, false=>2}
@seejohnrun
seejohnrun / tap_try.rb
Last active Aug 29, 2015
tap_try should exist
View tap_try.rb
# Mark NoMethodError(s) coming from a call on `nil`
class NilClass
def method_missing(*)
super
rescue NoMethodError => e
e.instance_variable_set :@real_nil, true
raise e
end
end
@seejohnrun
seejohnrun / example.rb
Last active Aug 29, 2015
satirical ruby
View example.rb
class String
def |(other)
other.call(*self)
end
def >(other)
File.write(other, *self)
0
end
end
@seejohnrun
seejohnrun / generate.js
Created Dec 17, 2013
Manipulate http://make8bitart.com/ using phantomjs (and a few canvas workarounds) to let the computer be the artist this example generates: http://i.minus.com/jccK6EuB7LRdl.png
View generate.js
var url = 'https://lh6.googleusercontent.com/-NQAMokukfdE/AAAAAAAAAAI/AAAAAAAAAAA/zSaLZajdgEI/s48-c-k-no/photo.jpg';
var xoffset = 240;
var yoffset = 210;
var pixelSize = 7;
var viewportWidth = 800;
var viewportHeight = 670;
//
// And away we go
//
@seejohnrun
seejohnrun / globber
Created Jun 21, 2013
I can never remember the args to find & xargs And a lot of times, I want something a billion % simpler
View globber
#!/usr/bin/env ruby
raise 'usage: globber \'views/**/*.php\' \'echo {}\'' unless ARGV.length == 2
Dir.glob(ARGV[0]).each { |f| system(ARGV[1].gsub('{}', f)) }
@seejohnrun
seejohnrun / simpleSequence.js
Created Jun 5, 2013
A quick runner for doing operations (either synchronous or asynchronous), in series
View simpleSequence.js
// A quick runner for doing operations (either synchronous or asynchronous),
// in series
var simpleSequence = function (sequenceCallback) {
// Collect the operations
var operations = [];
sequenceCallback(function (operation) {
operations.push(operation);
});
// Do the operations
(function perform() {
@seejohnrun
seejohnrun / timebomb.js
Last active Dec 17, 2015
Tick tick tick...
View timebomb.js
// Grab a timebomb copy of a function that will expire in a certain time
// period - raising errors when called outside of that window
var timebomb = function (toCall, expireIn) {
// Expire the function call in expireIn ms
setTimeout(function () {
toCall = undefined;
}, expireIn);
// Return a wrapper function that will clear the timeout, and
@seejohnrun
seejohnrun / drawer.js
Created May 15, 2013
Move around a fixed panel
View drawer.js
(function($){
/*
* This is a jQuery extension to make it so that when you drag
* a fixed-position div's handle, you can resize the div.
*
* options.handleSelector (default='.handle') - how to find the handle
* options.direction (default='up') - the expansion direction
* options.min (default=0) - the min height (or width)
* options.max (default=clientHeight) - the max height (or width)
@seejohnrun
seejohnrun / random_k.js
Last active Oct 9, 2018
Select (n) random elements from a weighted set randomly
View random_k.js
// ported from:
http://stackoverflow.com/questions/2140787/select-random-k-elements-from-a-list-whose-elements-have-weights
// each node in the heap has a value, weight, and totalWeight
// the totalWeight is the weight of the node plus any children
var Node = {};
var newNode = function (value, weight, totalWeight) {
var node = Object.create(Node);
node.value = value;
node.weight = weight;
@seejohnrun
seejohnrun / array_partition.php
Created Mar 13, 2013
This should really just be part of PHP
View array_partition.php
<?php
// implementation
function array_partition($arr, $callable) {
$r = array(array(), array());
foreach ($arr as $e) {
$r[$callable($e) ? 0 : 1][] = $e;
}
return $r;
You can’t perform that action at this time.