Created
June 12, 2010 17:52
-
-
Save j1n3l0/435926 to your computer and use it in GitHub Desktop.
This file contains 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
;;; ---------------------------------------------------------------------------- | |
;;; (Re)Introduction to Clojure | |
;;; ---------------------------------------------------------------------------- | |
(ns j1n3l0 | |
;; (:gen-class) | |
(:use [clojure.contrib.str-utils2 :only (capitalize)] | |
[clojure.contrib.duck-streams :only (read-lines)])) | |
(defn hello | |
"Simple 'Hello, name!' function" | |
[name] | |
(str "Hello, " (capitalize name) "!")) | |
(defn shout-at | |
"Use java function in clojure" | |
[name] | |
(.toUpperCase name)) | |
(defn a-sequence | |
"Perform a sequence of actions/expressions" | |
[name] | |
(println (hello name)) | |
(println (shout-at (str "oi " name "!")))) | |
(defn greet | |
"Simple multimethod greeter function" | |
([] (hello "world")) | |
([name & more] (map hello (list* name more)))) | |
(defn sq | |
"Return the square of the number n or n to the given power p" | |
([n] (reduce * (repeat 2 n))) | |
([n p] (reduce * (repeat p n)))) | |
;; user> (time (dotimes [_ 10000] (j1n3l0/digits-1 1234))) | |
;; "Elapsed time: 8.996 msecs" | |
;; nil | |
;; user> (time (dotimes [_ 10000] (j1n3l0/digits-2 1234))) | |
;; "Elapsed time: 47.653 msecs" | |
;; nil | |
;; user> | |
;; user> (time (dotimes [_ 10000] (j1n3l0/digits-3 1234))) | |
;; "Elapsed time: 129.459 msecs" | |
;; nil | |
;; user> | |
;; digits-1 is faster ;) | |
(defn digits-1 | |
"Return the digits of a number as a list" | |
[number] | |
(map #(- (int %) (int \0)) (seq (str number)))) | |
(defn digits-2 | |
"Return the digits of a number as a list" | |
[number] | |
(let [m { \0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9 }] | |
(map #(m %) (seq (str number))))) | |
(defn digits-3 | |
"Return the digits of a number as a list" | |
[number] | |
(reverse (map #(rem % 10) (take-while #(> % 0) (iterate #(quot % 10) number))))) | |
(defn digits | |
"Return the digits of a number as a list" | |
[number] | |
(map #(- (int %) (int \0)) (seq (str number)))) | |
(defn euler-016 | |
"Solution to Euler problem 016" | |
[] | |
(reduce + (digits (sq 2 1000)))) | |
(defn factorial | |
"" | |
[number] | |
(reduce * (range 1 (inc number)))) | |
(defn euler-20 | |
"" | |
[] | |
(reduce + (digits (factorial 100)))) | |
(defn number-list | |
"" | |
[file]) | |
(def file "/Users/io1/Documents/clojure/euler/data/one-hundred_fifty-digit_numbers.txt") | |
(defn euler-13 | |
"" | |
[file] | |
(apply str (take 10 (digits (reduce + (map #(BigInteger. %) (remove #(= "" %) (read-lines file)))))))) | |
;;; ---------------------------------------------------------------------------- | |
;;; My attempt at the the Quicksort algorithm | |
;;; ---------------------------------------------------------------------------- | |
;; (defn less-than | |
;; [x xs] | |
;; (filter (fn [n] (< n x)) xs)) | |
;; (defn more-than | |
;; [x xs] | |
;; (filter (fn [n] (> n x)) xs)) | |
;; (defn quicksort | |
;; "My attempt at the the Quicksort algorithm" | |
;; ([] ()) | |
;; ([ns] (let [x (first ns) xs (rest ns)] | |
;; (lazy-cat (less-than x xs) (list x) (more-than x xs))))) | |
;;; ---------------------------------------------------------------------------- | |
;;; My attempt to write a GenBank file parser | |
;;; ---------------------------------------------------------------------------- | |
(defn parse-genbank-file | |
"Parse a Genbank file and return the contents as a Clojure data structure | |
Below is an example of the data structure I would like returned. | |
Add more features to the list as you expand your tests and code. | |
Moved my features to a clojure file (since code is data) and just | |
simply load the file here. In the long run these will come | |
straight out of the GenBank file" | |
[file] | |
(load-file file)) | |
;; (load-file "/Users/io1/Documents/clojure/j1n3l0.clj") | |
;; (doseq [f (:features (j1n3l0/parse-genbank-file "/Users/io1/Documents/clojure/allele-imaging/features.clj"))] (println (:label f))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
testing gist.el