Skip to content

Instantly share code, notes, and snippets.

@guilespi
guilespi / memo.md
Created April 1, 2014 18:08 — forked from yokolet/memo.md
(defn validate-mappings
"Validates each mapped import is for an
existent field in the specified company"
[fields headers mappings]
(letfn [(found? [list element] (< (.indexOf list element) 0))]
(filter (fn [[header field]]
(or (found? headers header)
(found? fields field)))
mappings)))
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
(defmethod validation/valid-field? :document-MX
[{value :value}]
(let [match (re-find #"(?i)(^[a-z]{2})([a-z])([a-z])(\d{6})(H|M)([a-z]{2})([a-z]{3})(\d{2})$" value)]
match))
(defmethod validation/valid-field? :document-UY
[{value :value}]
(when-let [[_ number digit] (re-find #"^(\d{7})-(\d)$" value)]
(let [m [2 9 8 7 6 3 4]
c (map #(utils/str->int (str %)) number)
@guilespi
guilespi / get-NYSE-days.clj
Created October 29, 2012 02:05
QSTK Get days there was trading at the NYSE
(defn get-NYSE-days
"Create a set of timestamps between startday and endday
that correspond to the days there was trading at the NYSE"
[start-date end-date time-of-day]
(let [dates-file (str *QS* "/qstkutil/NYSE_dates.txt")
NYSE-dates (incanter.io/read-dataset dates-file)
fmt (formatter "MM/dd/yyyy")
dates (incanter.core/$map #(parse fmt %) :col0 NYSE-dates)]
(set (filter (fn [d] (within? (interval start-date end-date) d))
(map #(plus % time-of-day) dates)))))
@guilespi
guilespi / get-data.clj
Created October 29, 2012 02:25
Build a matrix from stock datasets
(defn select-value
"Given a dataset indexed by date, returns the value corresponding to a specified column
if existent for a specific date"
[ds column date]
(let [row (ds {:Date date})]
(when-not (nil? row) (incanter.core/$ 0 column row))))
(defn get-data
"Given a list of `symbols`, its data and a list of specific `timestamps`, builds a matrix(sequence)
with each column corresponding to a stock and the value extracted using `column`
@guilespi
guilespi / row-operation.clj
Created October 29, 2012 02:59
Iterating over a dataset to apply specific functions to each row
(defmacro apply-filtered
"Given two sequences, apply a function to each pair of elements when condition is met
anaphoras n and m exists for each indexed element
e.g. (apply-filtered / [1 2 3] [1 0 3] when (> m 0)) => (1 nil 1)
"
[op a b & condition]
`(for [x# (range (count ~a))]
(let [n# (nth ~a x#)
@guilespi
guilespi / run.clj
Created October 29, 2012 03:02
Running the QSTK tutorial in clojure
(defn run
[]
(let [symbols ["AAPL","GLD","GOOG","$SPX","XOM"]
start-day (date-time 2012 1 1)
end-day (date-time 2012 12 31)
time-of-day (hours 16)
timestamps (get-NYSE-days start-day end-day time-of-day)
symbols-data (read-symbols-data "Yahoo" symbols)
adj-close-data (incanter.core/to-dataset
(get-data timestamps symbols (keyword "Adj Close") symbols-data time-of-day))]
@guilespi
guilespi / questions by category.sql
Created October 31, 2012 01:42
How many language questions are in each difficulty category
DECLARE @tagname varchar(20) = ##language:string##
select DifficultyGroup, Count(1) Total,
avg(cast(ResponseTime as bigint)) as Average,
stdev(cast(ResponseTime as bigint)) as StandardDev
from
(SELECT
Questions.CreationDate,
Questions.Title,
Tags.TagName,
@guilespi
guilespi / response time by hour.sql
Created October 31, 2012 01:43
StackOverflow response times by language, grouped by hour
DECLARE @tagname varchar(20) = ##language:string##
select ResponseHour,
avg(cast(ResponseTime as bigint)) as Average,
stdev(cast(ResponseTime as bigint)) as StandardDev
from
(SELECT
Questions.CreationDate,
Questions.Title,