私はこのコーディングチャレンジのClojure実装を作成しており、Fasta形式のシーケンスレコードの平均の長さを見つけようとしています。
>1
GATCGA
GTC
>2
GCA
>3
AAAAA
詳細な背景については、Erlangソリューションに関するこの関連するStackOverflowの投稿を参照してください。
私の初心者のClojureの試みでは、lazy-seqを使用して、一度に1レコードずつファイルを読み込もうとしているため、大きなファイルにスケーリングされます。ただし、かなりメモリを消費し、低速であるため、最適に実装されていないのではないかと思います。これは、 BioJavaライブラリを使用してレコードの解析を抽象化するソリューションです。
(import '(org.biojava.bio.seq.io SeqIOTools))
(use '[clojure.contrib.duck-streams :only (reader)])
(defn seq-lengths [seq-iter]
"Produce a lazy collection of sequence lengths given a BioJava StreamReader"
(lazy-seq
(if (.hasNext seq-iter)
(cons (.length (.nextSequence seq-iter)) (seq-lengths seq-iter)))))
(defn fasta-to-lengths [in-file seq-type]
"Use BioJava to read a Fasta input file as a StreamReader of sequences"
(seq-lengths (SeqIOTools/fileToBiojava "fasta" seq-type (reader in-file))))
(defn average [coll]
(/ (reduce + coll) (count coll)))
(when *command-line-args*
(println
(average (apply fasta-to-lengths *command-line-args*))))
および外部ライブラリを使用しない同等のアプローチ:
(use '[clojure.contrib.duck-streams :only (read-lines)])
(defn seq-lengths [lines cur-length]
"Retrieve lengths of sequences in the file using line lengths"
(lazy-seq
(let [cur-line (first lines)
remain-lines (rest lines)]
(if (= nil cur-line) [cur-length]
(if (= \> (first cur-line))
(cons cur-length (seq-lengths remain-lines 0))
(seq-lengths remain-lines (+ cur-length (.length cur-line))))))))
(defn fasta-to-lengths-bland [in-file seq-type]
; pop off first item since it will be everything up to the first >
(rest (seq-lengths (read-lines in-file) 0)))
(defn average [coll]
(/ (reduce + coll) (count coll)))
(when *command-line-args*
(println
(average (apply fasta-to-lengths-bland *command-line-args*))))
現在の実装は、Python実装の7秒と比較して、大きなファイルでは44秒かかります。コードを高速化し、より直感的にするための提案はありますか?lazy-seqの使用法は、意図したとおりにファイルレコードをレコードごとに正しく解析していますか?