Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / dashtest.el
Created July 29, 2017 14:59 — forked from Wilfred/dashtest.el
dolist vs mapcar
;;; dashtest.el --- benchmarking dash -*- lexical-binding: t -*-
(defmacro --map-mapcar (form list)
(declare (debug (form form)))
`(mapcar (lambda (_) ,form) ,list))
(defmacro --map-loop (form list)
(declare (debug (form form)))
(let ((result-sym (make-symbol "result")))
`(let (,result-sym)
@skeeto
skeeto / coin-flip.el
Created July 30, 2012 14:55
Monte Carlo Elisp throwaways
;; Minimum Number Of Coin Tosses Such That Probability Of Getting 3
;; Consecutive Heads Is Greater Than 1/2
;; http://blog.eduflix.tv/2012/05/contest-answer-minimum-number-of-coin-tosses-such-that-probability-of-getting-3-consecutive-heads-is-greater-than-12/
(require 'cl)
(defun flip ()
"Flip a coin."
(if (< 0 (random)) 'H 'T))
@skeeto
skeeto / decimalize.el
Created November 30, 2011 01:20
emacs lisp exercise: latitude-longitude-decimalize
;; http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-latitude-longitude.html
(defun latitude-longitude-decimalize (str)
(destructuring-bind (lath latm lats latd lonh lonm lons lond)
(split-string str "[^0-9.NEWS]+" t)
(vector (latlon-helper latd lath latm lats)
(latlon-helper lond lonh lonm lons))))
(defun latlon-helper (d &rest hms)
(* (if (or (equal d "N") (equal d "E")) 1 -1)
@skeeto
skeeto / Makefile
Last active January 11, 2018 23:04
r/dailyprogrammer #346 : Cryptarithmetic Solver
CFLAGS = -std=c99 -Wall -Wextra -O3 -g3
solve: solve.c
clean:
rm -rf solve
@skeeto
skeeto / Makefile
Created January 17, 2018 20:16
r/dailyprogrammer Challenge #347 : Linear Feedback Shift Register
CFLAGS = -std=c99 -Wall -Wextra -O3 -g3
lfsr: lfsr.c
@skeeto
skeeto / pi.c
Created May 31, 2018 00:46
Estimate pi by shooting arrows at a shield
/* Estimate pi by shooting arrows at a shield.
* https://www.smbc-comics.com/comic/math-and-war
* Only uses integer operations except for displaying the quotient.
*/
#include <time.h>
#include <stdio.h>
#include <inttypes.h>
#define HALF UINT64_C(0x80000000)
#define QUARTER UINT64_C(0x40000000)
@skeeto
skeeto / hash.c
Last active August 11, 2018 02:08
MurmurHash3 vs. hashCode
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
unsigned
hashCode(const unsigned short *s, size_t len)
{
unsigned hash = 0;
for (size_t i = 0; i < len; i++)
hash = 31 * hash + s[i];
@skeeto
skeeto / gridsolve.c
Created August 14, 2018 13:57
Grid game solver
/* https://redd.it/96wper */
#include <stdio.h>
#include <stdlib.h>
static const unsigned long factors[] = {
1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL,
10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL,
100000000000ULL, 1000000000000ULL, 10000000000000ULL, 100000000000000ULL,
1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL
};
@skeeto
skeeto / utf7-example.c
Created August 8, 2018 01:23
UTF-7 example
/* https://github.com/skeeto/utf-7/issues/1
*/
#include <stdio.h>
#include "utf7.h"
static unsigned long
rand32(unsigned long s[1])
{
unsigned long x = (*s += 0xd6abc211ul) & 0xfffffffful;
x ^= x >> 16;
@skeeto
skeeto / perlin.m
Created May 8, 2012 02:20
Octave Perlin noise function
## Returns the Perlin noise value for the given point. Accepts 2D
## arrays for x and y, i.e. from meshgrid(). For example,
##
## [x y] = meshgrid(0:0.1:10, 0:0.1:10);
## d = perlin(x, y);
##
## The special thing here is it's entirely vectorized: no loops!
function v = perlin(x, y)
## Find the nearest grid points (2D)
x0 = floor(x);