Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
(defn flat?
"Returns true if seq contains no sequences"
[seq]
(not-any? (fn [x] (isa? (type x) java.util.List)) seq))
(defn flatten
"Returns an unnested sequence from the non-sequence elements of seq
for example, it turns (1 (2) 3) into (1 2 3)"
[seq]
(if (isa? (type seq) java.util.List)
;;from clojure.contrib.seq-utils
;;'flatten' written by Rich Hickey,
;;see http://groups.google.com/group/clojure/msg/385098fabfcaad9b
(defn flatten [x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))
;aprox. translate from paul graham's on lisp to clojure
(defn flatten-ol [x]
(letfn [(rec [x acc]
(ns org.apache.http.examples
"Basic HTTP Server.
A quick port of http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java to Clojure"
(:import (java.io File OutputStreamWriter InterruptedIOException IOException)
(java.net ServerSocket URLDecoder)
(java.util Locale)
(org.apache.http.protocol BasicHttpProcessor HttpContext BasicHttpContext HttpRequestHandler HttpRequestHandlerRegistry HttpService ResponseConnControl ResponseContent ResponseDate ResponseServer)
(org.apache.http ConnectionClosedException HttpEntity HttpEntityEnclosingRequest HttpException HttpRequest HttpResponse HttpServerConnection HttpStatus MethodNotSupportedException)
(org.apache.http.entity ContentProducer EntityTemplate FileEntity)
(org.apache.http.impl DefaultConnectionReuseStrategy DefaultHttpResponseFactory DefaultHttpServerConnection)
(app
(with-some-middleware1 arg arg arg)
(with-some-middleware2 arg arg arg)
["account" name] {:get (more code too)
:post (more code too)}
["files" & path] (really more code)
["app" &] (app ; je peux imbriquer les apps pour une meilleure réutilisation et pour pas coder les segmenst communs des url partout
["order" order-id] (display-order order-id)
["bill" order-id] (display-bill order-id))
;; here is roughly what I'd like to write:
; an app can simply wrap a handler
(app
(fn [req] {:status 200 :headers {"Content-Type" "text/html"}
:body "<h3>Hello World</h3>"}))
; an app can declare routes
(app
["hello" name]
;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; http://github.com/cgrand/moustache/tree/master
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
@cgrand
cgrand / irclog.clj
Created May 22, 2009 19:57
An IRC logger which serves its archives over HTTP
(ns irclog
(:use net.cgrand.moustache)
(:use net.cgrand.enlive-html)
(:require [ring.httpcore :as hc])
(:import (org.jibble.pircbot PircBot)
(org.joda.time DateTime)
(org.joda.time.format ISODateTimeFormat)))
(defn yyyy-MM-dd [d] (-> (ISODateTimeFormat/date) (.print d)))
(set! *warn-on-reflection* true)
(ns slow
(:import [java.nio ByteBuffer Buffer]))
(defn #^ints bytes-to-ints [#^bytes bs]
(let [#^ints is (make-array Integer/TYPE (alength bs))]
(loop [i (int 0)]
(if (< i (alength bs))
(do
; http://projecteuler.net/index.php?section=problems&id=27
(defn lazy-primes3 []
(letfn [(enqueue [sieve n step]
(let [m (+ n step)]
(if (sieve m)
(recur sieve m step)
(assoc sieve m step))))
(next-sieve [sieve candidate]
(if-let [step (sieve candidate)]
<project name="clojuredev" default="compile-clojure" xmlns:mvn="urn:maven-artifact-ant">
<description>
</description>
<property name="build" location="bin"/>
<property name="src" location="src"/>
<target name="compile-clojure"
description="Compile Clojure sources.">