Skip to content

Instantly share code, notes, and snippets.

@phelrine
Created June 11, 2011 14:52
Show Gist options
  • Save phelrine/1020622 to your computer and use it in GitHub Desktop.
Save phelrine/1020622 to your computer and use it in GitHub Desktop.
ファイル分割
#!/usr/bin/env gosh
(use util.match)
(use file.util)
(use gauche.sequence)
(define (usage program)
(print #`"usage: ,|program| filename num..."))
(define (main args)
(match args
((_ filename . rest)
(for-each-with-index
(lambda (i lines)
(unless (null? lines)
(for-each-print-to-file #`",|filename|-,|i|" lines)))
(divide (file->string-list filename)
(if (string? rest)
(string->integer-list rest)
(map x->integer rest)))))
((program . _) (usage program))))
(define (for-each-print-to-file filename ls)
(with-output-to-file filename
(lambda () (for-each print ls))))
(define (string->integer-list input)
(with-input-from-string input
(lambda ()
(port-map (lambda (x) (if (integer? x) x (error #`",|x| is not integer")))
read))))
(define (divide ls number-list)
(receive (mapped rest)
(map-accum (lambda (size start)
(let ((end (+ start size)))
(values (subseq ls start end) end)))
0
(remove (cut = 0 <>) number-list))
(append mapped (list (subseq ls rest)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment