<commit_before>(ns gtfve.data.queries
  (:require [datomic.api :as d]
            [gtfve.data.connection :refer [conn uri feed]]))

(defn pull
  "Use the Datomic Pull API to query the database."
  [spec e]
  (let [db (d/db conn)]
    (d/pull db spec e)))

(defn touch-all
  "Touches entity e and all other reachable Entities of e"
  [e]
  (cond
    (instance? datomic.query.EntityMap e)
    (reduce (fn [m [k v]]
              (assoc m k (touch-all v)))
            {}
            (d/touch e))
    (set? e) (into #{} (map touch-all e))
    :else e))

(defn stops-search
  [name]
  (let [db (d/db conn)]
    (->> (d/datoms db :aevt :stop/name)
         (filter #(re-find (re-pattern (str "(?i)" name)) (:v %)))
         (map :e)
         (map (partial d/entity db))
         (map d/touch))))

(defn stops-search
  ([name]
   (stops-search name [:stop/id :stop/name]))
  ([name p]
   (let [db (d/db conn)
         pattern (re-pattern (str "(?i)" name))
         match? #(not (nil? (re-find pattern %)))]
     (d/q '[:find [(pull ?s p) ...]<commit_msg>Add bounded functions to queries<commit_after>(ns gtfve.data.queries
  (:require [datomic.api :as d]
            [gtfve.data.connection :refer [conn uri feed]]))

(defn bounded?
  ([[x1 y1 x2 y2]]
   (fn [[px py]]
     (and (> px x1) (> py y1)
          (< px x2) (< px y2))))
  ([[x1 y1 x2 y2] [px py]]
   (and (> px x1) (> py y1)
        (< px x2) (< px y2))))

(defn pull
  "Use the Datomic Pull API to query the database."
  [spec e]
  (let [db (d/db conn)]
    (d/pull db spec e)))

(defn touch-all
  "Touches entity e and all other reachable Entities of e"
  [e]
  (cond
    (instance? datomic.query.EntityMap e)
    (reduce (fn [m [k v]]
              (assoc m k (touch-all v)))
            {}
            (d/touch e))
    (set? e) (into #{} (map touch-all e))
    :else e))

(defn bounded-stops
  [bbox]
  (let [db (d/db conn)]
    (->> (d/datoms db :aevt :stop/id)
         (map :e)
         (map (partial d/entity db))
         (map (fn [e] (into [e]
                            ((juxt :stop/latitude :stop/longitude) e))))
         (filter (fn [[e & point]]
                   (bounded? bbox point)))
         (map first)
         (map d/touch))))

(defn viewport
  [bbox]
  (bounded-stops bbox))

(defn stops-search
  ([name]
   (stops-search name [:stop/id :stop/name]))
  ([name p]
   (let [db (d/db conn)
         pattern (re-pattern (str "(?i)" name))
         match? #(not (nil? (re-find pattern %)))]
     (d/q '[:find [(pull ?s p) ...]
{'fraction_non_alphanumeric': 0.18298042349180982, 'fraction_numerical': 0.006392329204954055, 'mean_word_length': 2.0499390986601704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2111259', 'n_tokens_mistral': 978, 'n_tokens_neox': 967, 'n_words': 299}