Skip to content

Instantly share code, notes, and snippets.

View Wilfred's full-sized avatar

Wilfred Hughes Wilfred

View GitHub Profile
@Wilfred
Wilfred / dyn_prop.py
Last active November 22, 2023 16:01
dynamically defined properties in python
def attach_dyn_prop(instance, prop_name, prop_fn):
"""Attach prop_fn to instance with name prop_name.
Assumes that prop_fn takes self as an argument.
Reference: https://stackoverflow.com/a/1355444/509706
"""
class_name = instance.__class__.__name__ + 'Child'
child_class = type(class_name, (instance.__class__,), {prop_name: property(prop_fn)})

Template composition with inclusion

Every template language I have seen provides some mechanism for one template to include another, thus supporting the reuse of repeated elements like headers and footers. The included templates are called partials in Mustache parlance:

<!-- home.hbs -->
<html>
<body>
  {{> header}}
  <p> HOME </p>
  {{> footer}}
@Wilfred
Wilfred / init.lua
Created June 8, 2021 18:46
switch to firefox with hammerspoon
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "F", function()
local app = hs.application'firefox'
if app ~= nil then
app:activate()
end
end)
@Wilfred
Wilfred / Cargo.toml
Last active January 21, 2021 17:51
Calling a C function from Rust
[package]
name = "remacs"
version = "0.1.0"
authors = ["Wilfred Hughes <me@wilfred.me.uk>"]
[lib]
crate-type = ["staticlib"]
@Wilfred
Wilfred / flatten.py
Last active December 20, 2020 15:06
flatten an arbitrarily nested list in Python
from copy import deepcopy
def flatten_list(nested_list):
"""Flatten an arbitrarily nested list, without recursion (to avoid
stack overflows). Returns a new list, the original list is unchanged.
>> list(flatten_list([1, 2, 3, [4], [], [[[[[[[[[5]]]]]]]]]]))
[1, 2, 3, 4, 5]
>> list(flatten_list([[1, 2], 3]))
[1, 2, 3]
@Wilfred
Wilfred / regex.org
Last active January 7, 2020 17:22
Native Rust Regular Expressions in Remacs

RFC: Use Rust’s regex crate

We want to port Remacs to use a regex crate implemented in Rust. The Rust implementations are highly optimised, and this would simplify the Remacs codebase.

The two major crates are Rust’s regex crate, and the fancy-regex crate.

@Wilfred
Wilfred / XHP ns token.md
Created October 9, 2019 19:05 — forked from fredemmott/XHP ns token.md
XHP ns token
@Wilfred
Wilfred / dashtest.el
Last active September 18, 2019 11:21
dolist vs mapcar
;;; dashtest.el --- benchmarking dash -*- lexical-binding: t -*-
(defmacro --map-mapcar (form list)
(declare (debug (form form)))
`(mapcar (lambda (it) ,form) ,list))
(defmacro --map-loop (form list)
(declare (debug (form form)))
(let ((result-sym (make-symbol "result")))
`(let (,result-sym)
@Wilfred
Wilfred / MalType.st
Created June 23, 2019 15:15
MAL equality pharo
= anObject
self isIterable & anObject isIterable
ifTrue: [ ^ value = anObject value ].
self class = anObject class
ifFalse: [ ^ false ].
^ value = anObject value
@Wilfred
Wilfred / julia_syntax_test.jl
Last active June 19, 2019 19:33
Corner cases for Julia syntax highlighting
## Julia syntax highlighting test.
# This file is designed to test various corner cases of Julia
# syntax highlighting.
## Simple function definitions.
# Expected: `function` should be highlighted, as should `foo_bar!`.
function foo_bar!(x,y)
x + y + 1
end