Skip to content

Instantly share code, notes, and snippets.

@Higgs1
Higgs1 / gist:0a08c98af77a2cfd7b95
Created April 1, 2015 05:55
CoffeeScript "next" function (Python-like)
next = (x) ->
x.next().value
@Higgs1
Higgs1 / gist:3e2e4c0a3e1d547b5bd5
Created April 1, 2015 05:49
CoffeeScript "iter" function (Python-like)
iter = (x) ->
yield from x
@Higgs1
Higgs1 / gist:cebedef2e775ea63fcbc
Created March 27, 2015 05:59
CoffeeScript CRC-32
crc32 = do ->
table =
for n in [0..255]
for [0..7]
if n & 1
n = 0xEDB88320 ^ n >>> 1
else
n >>>= 1
n
(str, crc = -1) ->
@Higgs1
Higgs1 / gist:d828714f8484390529cf
Last active August 29, 2015 14:17
CoffeeScript String ⇔ Hex
str2hex = do ->
hex = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
hex = (hex[x >> 4] + hex[x & 15] for x in [0..255])
(str) ->
(hex[c.charCodeAt()] for c in str).join ''
hex2str = (hex) ->
(String.fromCharCode parseInt hex[i...i + 2], 16 for i in [0...hex.length] by 2).join ''
@Higgs1
Higgs1 / gist:08ec61fbb250c1c92151
Last active August 29, 2015 14:17
CoffeeScript Simple MD5
# Array sum helper function.
sum = (array) ->
array.reduce (x, y) -> x + y
md5 = do ->
# Per-round shift amounts.
s = [738695, 669989, 770404, 703814]
s = (s[i >> 4] >> i % 4 * 5 & 31 for i in [0..63])
# Constants cache generated by sine.
@Higgs1
Higgs1 / gist:24691a16f5cf98bb58a2
Last active August 29, 2015 14:17
CoffeeScript Bitwise Left Rotate
rotl = (a, b) ->
a << b | a >>> 32 - b
@Higgs1
Higgs1 / gist:e49068823a8779a83dd6
Created March 20, 2015 22:04
CoffeeScript Sum Function
sum = (arr, start = 0) ->
arr.reduce (x, y) ->
x + y
, start
@Higgs1
Higgs1 / gist:0efe62a05fa12d3333dc
Last active August 29, 2015 14:17
CoffeeScript UTF8 Codec
# Converts UCS2 to UTF8
utf8_encode = (s) ->
unescape encodeURIComponent s
# Converts UTF8 to UCS2
utf8_decode = (s) ->
decodeURIComponent escape s
@Higgs1
Higgs1 / gist:f2a53a5e77a902d1e00e
Last active August 29, 2015 14:17
CoffeeScript RGB ⇔ HSV
# Returns [hue, sat, value] when given red, green, blue. All 6 values are between 0...1.
rgb2hsv = (r, g, b) ->
v = Math.max r, g, b
x = v - Math.min r, g, b
[
( if x is 0
0
else if v is r
(g - b) / x
else if v is g
@Higgs1
Higgs1 / gist:16c8447fcbcf39d6f7df
Last active August 29, 2015 14:17
Javascript RGB ⇔ HSV
// Returns [hue, sat, val]. All 6 values are between 0...1.
function rgb2hsv(r, g, b) {
var v = Math.max(r, g, b),
x = v - Math.min(r, g, b);
return [
( x == 0 ? 0 :
v == g ? (b - r) / x + 2 :
v == b ? (r - g) / x + 4 :
(g - b) / x + 6