-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.clj
30 lines (26 loc) · 1.02 KB
/
util.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
(ns molecula.util)
(defn zipseq
"Returns a lazy sequence of partitions of interleaved colls.
Given `c1, c2, ..., cn` of the same length returns:
```
'(((first c1) (first c2) ... (first cn))
((second c1) (second c2) ... (second cn))
...
((last c1) (last c2) ... (last cn)))
```
If lengths are different then the returned seq will have the length of the shortest collection (like with ) return upto the length of the shortest collection and the tails of other collection discarded. There is no padding like with `partition` for example."
[& colls]
(partition (count colls) (apply interleave colls)))
(defn zipvec
"Like zipseq but returns all vectors."
[& colls]
(vec (map vec (partition (count colls) (apply interleave colls)))))
(defn with-timeout-fn
[t-ms f]
(let [fut (future (f))
ret (deref fut t-ms ::operation-timed-out)]
(when (= ret ::operation-timed-out)
(future-cancel fut))
ret))
(defmacro with-timeout [t-ms & body]
`(with-timeout-fn ~t-ms (fn [] ~@body)))