Wishful Coding

Didn't you ever wish your
computer understood you?

The age of APIs ... and virtual ghost towns?

Nowadays almost any self-respecting site seems to have an API. What can you do with all this data?

  • Clients
  • Analytics
  • Aggregation
  • And... a clone?  Sure!

I was thinking it'd be fun to do a Reddit clone, and experiment with different voting systems. Point is, there are quite a few of them, so how am I going to get users? Answer: I don't need users!

I took this and this, and in a few hours(mostly spent updating the work of Lau), I had an autonomous Reddit clone running of the Twitter API, and I believe it to be possible to create a complete social network that runs without any user input.

The age of APIs ... and virtual ghost towns?

How much Clojure do you know?

How much Clojure do you know?

user=> (count (ns-publics 'clojure.core))
546

That are a lot of functions. Unless you wrote half of them I wager you don’t know them all. Don’t worry, neither do I. But it doesn’t hurt to learn some more. That is why I wrote a Clojure quiz.

Simply do the standard stuff(make sure you have git and lein or cake)

git clone git://github.com/pepijndevos/clojure-quiz.git
cake deps
cake repl
(use 'clojure-quiz.core)

Now you can play three types of quizzes.

  • (doc-quiz) Find the correct doc string for the given function.
  • (name-quiz-easy) Find the correct function for the given doc.
  • (name-quiz) Same as above, but without multiple choice.

(name-quiz) is by far the hardest, and I generally score around 70% while I can usually get 100% on the others.

You can tweak the quiz by using binding to set the namespace to use and the number of options to give:

(binding [target 'clojure.set number 5] (doc-quiz))

Have fun!

Published on

Crowd sourced news with Clojure

This snippet gets tweets, filters their links, sorts, resolves and counts them in about 30 lines Clojure, showing off the power of its Java interoperability and concurrent data structures.

(ns news
  (:refer-clojure :exclude [resolve])
  (:use clojure.contrib.json)
  (:import [java.io BufferedReader InputStreamReader]
           [java.net URL]))

(def cred "") ; username:password

(def json (let [con (.openConnection (URL. "http://stream.twitter.com/1/statuses/sample.json"))]
            (.setRequestProperty con "Authorization" (str "Basic "
                                                          (.encode (sun.misc.BASE64Encoder.) (.getBytes cred))))
            (BufferedReader. (InputStreamReader. (.getInputStream con)))))

(def urls (agent (list)))

(def resolve (memoize (fn [url]
  (try
    (let [con (doto (.openConnection (URL. url))
                (.setInstanceFollowRedirects false)
                (.connect))
          loc (.getHeaderField con "Location")]
      (.close (.getInputStream con))
      (if loc loc url))
    (catch Exception _ url)))))

(defn top [urls]
  (reduce #(if (> (val %1) (val %2))
             %1 %2)
          (frequencies @urls)))

(future (doseq [tweet (repeatedly #(read-json json))
                url (:urls (:entities tweet))]
          (send-off urls #(conj % (resolve (:url url))))))

And this is just the start. I want to see if I can use Aleph and ClojureQL to kickstart my own little news service with it.