Skip to content

Instantly share code, notes, and snippets.

@lispm
Last active January 21, 2017 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispm/82f9265281cfc282a8797861277c685f to your computer and use it in GitHub Desktop.
Save lispm/82f9265281cfc282a8797861277c685f to your computer and use it in GitHub Desktop.
not-yield.lisp
; 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