Last active
January 21, 2017 13:09
-
-
Save lispm/82f9265281cfc282a8797861277c685f to your computer and use it in GitHub Desktop.
not-yield.lisp
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
; response to https://kaushikghose.wordpress.com/2017/01/18/common-lisp-doesnt-yield/ | |
; write functions which map a function over stuff | |
; use I/O streams | |
(defun map-fastq-stream (in fn) | |
"Map FN over the IN stream. FN gets called on each second line out of four lines." | |
(flet ((fastq-reader (in) | |
(prog2 | |
(read-line in nil) | |
(read-line in nil) | |
(read-line in nil) | |
(read-line in nil)))) | |
(loop for seq = (fastq-reader in) | |
while seq do (funcall fn seq)))) | |
(defun map-kmer (seq size fn &aux (length (length seq))) | |
"call FN on all sub-sequences of length size, starting left" | |
(loop for pos from 0 | |
for end = (+ pos size 1) | |
while (< end length) | |
do (funcall fn (subseq seq pos end)))) | |
; Example call: | |
(defparameter *example-input* | |
"a | |
27572394572934759823745723947592345 | |
c | |
d | |
a | |
dhaskhdkashdkhaskdhakshdkahskjdhasd | |
c | |
d | |
a | |
27572394572934759823745723947592345 | |
c | |
d | |
a | |
27572394572934759823745723947592345 | |
c | |
d") | |
(with-input-from-string (stream *example-input*) | |
(map-fastq-stream stream | |
(lambda (seq) | |
(map-kmer seq 20 #'print)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment