This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (def items | |
| [{:id 1 :parent_id nil :code "ONE"} | |
| {:id 2 :parent_id 3 :code "TWO"} | |
| {:id 3 :parent_id nil :code "THREE"} | |
| {:id 4 :parent_id nil :code "FOUR"}]) | |
| (def my-items [1 2]) | |
| (def result (magic-fn items my-items)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (use '[clojure.contrib.seq-utils :only [find-first]]) | |
| (def items [{:id 100} {:id 200} {:id 2}]) | |
| (find-first #(= (:id %) 200) items) | |
| (some #(and (= (:id %) 200) %) items) | |
| (first (filter #(= (:id %) 200) items)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; the function i want to mock, | |
| ; in a namespace called "other" | |
| (defn do-something [q callback] | |
| (doseq [n [1 2 3]] (callback n))) | |
| ; the function i want to test | |
| (defn top [] | |
| (other/do-something "q-arg" (fn [item] | |
| (prn item)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "rubygems" | |
| require "open-uri" | |
| require "rack/utils" | |
| require "json" | |
| class Fql | |
| class << self | |
| attr_accessor :base_url | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro with-query-results [query form] | |
| (sql/with-connection db/conn | |
| (sql/with-query-results rs query form))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmulti select-ux class) | |
| (defmethod select-ux String [s] (str "String => " s)) | |
| (prn (select-ux "hello")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; inspired by ActiveRecord's find_in_batches | |
| ; and http://asymmetrical-view.com/2010/10/14/clojure-and-large-result-sets.html | |
| (def *default-fetch-size* 50) | |
| (defn with-query-results-cursor [[sql & params :as sql-params] func] | |
| (transaction | |
| (with-open [stmt (.prepareStatement (connection) sql)] | |
| (doseq [[index value] (map vector (iterate inc 1) params)] | |
| (.setObject stmt index value)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| select l.name lname, p.name pname, r.place_type rtype, r.name rname | |
| from locations l | |
| left join geoplanet_places p on p.woeid = l.woeid | |
| left join geoplanet_places r on | |
| ( | |
| (p.ancestry LIKE concat('%', '/', r.woeid, '/', '%')) | |
| AND r.language = "ENG" AND r.place_type = "State") | |
| where p.woeid = 2397796; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Eventable | |
| def events | |
| @events ||= {} | |
| end | |
| def trigger event, *args, &block | |
| if block_given? | |
| self.trigger :"before_#{event}", self | |
| result = yield | |
| self.trigger :"after_#{event}", self | |
| result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn hotels [] | |
| (-> | |
| (sql/outer-join | |
| (sql/project (sql/table {:hc_en_hotels :cooked}) [:id :hotel_id]) | |
| (sql/project (sql/table :hotels) [:name]) | |
| :left | |
| (sql/where (= :hotels.id :cooked.hotel_id))) | |
| (sql/outer-join | |
| (sql/project (sql/table :locations) [[:name :as :location_name]]) | |
| :left |