Skip to content

Instantly share code, notes, and snippets.

View jonathonherbert's full-sized avatar

Jonathon Herbert jonathonherbert

View GitHub Profile
@jonathonherbert
jonathonherbert / script.sh
Last active April 22, 2024 15:26
Select git branch by date (requires fzf)
fbr() {
local branches branch
branches=$(git reflog show --pretty=format:'%gs ~ %gd' --date=relative \
| grep 'checkout:' \
| grep -oE '[^ ]+ ~ .*' \
| awk -F~ '!seen[$1]++' \
| head -n 20 \
| awk -F' ~ HEAD@{' '{printf(" \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}' \
| nl \
| sort -nr \
@jonathonherbert
jonathonherbert / example.scala
Created August 10, 2023 10:56
For comprehension desugaring
object ExampleObj {
val getFirstThing: () => Either[Throwable, String] = () => Left(new Exception("dsa"))
val getSecondThing: () => Either[Throwable, String] = () => Right("second thing")
val getThirdThing: () => Either[Throwable, String] = () => Right("third thing")
val result = getFirstThing().flatMap(firstThing => {
getSecondThing().flatMap(secondThing => {
getThirdThing().flatMap(thirdThing => {
Right(thirdThing)
})
@jonathonherbert
jonathonherbert / example.js
Created January 31, 2023 19:07
HTML template example
const episodeList = [{
name: 'A great show',
season: 1,
summary: '<p>Seriously, really good',
image: 'https://an-image/img.jpg'
},
{
name: 'Another great show',
season: 2,
https://glitch.com/edit/#!/busy-loving-snagglefoot?path=index.html%3A31%3A0
@jonathonherbert
jonathonherbert / type-key-val-pairs.ts
Last active May 5, 2021 13:53
Typing key/value pairs generically in function signatures in Typescript
type Fields = Record<string, number>
type Obj<ObjKey extends string, ObjVals extends Fields> = { [key in ObjKey]: ObjVals }
const obj = { "A": { fieldOne: 1 }, "B": { fieldTwo: 2 } }
const acceptsObj = <TKeys extends string, TFields extends Fields>(obj: Obj<TKeys, TFields>) => {
return <Key extends TKeys>(key: Key, fields: Obj<TKeys, TFields>[Key]) => undefined;
}
const fn = acceptsObj(obj)
@jonathonherbert
jonathonherbert / closure-demo.js
Created August 11, 2020 14:18
Demo of closure behaviour
"use strict";
class ExampleAsyncService {
currentSearch = undefined;
search(searchTerms) {
this.currentSearch = searchTerms;
console.log(`Searching for ${searchTerms}`);
lazy val adminToolsLib = project("admin-tools-lib", Some("admin-tools/lib"))
.settings(
excludeDependencies ++= Seq(
ExclusionRule("org.elasticsearch"),
ExclusionRule("com.sksamuel.elastic4s"),
ExclusionRule("com.drewnoakes", "metadata-extractor"),
ExclusionRule("org.codehaus.janino"),
ExclusionRule("com.typesafe.play"),
ExclusionRule("org.scalaz.stream"),
ExclusionRule("org.im4java"),
@jonathonherbert
jonathonherbert / ApiController.scala
Last active December 9, 2019 13:15
Websockets example
// in ApiController body
def checkStream: WebSocket = WebSocket.accept[JsValue, JsValue] { _ =>
ActorFlow.actorRef(out =>
CheckStreamActor.props(out, matcherPool)
,
// A large buffer is necessary to ensure messages aren't dropped -- see
// https://github.com/playframework/playframework/issues/6246.
10000)
}
// @ts-check
const levenshtein = require("fast-levenshtein");
const crypto = require("crypto");
const traceHashMap = {};
const trace = n => {
const hash = crypto.createHash("sha1");
hash.update(n.toString());
@jonathonherbert
jonathonherbert / exampleDataStructures.js
Created May 10, 2018 09:27
Example data structures for generic collections module
// Collection (in state)
{
// We've married the config and collections objects
// here, as the fact that they're separated is not relevant
// to the module
displayName: name,
lastModifyBy: Reetta
live: [{
// There's some nesting here - we're unsure how we'd manage