Archive

Posts Tagged ‘Clojure’

Build your Twitter API app in Clojure, Part 1

September 22nd, 2009

Building your own Twitter API application in Clojure is made real easy now thanks to clojure-twitter and clj-oauth libraries.

In this part, I will simply show you how easy it is to access the Twitter API using clojure-twitter. Here, I update my own Twitter status:

(require 'twitter ['oauth.client :as 'oauth])

(def consumer (oauth/make-consumer <consumer-token>
                                   <consumer-token-secret>
                                   "http://twitter.com/oauth/request_token"
                                   "http://twitter.com/oauth/access_token"
                                   "http://twitter.com/oauth/authorize"
                                   :hmac-sha1))

(def request-token (:oauth_token (oauth/request-token consumer)))

(oauth/user-approval-uri consumer
                         request-token
                         <callback-uri>)

(def access-token-response (oauth/access-token consumer
                                               request-token
                                               <verifier>))

(twitter/with-oauth consumer (:oauth_token access-token-response)
                             (:oauth_token_secret access-token-response)
  (twitter/update-status "#clojure is awesome"))


Making it all work:

  • Download the latest version of clojure-twitter
  • Download the latest version of clj-oauth
  • Add clojure-twitter.jar and dependents in the clojure-twitter/lib directory to your classpath
  • Next, you will need to register an application at Twitter here in order to obtain a Consumer token and token secret
  • Once you have register the application, launch REPL and replace <consumer-token> and <consumer-token-secret> in the above code snippet accordingly
  • Additionally, replace the <callback-uri> with the “Application Website” you defined in the application registration.
  • Once the user approval URI has been accessed and accepted, the verifier PIN code will be provide which needs to replace the <verifier>

In part 2, I will go through a more detailed, step-by-step example that demonstrates building a real-world Twitter API app in Clojure. Follow me on Twitter and I’ll keep you posted.

alen Clojure , ,

CS Research Topic Generator, in Clojure

September 14th, 2009

If you need a worthy topic for your CS research project, let this very simple CS topic generator spark some ideas.
http://www.cs.purdue.edu/homes/dec/essay.topic.generator.html

I’ve decided to get myself more familiarized with functional programming so I picked Clojure as my language of choice. It’s new, its a Lisp dialect, it has a great concurrency API and it runs on the already familiar JVM.

Here is my implementation of the “CS topic generator” in Clojure:

(ns com.alenribic.topic-generator
 (:use [clojure.contrib.str-utils :only (str-join)]
       [clojure.contrib.seq-utils :only (rand-elt)]))

(defn compute-article
  [word capitalize?]
  (let [vowels #{\a \e \i \o \u} a (if capitalize? \A \a)]
    (if (vowels (Character/toLowerCase (first word)))
      (str a \n) a)))

(defn gen-phrase
  [col1 col2 col3 first-phase?]
  (let [word1 (rand-elt col1) word2 (rand-elt col2) word3 (rand-elt col3)]
      (str-join " " [(compute-article word1 first-phase?) word1 word2 word3])))

(defn gen-random-topic []
  (let [col1 [
      "integrated", "parallel", "virtual", "interactive", "responsive",
      "synchronized", "balanced", "virtual", "meta-level", "optimized", "active", "parameterized",
      "conceptual", "scalable", "dynamic", "high-level", "collaborative", "type-safe"]
     col2 [
      "mobile", "functional", "programmable", "distributed", "logical",
      "digital", "concurrent", "knowledge-based", "multimedia", "binary", "object-oriented",
      "secure", "high-speed", "real-time", "functional", "parallelizing", "watermarking",
      "proxy"]
     col3 [
      "network", "preprocessor", "compiler", "system", "interface",
      "protocol", "architecture", "database", "algorithm", "toolkit", "display", "technology",
      "solution", "language", "agent", "theorem prover", "work cluster", "cache"]
     conn ["for", "related to", "derived from", "applied to", "embedded in"]]
    (str-join " " [(gen-phrase col1 col2 col3 true)
		   (rand-elt conn) (gen-phrase col1 col2 col3 false)])))

alen Clojure ,