extensions.hy 1.21 KB
Newer Older
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 65 66 67 68 69 70 71 72 73 74 75
(import re)
(import pickle)
(import os)
(require [hy.contrib.loop [loop]])
(import [collections [Counter]])



;;;;;;;;;;;;;;
;;; Macros ;;;
;;;;;;;;;;;;;;

(defmacro map-l [f coll]
  "Wraps a call to map in a call to list"
  `(list (map ~f ~coll)))


(defmacro rest-l [coll]
  "Wraps a call to rest in a call to list"
  `(list (rest ~coll)))


(defmacro comment [&rest stuff]
  None)


(defmacro λ [args body]
  `(fn ~args ~body))


;;;;;;;;;;;;;;;;;
;;; Functions ;;;
;;;;;;;;;;;;;;;;;


(defn list-equals [x y]
  (= (Counter x) (Counter y)))


(defn None? [obj]
  (is obj None))

(defn one? [n]
  (= n 1))

(defn merge [&rest maps]
  (->> maps
       (reduce (fn [d1 d2] (merge-with (fn [x y] y) d1 d2)))))


(defn file-exists? [filename]
  (.isfile (. os path) filename))


(defn regex-match? [regex-string s]
  "(regex-match? regex string)"
  (-> (.compile re regex-string)
      (.match s)
      (None?)
      (not)))


(defn slurp [filename]
  "Reads in a pickled object"
  (.load pickle (open filename "rb")))


(defn spit [obj filename]
  "Writes an object to a pickle file"
  (.dump pickle obj (open filename "wb")))


(defn print-> [obj]
  "Prints an object and then returns it for use in threading macros."
  (print obj) obj)