Skip to content

Instantly share code, notes, and snippets.

View perlmonger42's full-sized avatar

Thom Boyer perlmonger42

View GitHub Profile
@perlmonger42
perlmonger42 / jq.jq
Created December 12, 2019 20:44
jq examples
#!/usr/bin/env bash
# For a similar tool for wrangling HTML rather than JSON, see
# [Pup](https://github.com/ericchiang/pup)
jq --run-tests <<'END-OF-JQ-TESTS' && echo -e "\e[7;30;42mSUCCESS: ALL TESTS PASSED\e[0;37;40m" || echo "\e[7;30;41mFAILURE\e[0;37;40m"
# jq code follows
#
# old shebang line:
#! /usr/bin/env jq -M --run-tests
@perlmonger42
perlmonger42 / subclass-constants.rb
Last active June 26, 2019 18:44
[Ruby] Providing different values for a constant across the subclasses of a class
# How do you write a method in a base class that uses a constant value provided
# by its various subclasses?
class BaseGreeter
# This does not work, because GREETING is undefined.
# Apparently Ruby resolves all-caps identifiers (i.e., constants) using a
# different mechanism than for lower-case identifiers (i.e., methods).
def speak1
begin
@perlmonger42
perlmonger42 / flexagon.json
Last active June 12, 2019 14:44
Dodeca-hexa-flexagon flexing map (paste RAW URL into "data=d3.json(...)" statement at https://observablehq.com/@d3/force-directed-graph
{
"nodes": [
{"id": "Y", "group": 1, "groupcolor": "blue"},
{"id": "BR1f", "group": 1},
{"id": "BY1g", "group": 1},
{"id": "B2h", "group": 2, "groupcolor": "orange"},
{"id": "RB11f", "group": 2},
{"id": "Y2j", "group": 3, "groupcolor": "purple"},
@perlmonger42
perlmonger42 / Intensely-Do.go
Last active December 12, 2019 20:55
Dynamic Re-Dispatch, in Go
// Demonstration of how to emulate class-based dynamic redispatch in Go.
// This is a pattern in which a method of a base class,
// which is inherited by subclasses without override,
// is implemented in terms of overridden versions of other methods.
//
// This may be an answer to, or it may be tangential to, [a question on
// StackOverflow](https://stackoverflow.com/questions/49114057/idiomatic-way-to-mimic-proper-dynamic-dispatch-in-go).
//
// In Go, the right way to do it seems to be to have the inherited base-class
// method not be a method. Instead, it's a function that takes an interface
@perlmonger42
perlmonger42 / Tic-Tac-Toe-Winner.js
Created August 30, 2012 22:49
JavaScript function to determine whether a game of tic-tac-toe has been finished.
// Game_Board will have two slots:
// .boardString represents the contents of the 9 cells of the board
// .winner indicates whether or not the game has been won, and by whom
//
// Each cell in the game board has a cell #, as shown in this chart:
// | |
// 0 | 1 | 2
// ----+---+----
@perlmonger42
perlmonger42 / reverse.pl
Created August 16, 2012 20:25
reverse_string(STRING): return a reversed copy of the input string
# Normally, you'd just say
# reverse STRING
# since Perl provides a builtin for this functionality, but that would be
# cheating based on the rules of the assignment.
# my $desrever = reverse_string(STRING);
#
# Return a reversed copy of the input string.
sub reverse_string {
return join '', reverse split //, $_[0];
@perlmonger42
perlmonger42 / reverse.c
Created August 16, 2012 20:20
reverse(char *s): reverse a string's content in place
/*
* char *ptr = reverse(STR);
*
* Reverse the characters of STR[] in place, and return STR.
*/
char *reverse(char *s) {
char ch, *p, *q;
/* Point q at the last character in s[], or at &s[0] if s is empty. */