Skip to content

Instantly share code, notes, and snippets.

View edfletcher's full-sized avatar

Ed Fletcher edfletcher

View GitHub Profile
@edfletcher
edfletcher / BasicPubSub.js
Created March 4, 2024 23:45
The most basic event subscriber / pub-sub implementation, for illustration purposes
class BasicPubSub {
constructor() {
this.channel_subs = {};
}
subscribe(channel, subscriber) {
if (!this.channel_subs[channel]) {
this.channel_subs[channel] = new Set();
}
@edfletcher
edfletcher / findInObjByDottedPath.js
Last active February 29, 2024 05:14
findInObjByDottedPath()
function findInObjByDottedPath(path, obj) {
const pathElements = path.split('.');
const element = obj[pathElements[0]];
if (element) {
if (typeof(element) === "object" && !Array.isArray(element)) {
return findInObjByDottedPath(
pathElements.slice(1).join('.'),
element
);
const __q = [];
class MPromise {
constructor(ctorCallback) {
this.__thens = [];
this.__catches = [];
this.resolve = (val) => {
this.__thens.forEach((thenCb) => thenCb(val));
};
this.reject = (rejectReason) => {
@edfletcher
edfletcher / node-test-mock-methods.md
Created October 17, 2023 00:36
Using node's new test mock to replace instance methods
const { mock } = require('node:test');

class ToBeMocked {
    constructor() {
        console.log('ToBeMocked is born!');
    }

    aMethod(oneArg) {
 console.log(`ToBeMocked.aMethod(${oneArg})`);
@edfletcher
edfletcher / llms_debate_ai.md
Created April 2, 2023 04:40
Two LLMs debate AI's effect on humanity

Two instances of ggml-alpaca-7b-q4.bin running via alpaca - one at (the default) temperature of 0.1 and the other at 0.9 - debate AI's effect on humanity! "They" even get into government regulation!

This was not human-initiated: the initial prompt (to the t=0.9 instance) was only "present a controversial opinion", and the response is the first line in the resulting debate below.

Every iteration after that just prompted with "present a counterargument to the following: '...'" where '...' is the previous response (stripping any repeition of the original prompt, which does happen sometimes).

Each line below is one iteration in that process, entirely unedited.


@edfletcher
edfletcher / loadPlugin.go
Created February 5, 2023 22:21
loadPlugin.go
func loadPlugin(path string) (*PluginImpl, error) {
ifaceType := reflect.TypeOf((*Plugin)(nil)).Elem()
pluginLoad, err := plugin.Open(path)
if err != nil {
return nil, err
}
// without stubbing the fields, reflect.ValueOf(...).Elem().FieldByName(...) below will return nil
newPlugin := newPluginImpl()
async function scopedRedisClient (scopeCb) {
const scopeClient = new Redis(config.redis.url);
try {
return await scopeCb(scopeClient, PREFIX);
} catch (e) {
console.error(e);
} finally {
scopeClient.disconnect();
}
//let s = '.ban ?user @user ?r reason ?dm <dms the member>';
let s = '!test ?r reason ?dm ?t time ?p'
// split the string by spaces
let sa = s.split(/\s+/);
// create a map to track things, and filter only the "?cmd" tokens
let cmd_tokens = sa.map((token, index) => ({ token, index }))
.filter(({ token }) => token.indexOf('?') === 0);
@edfletcher
edfletcher / async_gen_ex.js
Created March 18, 2022 22:42
async_gen_ex
async function* tick(cadence = 1000) {
let tickCount = 0;
while (true) {
yield new Promise((res) =>
setTimeout(() => res(tickCount++), cadence));
}
}
for await (const tickCount of tick()) {
console.log(new Date(), tickCount);