Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit d2537e5ceaf9a4ba4c9dba515f1b8edf4b1bcb95 @danielstockton committed Oct 9, 2011
Showing with 59 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +13 −0 README
  3. +5 −0 project.clj
  4. BIN src/evolve/.core.clj.swp
  5. +29 −0 src/evolve/core.clj
  6. +6 −0 test/evolve/test/core.clj
@@ -0,0 +1,6 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-failures
+.lein-deps-sum
13 README
@@ -0,0 +1,13 @@
+# tryclojure
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2011 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,5 @@
+(defproject evolve "1.0.0-SNAPSHOT"
+ :description "String Evolution"
+ :dependencies [[org.clojure/clojure "1.2.1"]
+ [org.clojure/clojure-contrib "1.2.0"]]
+ :main evolve.core)
Binary file not shown.
@@ -0,0 +1,29 @@
+(ns evolve.core
+ (:require [clojure.contrib.string :as contstr])
+ (:gen-class))
+
+(defn fitness [target]
+ (fn [source]
+ (apply +
+ (map
+ (fn [x y] (* (- x y) (- x y)))
+ (contstr/codepoints source)
+ (contstr/codepoints target))) ))
+
+(defn mutate [source]
+ (let [i (rand-int (count source))]
+ (str
+ (subs source 0 i) (char (inc (int (nth source i)))) (subs source (inc i)))))
+
+(defn evolve
+ [source fitness]
+ (first
+ (filter #(< (fitness %) (fitness source))
+ (iterate mutate source))))
+
+(defn start
+ [generations source target]
+ (nth (iterate #(evolve % (fitness target)) source) generations))
+
+(defn -main [& args]
+ (println (start 500 "dlk33ndoemgl" "Hello World!")))
@@ -0,0 +1,6 @@
+(ns evolve.test.core
+ (:use [evolve.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit d2537e5

Please sign in to comment.