Skip to content

Instantly share code, notes, and snippets.

View raybellis's full-sized avatar

Ray Bellis raybellis

  • Oxfordshire, England
View GitHub Profile
@raybellis
raybellis / jquery.queued.js
Created October 2, 2012 06:45
A jQuery plugin to allow any function to be queued (default 'fx' queue only)
(function($) {
$.fn.queued = function() {
var self = this;
var func = arguments[0];
var args = [].slice.call(arguments, 1);
return this.queue(function() {
$.fn[func].apply(self, args).dequeue();
});
}
}(jQuery));
@raybellis
raybellis / jquery.loop.js
Created October 15, 2012 12:05
jQuery loop plugin
(function($) {
$.loop = function(n, cb, ctx) {
for (var i = 0; i < n; ++i) {
if (cb.call(ctx, i) === false) break;
}
};
})(jQuery);
@raybellis
raybellis / jquery.classlist.js
Last active December 14, 2015 16:38
A shim for jQuery that uses the native "classList" property of an element for class modifications, if available.
/*global jQuery */
;(function($) {
/*global document */
"use strict";
if (typeof document !== 'undefined' && ('classList' in document.createElement('a'))) {
var $ = jQuery;
@raybellis
raybellis / jquery.memoize.js
Last active December 15, 2015 11:39
Non-persistent and Persistent memoized functions
;(function($) {
"use strict";
$.memoize = function(factory, ctx) {
var cache = {};
return function(key) {
if (!(key in cache)) {
cache[key] = factory.call(ctx, key);
}
@raybellis
raybellis / merger.c
Created March 28, 2018 14:55
16-bit ROM merger
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
int main(int argc, char *argv[])
{
const size_t bufsize = 4096;
uint8_t buf_a[bufsize];
@raybellis
raybellis / memoize_multi.js
Created July 31, 2020 14:56
Generic memoize function supporting multiple parameter key lookups
function memoize(factory, ctx, mapper) {
const cache = new Map();
return function(...keys) {
const key = mapper ? mapper(keys) : keys[0];
if (!cache.has(key)) {
cache.set(key, factory.apply(ctx, keys));
}
console.debug(cache);
return cache.get(key);
}
@raybellis
raybellis / div10.s
Created October 1, 2020 10:06
16-bit division by 10, for 6502
; input value location
.exportzp X0 := $10
.exportzp X1 := X0 + 1
; final result appears in [R3:R2]
.exportzp R0 := X1 + 1
.exportzp R1 := R0 + 1
.exportzp R2 := R0 + 2
.exportzp R3 := R0 + 3
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
extern void lcddata(unsigned char n);
static inline uint16_t
display_uint16_t_digit(
uint16_t n, uint16_t div,
@raybellis
raybellis / random.js
Last active March 15, 2022 21:17
Emulation of java.util.Random in Javascript
class UInt48 {
constructor(n) {
if (n instanceof UInt48) {
Object.assign(this, n);
} else if (typeof n === 'number') {
let w0 = n & 0xffff;
n /= 0x10000;
let w1 = n & 0xffff;
n /= 0x10000;
@raybellis
raybellis / ArrayModalValue.swift
Created November 16, 2022 22:39
A Swift extension to find the most common (modal) value among an array of (hashable) values
extension Array where Element : Hashable {
func modalValue() -> Self.Element? {
// populate a dictionary of element vs count
var dict: [Self.Element : Int] = [:]
self.forEach { v in
if let count = dict[v] {
dict[v] = count + 1
} else {