Skip to content

Instantly share code, notes, and snippets.

View jeremyheiler's full-sized avatar

Jeremy Heiler jeremyheiler

View GitHub Profile
package main
import "code.google.com/p/go-tour/tree"
import "fmt"
func Walk2(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk2(t.Left, ch)
}
ch <- t.Value
@jeremyheiler
jeremyheiler / get-line.clj
Last active December 19, 2015 10:28
Read a line that ends with a CRLF.
(defn get-line
[reader]
(loop [last-cr? false line (StringBuilder.)]
(let [i (.read reader)]
(if (neg? i)
(str line)
(let [c (char i)]
(if (and last-cr? (= c \newline))
(.substring line 0 (dec (.length line)))
(recur (= c \return) (.append line c))))))))
@jeremyheiler
jeremyheiler / helo.clj
Last active December 18, 2015 23:49
Options...
;; Straighforward
(defn handle-helo
[session command]
[(-> session
(reset-session)
(assoc :client-host (:domain command)))
{:code 250 :text (:server-host session)}])
;; State monad style
;; Not sure if command needs to be closed over, though.
(require '[paprika.core] :as adn)
;; Assume t is your access token.
(def at {:access-token t})
;; The simple case allows the first argument to be a string.
(adn/create-post "This is a test" at)
;; For every other case, pass in a map.
(adn/create-post {:text "This is a test" :reply-to "123"} at)
(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]))
@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"
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
(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"}
(defmacro thing
[id & body]
`(do
(foo ~id)
(let [result# (do ~@body)]
(bar ~id)
result#)))
(defmacro sum
[nums]
@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.