Skip to content

Instantly share code, notes, and snippets.

View strake's full-sized avatar

M Farkas-Dyck strake

View GitHub Profile
@strake
strake / htgk.rc
Last active December 24, 2015 17:38
HyperText GateKeeper: All requests go to given CGI script.
#!/bin/rc
fn hdrs {
ifs='
' while ( x=`{ read | sed 's/:[ ]*/:/' } && ! ~ $#x 0) {
v=`{ echo $x | sed 's/:.*//' | tr - _ }
x=`{ echo $x | sed 's/^[^:]*:[ ]*//' }
HTTP_$v=$x
}
}
@strake
strake / tcpsrv.c
Last active December 24, 2015 17:39
TCP server: spawn arguments with each connection on fds 0, 1
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
int setreuseaddr (int s, int v) {

Rust 2018

To begin, let me make this clear: I like Rust. It's a very useful tool, despite the following complaints, and the only one i use for serious systems programming any more. (I was making my own language, but really like having some nice other people do all that hard work for me, so thank you, Rust devs!)

That said, it could be better.

The Standard Library

The standard library has a major flaw: it aborts on failure to allocate memory. This is unacceptable in many of my use cases [1]. If it is acceptable, i am using Haskell, and trust me: you really want to not be competing with Haskell (and its ilk). It is frustrating as it would be easy to use/wrap a fallible API infallibly [2], but not vice versa.

module OrdNub where
import qualified Data.Set as Set
ordNubOn :: Ord b => (a -> b) -> [a] -> [a]
ordNubOn f = go Set.empty
where go _ [] = []
go s (x:xs) | f_x `elem` s = go s xs
| otherwise = x : go (Set.insert f_x s) xs
where f_x = f x
@strake
strake / with-ghc
Created February 21, 2020 01:46
Script to do a shell command with the given GHC version in the PATH
#!/bin/sh
v=$1
shift
PATH="$HOME/.ghcup/ghc/$v/bin:$PATH" exec "$@"
@strake
strake / cabal-publish
Created June 10, 2022 07:34
Script to ease uploading packages to a Hackage instance
#!/bin/sh
set -e
src_pkg_path="$(cabal sdist | sed -n '$p')"
doc_pkg_path="$(cabal haddock --haddock-for-hackage | sed -n '$p')"
set +e
cabal upload "$@" "$src_pkg_path"
cabal upload -d "$@" "$doc_pkg_path"