Skip to content

Instantly share code, notes, and snippets.

View mheiber's full-sized avatar

Max Heiber mheiber

View GitHub Profile
(* run like this: ocamlc unix.cma par.ml && ./a.out *)
open StdLabels
let par_iter (items: 'i list) ~(f: 'i -> unit): unit =
let orig_pid = Unix.getpid () in
let rec loop pids = function
| [] -> pids
| h :: t ->
match Unix.fork () with
| 0 -> f h; pids
@mheiber
mheiber / toggle.vim
Last active November 10, 2022 00:01
" usage: \b toggles the file browser, open to the directory containing the current file
nnoremap <leader>b :call ToggleNERDTree()<Cr>
function! ToggleNERDTree()
if !g:NERDTree.ExistsForTab() || !g:NERDTree.IsOpen()
if <SID>WindowFileExists()
:NERDTreeFind
else
let cwd = getcwd()
module Compact_array = struct
type 'a t = { len: unit -> int
 ; get: int -> 'a
 ; set: int -> 'a -> unit
 }
let of_string s =
 { len = (fun () -> String.length s)
 ; get = (fun i -> String.get s i)
 ; set = (fun i x -> String.set s i x)
 }
(* adapted from https://blog.janestreet.com/why-gadts-matter-for-performance/*)
module Compact_array : sig
type 'item t
val of_bytes : bytes -> char t
val of_array : 'item array -> 'item t
val length : 'item t -> int
val get : 'item t -> int -> 'item
end = struct
type 'a t =
struct
type 'out t = < get : int -> 'out ; length : int >
let of_bytes v =
object
method length = bytes.length v
method get i = bytes.get v i
end
let of_array v =
#lang typed/racket
; try this in Dr. Racket
(: bad-union (All (k) (-> (U (Listof k) (Listof (Listof k))) (Listof k))))
(define (bad-union x) '())
(: use-bad-union Any)
(define (use-bad-union) (bad-union '()))
@mheiber
mheiber / refactor-most-fowl.js
Created March 25, 2022 20:59
A Refactor Most Fowl
// From "Refactoring" 2nd Edition by Martin Fowler
export default function createStatementData(invoice, plays) {
const result = {};
result.customer = invoice.customer;
result.performances = invoice.performances.map(enrichPerformance);
result.totalAmount = totalAmount(result);
result.totalVolumeCredits = totalVolumeCredits(result);
return result;
trait A {}
struct SasA{}
impl A for SasA {}
trait Db<'d> {
type Mem: ?Sized + A + 'd;
fn d(&self) -> &Self::Mem;
}
fn main() {
let make_a = |n| move || n;
let make_b = |n| move || n;
vec![make_a(1), make_a(2)];
vec![make_a(1), make_b(1)];
vec![|| 1, || 1];
}

Vim Advice

For best efficiency: Keep the Bindings, Leave the Editor

Most of the efficiency and joy of Vim is in the keybindings. All popular editors have Vim bindings. When first learning (and after), one can spare oneself a lot of pain by just focusing on the bindings.

The key idea is:

  • "insert mode" is for inserting text, and that is all.
  • "normal mode" is for navigating and manipulating code. Then all the keys can be used for useful things besides inserting text. What keys do is based mostly on easy-to-reachness and menmonics.