|
1 | 1 | (in-ns 'basilisp.core)
|
2 | 2 |
|
| 3 | +(import* abc |
| 4 | + atexit |
| 5 | + collections |
| 6 | + decimal |
| 7 | + fractions |
| 8 | + functools |
| 9 | + multiprocessing |
| 10 | + os |
| 11 | + pathlib |
| 12 | + [queue :as py-queue] |
| 13 | + random |
| 14 | + re |
| 15 | + threading |
| 16 | + [time :as py-time] |
| 17 | + uuid) |
| 18 | + |
| 19 | +(import* attr) |
| 20 | + |
3 | 21 | (def ^{:doc "Create a list from the arguments."
|
4 | 22 | :arglists '([& args])}
|
5 | 23 | list
|
|
928 | 946 | ;; Futures ;;
|
929 | 947 | ;;;;;;;;;;;;;
|
930 | 948 |
|
931 |
| -(import* atexit) |
932 |
| - |
933 | 949 | (def ^:dynamic *executor-pool*
|
934 | 950 | (basilisp.lang.futures/ThreadPoolExecutor))
|
935 | 951 |
|
|
1103 | 1119 | ;; Simple Predicates ;;
|
1104 | 1120 | ;;;;;;;;;;;;;;;;;;;;;;;
|
1105 | 1121 |
|
1106 |
| -(import* decimal fractions uuid) |
1107 |
| - |
1108 | 1122 | (defn any?
|
1109 | 1123 | "Return true for any x."
|
1110 | 1124 | [_]
|
|
1404 | 1418 | ;; Type Coercion ;;
|
1405 | 1419 | ;;;;;;;;;;;;;;;;;;;
|
1406 | 1420 |
|
| 1421 | +(defn cast |
| 1422 | + "Throws a `TypeError` if x is not a cls. Otherwise, return x." |
| 1423 | + [cls x] |
| 1424 | + (when-not (instance? cls x) |
| 1425 | + (throw (python/TypeError |
| 1426 | + (str "Cannot cast object of type " (python/type x) " to " cls)))) |
| 1427 | + x) |
| 1428 | + |
| 1429 | +(defn class |
| 1430 | + "Return the class of x." |
| 1431 | + [x] |
| 1432 | + (python/type x)) |
| 1433 | + |
| 1434 | +(defn type |
| 1435 | + "Return the type of x." |
| 1436 | + [x] |
| 1437 | + (python/type x)) |
| 1438 | + |
1407 | 1439 | (defn bigdec
|
1408 | 1440 | "Coerce x to a Decimal."
|
1409 | 1441 | [x]
|
|
3134 | 3166 | (mapcat walk (children node))))))]
|
3135 | 3167 | (walk root)))
|
3136 | 3168 |
|
3137 |
| -(import* multiprocessing) |
3138 |
| - |
3139 | 3169 | (def ^:dynamic *pmap-cpu-count*
|
3140 | 3170 | (* 2 (multiprocessing/cpu-count)))
|
3141 | 3171 |
|
|
3190 | 3220 | ;; Random Functions ;;
|
3191 | 3221 | ;;;;;;;;;;;;;;;;;;;;;;
|
3192 | 3222 |
|
3193 |
| -(import* random) |
3194 |
| - |
3195 | 3223 | (defn rand
|
3196 | 3224 | "Return a random real number between lower (default: 0) and upper (default: 1) inclusive."
|
3197 | 3225 | ([] (random/uniform 0 1))
|
|
3627 | 3655 | (finally
|
3628 | 3656 | (pop-thread-bindings)))))
|
3629 | 3657 |
|
3630 |
| -(import* [time :as py-time]) |
3631 |
| - |
3632 | 3658 | (defn ^:private perf-counter
|
3633 | 3659 | []
|
3634 | 3660 | (py-time/perf-counter))
|
|
3750 | 3776 | ;; String Functions ;;
|
3751 | 3777 | ;;;;;;;;;;;;;;;;;;;;;;
|
3752 | 3778 |
|
3753 |
| -(import* os) |
3754 |
| - |
3755 | 3779 | (defn format
|
3756 | 3780 | "Format a string as by Python's `%` operator."
|
3757 | 3781 | [fmt & args]
|
|
3781 | 3805 | ;; File Functions ;;
|
3782 | 3806 | ;;;;;;;;;;;;;;;;;;;;
|
3783 | 3807 |
|
3784 |
| -(import* pathlib) |
3785 |
| - |
3786 | 3808 | (defn file-seq
|
3787 | 3809 | "Return a seq of `pathlib.Path` objects for all files and subdirectories of `dir`."
|
3788 | 3810 | [dir]
|
|
4520 | 4542 | ;; Regex Functions ;;
|
4521 | 4543 | ;;;;;;;;;;;;;;;;;;;;;
|
4522 | 4544 |
|
4523 |
| -(import re) |
4524 |
| - |
4525 | 4545 | (defn re-pattern
|
4526 | 4546 | "Return a new re.Pattern instance."
|
4527 | 4547 | [s]
|
|
4577 | 4597 | ;; Multimethods ;;
|
4578 | 4598 | ;;;;;;;;;;;;;;;;;;
|
4579 | 4599 |
|
4580 |
| -(import basilisp.lang.multifn) |
| 4600 | +(import* basilisp.lang.multifn) |
4581 | 4601 |
|
4582 | 4602 | (defmacro defmulti
|
4583 | 4603 | "Define a new multimethod with the dispatch function."
|
|
5034 | 5054 | ;; Interfaces ;;
|
5035 | 5055 | ;;;;;;;;;;;;;;;;
|
5036 | 5056 |
|
5037 |
| -(import* abc) |
5038 |
| - |
5039 | 5057 | (defmulti munge
|
5040 | 5058 | "Munge the input value into a Python-safe string. Converts keywords and
|
5041 | 5059 | symbols into strings as by `name` prior to munging. Returns a string."
|
|
5174 | 5192 | ;; Protocols ;;
|
5175 | 5193 | ;;;;;;;;;;;;;;;
|
5176 | 5194 |
|
5177 |
| -(import* functools) |
5178 |
| - |
5179 | 5195 | (defn ^:private gen-protocol-dispatch
|
5180 | 5196 | "Return the dispatch function for a single protocol method."
|
5181 | 5197 | [protocol-name interface-name [method-name & args+docstring :as method-def]]
|
|
5656 | 5672 | ;; Records ;;
|
5657 | 5673 | ;;;;;;;;;;;;;
|
5658 | 5674 |
|
5659 |
| -(import* attr) |
5660 |
| - |
5661 | 5675 | (defn record?
|
5662 | 5676 | "Return true if v is a record type."
|
5663 | 5677 | [v]
|
|
5937 | 5951 | ;; Transducers ;;
|
5938 | 5952 | ;;;;;;;;;;;;;;;;;
|
5939 | 5953 |
|
5940 |
| -(import* collections) |
5941 |
| - |
5942 | 5954 | ;; Eduction types return a custom iterator type which continually pulls from the
|
5943 | 5955 | ;; input collection (cast as a `seq`). `xf` is called on each value of that
|
5944 | 5956 | ;; sequence and it stores eligible values in a queue (`buf`). `__next__` adds
|
|
6125 | 6137 | ([result] (rf result))
|
6126 | 6138 | ([result input]
|
6127 | 6139 | (reduce rf result input))))
|
| 6140 | + |
| 6141 | +;;;;;;;;;; |
| 6142 | +;; Taps ;; |
| 6143 | +;;;;;;;;;; |
| 6144 | + |
| 6145 | +(defonce ^:private tap-queue |
| 6146 | + (->> (os/getenv "BASILISP_TAP_QUEUE_SIZE" 1024) |
| 6147 | + (python/int) |
| 6148 | + (py-queue/Queue))) |
| 6149 | + |
| 6150 | +(defonce ^:private tapset (atom {})) |
| 6151 | + |
| 6152 | +(defonce ^:private tap-thread |
| 6153 | + (delay |
| 6154 | + (doto (threading/Thread |
| 6155 | + ** |
| 6156 | + :target (fn [] |
| 6157 | + (loop [] |
| 6158 | + (let [{:keys [topic val]} (.get tap-queue) |
| 6159 | + topic-tapset (get @tapset topic #{})] |
| 6160 | + (doseq [tap (seq topic-tapset)] |
| 6161 | + (try |
| 6162 | + (tap val) |
| 6163 | + (catch python/Exception _ nil))) |
| 6164 | + (.task-done tap-queue)) |
| 6165 | + (recur))) |
| 6166 | + :daemon true) |
| 6167 | + (.start)))) |
| 6168 | + |
| 6169 | +(defn add-tap |
| 6170 | + "Add `tf`, a function of one argument, to the tap set for the topic `topic`. If no |
| 6171 | + topic is given, the default topic is used. |
| 6172 | + |
| 6173 | + Tap functions are called only with values from calls to `tap>` with the topic they |
| 6174 | + are added with. |
| 6175 | + |
| 6176 | + Taps may be removed from the tap set for a topic (or with the default topic) by a |
| 6177 | + matching call to `remove-tap`. Taps are identified only by their identity, so hang |
| 6178 | + on to a reference if you do need to remove the tap. |
| 6179 | + |
| 6180 | + Returns `nil`." |
| 6181 | + ([tf] |
| 6182 | + (add-tap :basilisp.core.tap/default tf)) |
| 6183 | + ([topic tf] |
| 6184 | + (force tap-thread) |
| 6185 | + (swap! tapset |
| 6186 | + (fn [old-state] |
| 6187 | + (let [topic-tapset (get old-state topic #{})] |
| 6188 | + (->> (conj topic-tapset tf) |
| 6189 | + (assoc old-state topic))))) |
| 6190 | + nil)) |
| 6191 | + |
| 6192 | +(defn remove-tap |
| 6193 | + "Remove a tap function from the tap set added by `add-tap`. |
| 6194 | + |
| 6195 | + Tap functions may only be removed from the tap set corresponding to the topic they |
| 6196 | + were added with. If no topic is given, the default topic is used. |
| 6197 | + |
| 6198 | + Returns `nil` in all cases." |
| 6199 | + ([tf] |
| 6200 | + (remove-tap :basilisp.core.tap/default tf)) |
| 6201 | + ([topic tf] |
| 6202 | + (force tap-thread) |
| 6203 | + (swap! tapset |
| 6204 | + (fn [old-state] |
| 6205 | + (let [topic-tapset (get old-state topic #{})] |
| 6206 | + (->> (disj topic-tapset tf) |
| 6207 | + (assoc old-state topic))))) |
| 6208 | + nil)) |
| 6209 | + |
| 6210 | +(defn tap> |
| 6211 | + "Send the value `val` to all tap functions registered for the topic. If no topic |
| 6212 | + is given, the default topic is used. |
| 6213 | + |
| 6214 | + `tap>` will never block, though if the tap queue is full then tap values may be |
| 6215 | + dropped. |
| 6216 | + |
| 6217 | + Returns true if `val` was sent to the queue, `false` if `val` was dropped." |
| 6218 | + ([val] |
| 6219 | + (tap> :basilisp.core.tap/default val)) |
| 6220 | + ([topic val] |
| 6221 | + (force tap-thread) |
| 6222 | + (try |
| 6223 | + (do |
| 6224 | + (.put-nowait tap-queue {:topic topic :val val}) |
| 6225 | + true) |
| 6226 | + (catch queue/Full _ false)))) |
0 commit comments