init.fnl (1988B)
1 (fn merge [table1 table2] 2 (collect [k v (pairs table2) &into table1] 3 k v)) 4 5 (fn dup [table] 6 (collect [k v (pairs table)] k v)) 7 8 (fn split [sep string] 9 (icollect [v (string.gmatch string (.. "([^" sep "]+)"))] 10 v)) 11 12 (fn file-exists? [name] 13 (match (io.open name :r) 14 f (do (f:close) true) 15 _ false)) 16 17 (fn system [s] 18 (match (os.execute s) 19 res (do (print (.. "Executed \"" s "\", exit code " (tostring res))) res) 20 (nil err) (error (.. "Error executing \"" s "\" (" err ")")))) 21 22 (fn hash [str] 23 (accumulate [h 5381 24 c (str:gmatch ".")] 25 (+ (* h 33) (string.byte c)))) 26 27 28 (local 29 base64-indices 30 (doto [ 31 "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" 32 "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "a" "b" "c" "d" "e" "f" 33 "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" 34 "w" "x" "y" "z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "-" "_" 35 ] 36 (tset 0 "A"))) 37 38 ;; local function base64(s) 39 ;; local byte, rep = string.byte, string.rep 40 ;; local pad = 2 - ((#s-1) % 3) 41 ;; s = (s..rep('\0', pad)):gsub("...", function(cs) 42 ;; local a, b, c = byte(cs, 1, 3) 43 ;; return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2|c>>6] .. bs[c&63] 44 ;; end) 45 ;; return s:sub(1, #s-pad) .. rep('=', pad) 46 ;; end 47 48 (fn base64url [s] 49 "URL-safe Base64-encoded form of s (no trailing padding)" 50 (let [pad (- 2 (% (- (# s) 1) 3)) 51 bs base64-indices 52 blank (string.rep "\0" pad) 53 s (-> (.. s blank) 54 (: :gsub 55 "..." 56 (fn [cs] 57 (let [(a b c) (string.byte cs 1 3)] 58 (.. (. bs (rshift a 2)) 59 (. bs (bor (lshift (band a 3) 4) (rshift b 4))) 60 (. bs (bor (lshift (band b 15) 2) (rshift c 6))) 61 (. bs (band c 63)))))))] 62 (s:sub 1 (- (# s) pad)))) 63 64 65 { : merge : split : file-exists? : system : hash : base64url : dup }