-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.clj
64 lines (52 loc) · 1.12 KB
/
bytes.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns clj-halodb.bytes
(:require [clojure.spec.alpha :as spec]))
(spec/fdef
string->bytes
:args (spec/cat :s string?)
:ret bytes?)
(defn string->bytes [s]
(-> s
(.getBytes)))
(spec/fdef
bytes->string
:args (spec/cat :b bytes?)
:ret string?)
(defn bytes->string [b]
(String. b))
(spec/fdef
keyword->bytes
:args (spec/cat :k keyword?)
:ret bytes?)
(defn keyword->bytes [k]
(let [namespace (namespace k)
name (name k)]
(-> (if namespace
(str namespace "/" name)
name)
(string->bytes))))
(spec/fdef
->bytes
:ret bytes?)
(defn ->bytes [x]
(cond
(string? x) (string->bytes x)
(keyword? x) (keyword->bytes x)
:else (-> (str x) (string->bytes))))
(comment
(-> (string->bytes "string")
(bytes->string))
(-> (keyword->bytes ::keyword)
(bytes->string))
(-> (keyword->bytes :keyword)
(bytes->string))
(-> (->bytes 1)
(bytes->string))
(-> (->bytes 1.1)
(bytes->string))
(-> (->bytes "abc")
(bytes->string))
(-> (->bytes ::keyword)
(bytes->string))
(-> (->bytes :keyword)
(bytes->string))
)