Last active
December 30, 2015 11:09
-
-
Save nicokosi/7820162 to your computer and use it in GitHub Desktop.
Code from Zenika session for Clojure beginners ("NightClazz" session, 2013/12/05).
This is just a backup, see repo https://github.com/nicokosi/clojure-nightclazz.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns nightclazz.gameOfLife | |
| (:require [quil.core :as q])) | |
| ;; generation: | |
| (def world (atom | |
| #{[0 1] [1 2] [1 1] [1 3]})) | |
| (defn setup [] | |
| (q/smooth) | |
| (q/background 30)) | |
| (defn draw [] | |
| (q/background (q/color 0 0 255)) | |
| (q/stroke-weight 0) | |
| (q/fill (q/color 255 255 0)) | |
| (doseq [[x y] (deref world)] | |
| (let [diam 20 | |
| x (+ 100 (* x 20)) | |
| y (+ 100 (* y 20))] | |
| (q/ellipse x y diam diam)))) | |
| (q/defsketch example ;;Define a new sketch named example | |
| :title "game of life" ;;Set the title of the sketch | |
| :setup setup ;;Specify the setup fn | |
| :draw draw ;;Specify the draw fn | |
| :size [323 200]) ;;You struggle to beat the golden ratio | |
| (swap! world step) | |
| (defn neighbours | |
| "calcule les 8 voisines" ; doc | |
| [[x y]] | |
| {:post [(= (count %) 8)]} ; assertion sur le résultat | |
| (for [i (range (dec x) (+ x 2)) ;; for => produit cartésien | |
| j (range (dec y) (+ y 2)) | |
| :when (not (and (= x i) (= y j)))] ; filtre la cellule courante | |
| [i j]) | |
| ) | |
| (defn step [cells] ; fonction pure qu'on peut appeller en "explorant" dans le REPL | |
| ; ou pour connaitre la 1000 ème génération | |
| (let [freqs (frequencies | |
| (mapcat neighbours @world) ; flatmap | |
| )] | |
| (set (keys (filter (fn [[cell n]] | |
| (if (contains? cells cell | |
| (<= 2 n 3) | |
| (= n 3)) | |
| ) | |
| ) freqs))))) | |
| (while true?(swap! world step) | |
| (Thread/sleep 100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defproject clojure-nightclazz "0.1.0-SNAPSHOT" | |
| :description "Code from Zenika session for Clojure beginners (\"NightClazz\" session, 2013/12/05)." | |
| :url "https://github.com/nicokosi/clojure-nightclazz" | |
| :dependencies [ | |
| [org.clojure/clojure "1.5.1"] | |
| [quil "1.6.0"] | |
| ;; deuxième session | |
| [ring "1.2.1"]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns web | |
| (:require [ring.adapter.jetty :as jetty] | |
| [ring.middleware.params :as params] | |
| [clojure.edn :as edn] | |
| [core :as core])) | |
| ;;(defonce lineages (atom [])) | |
| (defonce lineages (atom [[0 0] [1 1] [0 1] [1 0]])) | |
| (def create-gen | |
| (params/wrap-params | |
| (fn [req] | |
| (let [cells (-> req :params (get "cells") edn/read-string) | |
| lineage (iterate core/step cells) | |
| lineages (swap! lineages conj lineage) | |
| n (dec (count lineages))] | |
| {:status 201 | |
| :headers {"Location" (str "/" n "/0")}})))) | |
| (defn app [req] | |
| (let [uri (:uri req)] | |
| (if (= "/create" uri) | |
| (create-gen req) | |
| (or | |
| (when-let [[_ id n] (re-matches #"/(\d+)/(\d+)" uri)] | |
| (let [id (Long/parseLong id) | |
| n (Long/parseLong n)] | |
| (when-let [cells (some-> @lineages (nth id) (nth n))] | |
| {:status 200 | |
| :headers {"Content-Type" "text/plain"} | |
| :body (str (pr-str {:cells cells :next (str "/" id "/" (inc n))}))}))) | |
| {:status 404 | |
| :headers {"Content-Type" "text/plain"} | |
| :body "Pas trouvé"})))) | |
| (defonce server (jetty/run-jetty #'app | |
| {:port 8080 | |
| :join? false})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment