Skip to content

Instantly share code, notes, and snippets.

View hippietrail's full-sized avatar

Andrew Dunbar hippietrail

  • Phong Nha
  • 14:26 (UTC +07:00)
View GitHub Profile
@hippietrail
hippietrail / optional_u64_formatter.zig
Created May 17, 2024 12:09
Minimal Zig of a custom formatter
const std = @import("std");
pub fn main() void {
var foo: ?u64 = 101;
std.debug.print("optional u64 foo = '{}'\n", .{optionalU64(foo)});
foo = null;
std.debug.print("optional u64 foo = '{}'\n", .{optionalU64(foo)});
foo = 1000000;
std.debug.print("optional u64 foo = '{}'\n", .{optionalU64(foo)});
}
@hippietrail
hippietrail / custom_fprintf.c
Last active March 23, 2024 19:38
Example of how to wrap C printf type functions to add a new format specifier
// adds the %hb specifer to print a number of bytes in human-readable number of bytes/kb/mb
// there's no way to modify or build a va_list after handling some of them ourselves
// so we get the standard function to ignore them by treating them as zero-length strings
// this could be a problem for compilers that evaluate a number as a string pointer before truncating
// but it works with clang on Xcode 15.3
// there's no buffer overflow handling to make the code easy to follow - don't deploy as-is in real code!
void custom_fprintf(FILE* stream, const char* format_orig, ...) {
size_t format_orig_len = strlen(format_orig);
va_list args_orig, args_copy;
@hippietrail
hippietrail / fetch-wikipedia-text.swift
Last active May 10, 2024 16:14
Swift code to fetch one or more English Wikipedia articles as plain text without most of the interface text
import Foundation
/// Executes two commands in a pipeline.
///
/// - Parameters:
/// - firstCommand: The first command to execute. This should be a valid
/// command that you would type into the terminal.
/// - secondCommand: The second command to execute. This should be a valid
/// command that you would type into the terminal.
/// - args: The command line arguments passed to the script.
@hippietrail
hippietrail / fetch-youtube-transcript.ts
Last active May 8, 2024 08:22
TypeScript code to fetch one or more YouTube transcripts as plain text without API key
import url from 'url';
import parse from 'html-dom-parser';
import { Element, Text } from 'domhandler';
import { decodeXML } from 'entities';
async function getHtmlByVideoID(videoID: string): Promise<string> {
const reponse = await fetch(url.format({
protocol: 'https',
hostname: 'www.youtube.com',
@hippietrail
hippietrail / introrx.md
Last active August 29, 2015 14:21 — forked from staltz/introrx.md

The introduction to Reactive Programming you've been missing

(by @andrestaltz)

So you're curious about learning this new thing called Reactive Programming, particularly its variant comprising of Rx, Bacon.js, RAC, and others.

Learning it is hard, even harder due to the lack of good material. When I started, I tried looking for tutorials. I found only a handful of practical guides, but they just scratched the surface and never tackled the challenge of building the whole architecture around it. Library documentation often don't help when you're trying to understand some function. I mean, honestly, look at this:

Rx.Observable.prototype.flatMapLatest(selector, [thisArg])

Projects each element of an observable sequence into a new sequence of observable sequences by incorporating the element's index and then transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.

@hippietrail
hippietrail / linebyline.js
Created January 5, 2013 11:40
My attempt at the minimal code to read text files one line at a time in node.js
var fs = require('fs'),
StringDecoder = require('string_decoder').StringDecoder,
util = require('util');
function lineByLine(fd) {
var blob = '';
var blobStart = 0;
var blobEnd = 0;
var decoder = new StringDecoder('utf8');
@hippietrail
hippietrail / ht-se-search-google.user.js
Created December 2, 2012 04:08
Convert a search on a StackExchange site to a Google search of that site
// ==UserScript==
// @name Hippietrail's StackExchange search to Google search
// @description Convert a search on a StackExchange site to a Google search of that site
// @version 0.0
// @namespace hippietrail
// @include http://stackoverflow.com/search*
// @include http://meta.stackoverflow.com/search*
// @include http://serverfault.com/search*
// @include http://meta.serverfault.com/search*
// @include http://superuser.com/search*
@hippietrail
hippietrail / se-sort-tags-by-relevance.js
Created October 19, 2012 06:24
Sort Stack Exchange tags by relevance
var $c = $('#h-related-tags').parent().contents(),
s = null,
arr = [];
$c.each(function(i, e) {
var $sl, counts;
if (e.tagName === 'A') {
s = i;
} else if (e.tagName == 'BR') {
@hippietrail
hippietrail / google-search-se-questions.js
Created October 18, 2012 17:26
Get Stack Exchange question IDs from Google Search results
$('a.l').map(function(i, e) {
return !e.search ? e.href : (function(s) {
var u = '?';
s.split('&').forEach(function(ee, ii) {
var e = ee.split('=');
if (e[0] == 'url')
u = e[1];
});
return decodeURIComponent(u);
})(e.search);
@hippietrail
hippietrail / se-add-search-result.js
Created October 18, 2012 17:19
Add a new entry to Stack Exchange search results
$('<div>', {
class: 'question-summary',
id: 'question-summary-QID',
html: $('<div>', {
class: 'statscontainer',
html: $('<div>', {
class: 'statsarrow'
})
}).append($('<div>', {
class: 'stats',