Skip to content

Instantly share code, notes, and snippets.

View bjjb's full-sized avatar

JJ Buckley bjjb

View GitHub Profile
#!/usr/bin/env ruby
# frozen_string_literal: true
module ParseGitURL
SSH = /^(?:[^@]+)@([^:]+):(.+)/.freeze
HTTPS = %r{^https://([^/]+)/(.+)}.freeze
def parse(input)
case input
when SSH then "#{Regexp.last_match(1)} #{Regexp.last_match(2)}"
@bjjb
bjjb / Calling Rust from Crystal
Last active September 5, 2022 14:56
Calling Rust from Crystal
# Makefile
lib = target/debug/foo.dylib
app = bin/foo
all: $(app)
$(lib): src/lib.rs Cargo.toml
cargo build
$(app): $(lib) src/foo.cr
@bjjb
bjjb / main.rs
Last active February 2, 2022 16:26
A Rust implementation of the Mars Rover exercise.
use rover::Rover;
use std::{env, io};
fn main() {
let mut r = match Rover::from_args(env::args()) {
Ok(r) => r,
Err(e) => return eprintln!("{}", e),
};
println!("{}", r.report());
loop {
@bjjb
bjjb / main.go
Created November 2, 2021 15:56
spf13/cobta completion example
// Package main is an example of using dynamic command-line completion with
// github.com/spf13/cobra.
package main
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
@bjjb
bjjb / rover.lisp
Created October 15, 2020 22:24
A Common Lisp solution for the Mars Rover exercise
;;; Mars Rover
(defun move (rover cmd)
(let ((compass '(NORTH EAST SOUTH WEST)))
(let ((x (caar rover))
(y (cadar rover))
(h (cadr rover)))
(let ((dd (case cmd ('F 1) ('B -1) (otherwise 0)))
(dh (case cmd ('R 1) ('L -1) (otherwise 0)))
(dx (case h ('EAST 1) ('WEST -1) (otherwise 0)))
@bjjb
bjjb / awsume.sh
Created April 11, 2020 17:41
Assume an AWS role with an MFA token
# awsume
# Usage: awsume ROLENAME
# It uses the _current_ AWS profile (set as an env-var or the default) to get
# the user's ARN and associated MFA devices, then it asks for a token code,
# sends the `aws sts assume-role` call to AWS, and exports the resulting session.
# /usr/bin/env bash
awsume() {
user="$(aws sts get-caller-identity --query 'Arn' --output text)"
role="${user%:*}:role/${1?Usage: $0 profile}"
@bjjb
bjjb / rover.py
Created January 13, 2020 12:44
Mars Rover
#!/usr/bin/env python
from enum import Enum, IntEnum
from typing import Dict, Callable
import unittest
class Direction(IntEnum):
NORTH = 0
EAST = 1
require 'minitest/autorun'
# A Ruby solution to Uncle Bob's Bowling Game
# https://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-kata/
module Bowling
module_function
def strike?(scores)
scores&.first == 10
@bjjb
bjjb / bowling.lisp
Created June 28, 2018 16:57
A Common-Lisp solution to Uncle Bob's Bowling Game kata
; A functional solution to Uncle Bob's Bowling Game
; https://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-kata/
(defun strike? (roll)
(and roll
(= 10 (first roll))))
(defun spare? (roll)
(and roll
(rest roll)
/*
* Converts the 2-letter code (such as "es" or "ie" to an emoji representing that country's flag.
* It works because a flag emoji, such as the German flag, is a combination of regional letter
* glyphs (🇩 🇪 without a space is rendered as 🇩🇪, the German (de) flag). Those glyphs start at
* 0x1f1e6 and end at 0x1f1ff. 'a' is 97.
*/
const flagEmojiForCode = (code) => {
const h = code.toLowerCase().charCodeAt(0) + (0x1f1e6 - 97)
const l = code.toLowerCase().charCodeAt(1) + (0x1f1e6 - 97)
return String.fromCodePoint(h, l)