Skip to content

Instantly share code, notes, and snippets.

View jeremyheiler's full-sized avatar

Jeremy Heiler jeremyheiler

View GitHub Profile
@jeremyheiler
jeremyheiler / euler_1.clj
Created January 26, 2011 17:44
Project Euler Problem 1 in Clojure
(defn pe1 [max]
(reduce +
(union
(set (range 0 max 3))
(set (range 0 max 5)))))
@Entity
public class Foo{
@Id
public int id;
}
public ItemProcessor extends IterativeProcessor<String> implements Processor {
@Override
public List<String> getItems(Request req) {
return req.getParameters("items");
}
@Override
public void processItem(Request req, String item) {
Database.saveItem(item);
@jeremyheiler
jeremyheiler / smtp.clj
Last active December 14, 2015 03:38
Possible ways to script SMTP with Clojure.
;; A data-oriented approach.
;; Basic SMTP. Falls back to HELO if Extended SMTP isn't supported.
(def script
[[:CONNECT ["server.example.com" 587]]
[:EHLO ["client.example.com"] :on-reply {502 [:HELO ["client.example.com"]]}]
[:MAIL ["foo@example.com"]]
[:RCPT ["bar@example.com"]]
[:RCPT ["baz@example.com"]]
@jeremyheiler
jeremyheiler / smtp-client.clj
Last active December 14, 2015 06:39
Thoughts on configuring an SMTP client.
(require '(lime [smtp :as smtp]))
;; You can define the client yourself.
(def send-mail
(-> (smtp/base-smtp "smtp.gmail.com" 587)
(smtp/with-auth :PLAIN "username" "password")
(smtp/with-starttls)))
;; Perhaps have "batteries included" client that you configure with a map.
(defmacro thing
[id & body]
`(do
(foo ~id)
(let [result# (do ~@body)]
(bar ~id)
result#)))
(defmacro sum
[nums]
(let [d [["a" " 1 "] ["b " "2"] ["c" " 3"]]]
(reduce (partial apply assoc) {} (map (partial map clojure.string/trim) d)))
;=> {"c" "3", "b" "2", "a" "1"}
user> `inc
clojure.core/inc
user> 'inc
inc
user> (defmacro inc1 [n] `(inc ~n))
#'user/inc1
user> (inc1 3)
4
user> (defmacro inc2 [n] (list 'inc n))
#'user/inc2
@jeremyheiler
jeremyheiler / fizz-buzz-fibonacci.clj
Created May 16, 2013 19:31
Fizz Buzz Fibonacci!
(defn fibonacci
([] (fibonacci 1 1))
([x y] (cons x (lazy-seq (fibonacci y (+ x y))))))
(defn fizz-buzz
[n]
(let [fizz (zero? (mod n 3)) buzz (zero? (mod n 5))]
(cond
(and fizz buzz) "FizzBuzz"
fizz "Fizz"
(require '[paprika.core :as adn])
;; This returns a sequence your of vectors, one for each of the people you follow.
;; Each vector contains the user's username and the date of their last post.
;; The sequence is in ascending order by the date of their last post
;; Careful, though. The lookup is N+1.
(sort-by first
(map (juxt :created-at #(get-in % [:user :username]))