Skip to content

Instantly share code, notes, and snippets.

Christophe Grand cgrand

View GitHub Profile
View Flattening a nested list in clojure
(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)
View flatten-versions.clj
;;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]
View httpcore.clj
(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)
View secret.clj
(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))
View secret-µframework.clj
;; 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]
View demo.clj
;; 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
An IRC logger which serves its archives over HTTP
View irclog.clj
(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)))
View gist:126676
(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
View euler27.clj
; 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)]
View build-clj.xml
<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.">
You can’t perform that action at this time.