Skip to content

Instantly share code, notes, and snippets.

View Sophia-Gold's full-sized avatar

Sophia Gold Sophia-Gold

View GitHub Profile
@Sophia-Gold
Sophia-Gold / SafeMath.ml
Created March 6, 2020 19:39
OCaml arithmetic with no overflow
(* From http://caml.inria.fr/pub/ml-archives/caml-list/2004/06/8730fc362310ad9db0c2239b5b815a22.en.html *)
exception Overflow
let ( + ) a b =
let c = a + b in
if (a lxor b) lor (a lxor (lnot c)) < 0 then c else raise Overflow
let ( - ) a b =
let c = a - b in

Safe ways to do things in bash

Why bash?

Sometimes, a shellscript is the right tool for the job. At that, Bash has arrays and a safe mode, which in large part makes it possible and practical (respectively) to write scripts without glaring bugs. Other times, you are invoking the shell because that's what you get.

-- https://aupiff.com/eliminating-explicit-parentheses-with-a-handy-combinator
u = (:) 'g' (show 13)
u' = (.) (.) (:) 'g' show 13
v = length ((:) 1 [4,2])
v' = (.) (.) (.) length (:) 1 [4,2]
w = (++) "elab" ((:) 'o' "rate")
w' = (.) ((.) (.) (.)) (++) "elab" (:) 'o' "rate"
(defn collapse
"Collapses a tree of nested collections
into a vector indexed by depth
in the original structure."
[tree]
(loop [flat (transient [])
tree tree]
(if (empty? tree)
(persistent! flat)
(recur (conj! flat
@Sophia-Gold
Sophia-Gold / bfs.clj
Created June 7, 2018 05:09
breadth-first search is annoying in Clojure
;; https://stackoverflow.com/questions/11409140/stumped-with-functional-breadth-first-tree-traversal-in-clojure
(def tree [1 [2 [4] [5]] [3 [6]]])
(defn bfs
[tree]
(loop [ret []
queue (conj [] tree)]
(if (seq queue)
(let [[node & children] (first queue)]
@Sophia-Gold
Sophia-Gold / curry.clj
Last active May 19, 2018 23:35 — forked from rlm/gist:746185
curry.clj
(ns sunil.curry)
(defn partial+
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args.
differs from the core version in that it works on just one argument."
{:added "1.0"}
([f] f)
([f arg1]
(defn tally [n]
(apply str
(concat
(repeat (quot n 5) "卌")
(repeat (mod n 5) "|"))))
(map tally (range 1 11))
;; ("|" "||" "|||" "||||" "卌" "卌|" "卌||" "卌|||" "卌||||" "卌卌")
@Sophia-Gold
Sophia-Gold / discont.clj
Created May 1, 2018 14:00
Disassembling with Continuations
(ns core.clj
(:require [clojure.string :as str]
[fipp.edn :refer [pprint] :rename {pprint fipp}]
[com.positronic-solutions.pulley.cps :refer :all]
[no.disassemble :refer :all]))
(defn stacks []
(->> disassemble
call-cc
str/split-lines
@Sophia-Gold
Sophia-Gold / cc.clj
Created April 24, 2018 18:32
disassembled continuation
{:minor-version 0,
:major-version 49,
:name anaphora$get_cc$fn__9738$fn__9739,
:interface-names [],
:fields
({:access-flags #{},
:name cc,
:descriptor "Ljava/lang/Object;",
:synthetic? false,
:deprecated? false,
@Sophia-Gold
Sophia-Gold / church.scm
Created April 22, 2018 02:03
church numerals
(define zero (lambda (f) (lambda (x) x)))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(define three (lambda (f) (lambda (x) (f (f (f x))))))
(define four (lambda (f) (lambda (x) (f (f (f (f x)))))))
(define five (lambda (f) (lambda (x) (f (f (f (f (f x))))))))
(define six (lambda (f) (lambda (x) (f (f (f (f (f (f x)))))))))
(define seven (lambda (f) (lambda (x) (f (f (f (f (f (f (f x))))))))))
(define eight (lambda (f) (lambda (x) (f (f (f (f (f (f (f (f x)))))))))))
(define nine (lambda (f) (lambda (x) (f (f (f (f (f (f (f (f (f x))))))))))))