Skip to content

Instantly share code, notes, and snippets.

@bobthemighty
Last active August 29, 2015 13:59
Show Gist options
  • Save bobthemighty/10531889 to your computer and use it in GitHub Desktop.
Save bobthemighty/10531889 to your computer and use it in GitHub Desktop.
Funky num
module FunkyInt64 =
(* This is Thomas Wang's int64 hash function *)
let hash (k:int64) =
let k = uint64 k
let k = k |> (~~~) |> ((+) (k <<< 21))
let k = (k ^^^ (k >>> 24))
let k = (k + (k <<< 3 )) + (k <<< 8)
let k = k ^^^ (k >>> 14)
let k = k + (k <<< 2) + ( k <<< 4 )
let k = k ^^^ ( k >>> 28 )
k + (k <<< 31 )
let baseConvert (chars:string) n =
let b = chars.Length - 1 |> uint64
let rec digits (k:uint64) = seq {
if(k <> 0uL) then
let remainder = k % b |> int |> abs
yield chars.[remainder]
yield! digits ( k / b )
}
new System.String(digits n |> Seq.toArray |> Array.rev)
(* We convert to an arbitrary base, using whatever chars we want to represent digits.
We purposefully skip f c k t and x so we avoid uris like xxx_fuck_shit :D
*)
let funkify = hash >> baseConvert "abdeghijklmnoqrsuvwyz_-"
module FunkyInt32 =
let hash (k:int) =
let hash = uint32 k;
let hash = hash + ~~~(hash <<<15);
let hash = hash ^^^ (hash >>> 10);
let hash = hash + (hash <<< 3);
let hash = hash ^^^ (hash >>> 6);
let hash = hash + ~~~(hash <<< 11);
let hash = hash ^^^ (hash >>> 16);
(int32)hash;
let baseConvert (chars:string) n =
let b = chars.Length - 1
let rec digits (k:int) = seq {
if(k <> 0) then
let remainder = k % b |> int |> abs
yield chars.[remainder]
yield! digits ( k / b )
}
new System.String(digits n |> Seq.toArray |> Array.rev)
let funkify = hash >> baseConvert "abdeghijklmnopqrtuvwyz_-"
printfn "Testing int32"
[1; 59; 2183475; 123087; 01934; 3736472; 103851893; 2819471; 1263; 1837461; 37437; 10476] |> Seq.map (fun i -> i, FunkyInt32.funkify i) |> Seq.iter (fun (i, s) -> printfn "%d = %s" i s)
printfn "Testing int64"
[1L; 59L; 2183475L; 123087L; 01934L; 3736472L; 103851893L; 2819471L; 1263L; 1837461L; 37437L; 10476L] |> Seq.map (fun i -> i, FunkyInt64.funkify i) |> Seq.iter (fun (i, s) -> printfn "%d = %s" i s)
Testing int32
1 = ngkekhu
59 = oioqdli
2183475 = pyvwgdn
123087 = dezzvug
1934 = lpzvkue
3736472 = tkuhzv
103851893 = evqqgk_
2819471 = igejead
1263 = debdzah
1837461 = j_yt_zy
37437 = lptdwdb
10476 = lubnzto
Testing int64
1 = bbknosdbymkjmdw
59 = mdlanhaugjiamz
2183475 = egjlahkredhnmh
123087 = bqwh_nhdoodmlwl
1934 = bky_yinmvhknziy
3736472 = blmvhdgnazabnib
103851893 = djozssngwahekgq
2819471 = drhrdojdjzaw_gv
1263 = brve_kszbvk_iu
1837461 = bo_miongarragde
37437 = bswobbk_hhjy_hq
10476 = dszhykyruq_wyek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment