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