Skip to content

Instantly share code, notes, and snippets.

@luther9
luther9 / monad.scm
Created November 18, 2021 17:46
IO monad in Scheme
#!/usr/bin/env guile
!#
(use-modules ((srfi srfi-1) #:select (fold)))
;;; An IO monad is a function which has no parameters.
(define (io-unit x) (lambda () x))
;;; The bind function.
@luther9
luther9 / iomonad.py
Created November 18, 2021 17:42
IO monad in Python
from collections.abc import Callable
from dataclasses import dataclass
import traceback
@dataclass(frozen=True)
class IO:
_action: Callable
allowedExceptions: tuple = ()
@luther9
luther9 / monty-hall.lua
Created November 18, 2021 17:33
IO monad in Lua
#!/usr/bin/env lua
local IO
IO = {
-- Class method. Create an IO that does nothing and returns x.
unit = function(x)
return IO(function() return x end)
end,
@luther9
luther9 / defaultrandseed.go
Created October 25, 2018 15:41
Not seeding the RNG is equivalent to a seed of 1.
package main
import (
"fmt"
"math/rand"
)
func show5(f func() int) {
for i := 0; i < 5; i++ {
fmt.Println(f())
@luther9
luther9 / blacklistlinks.js
Last active June 17, 2018 02:41
A Greasemonkey script to block certain links on all websites
#!/usr/bin/env python3
# John's version.
def shuffle(deck):
return [e for l in [a for a in zip(deck[:(len(deck)//2)], deck[(len(deck)//2):])] for e in l]
# Slightly simplified. Above, the inner comprehension has just "a for a" with no
# "if" at the end, so we can reduce it down to just the iterator.