Last active
December 21, 2015 18:19
-
-
Save ktakashi/6346720 to your computer and use it in GitHub Desktop.
お題: ls -l の出力結果からサイズを取り出して加算した結果を返す
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
#!r6rs | |
(import (rnrs)) | |
(define (port-fold proc seed reader) | |
(let loop ((e (reader)) (seed seed)) | |
(if (eof-object? e) | |
seed | |
(loop (reader) (proc e seed))))) | |
(define (parse-line port) | |
(define (skip-space in) | |
(do ((c (lookahead-char in) (lookahead-char in))) | |
((or (eof-object? c) (not (char-whitespace? c)))) | |
(get-char in)) | |
(do ((c (get-char in) (get-char in)) (r '() (cons c r))) | |
((or (eof-object? c) (char-whitespace? c)) | |
(if (null? r) c (list->string (reverse r)))))) | |
(define (->number n) | |
(let ((l (string-length n))) | |
(if (char-numeric? (string-ref n (- l 1))) | |
(string->number n) | |
(let ((n2 (string->number (substring n 0 (- l 1))))) | |
(case (string-ref n (- l 1)) | |
((#\K) (* n2 1000)) | |
((#\M) (* n2 (* 1000 1000))) | |
(else (error 'parse-line "not supported" n))))))) | |
(let ((line (get-line port))) | |
(if (eof-object? line) | |
line | |
(let ((elems (call-with-port (open-string-input-port line) | |
(lambda (in) | |
(port-fold cons '() (lambda () (skip-space in))))))) | |
(if (= (length elems) 9) | |
(->number (car (cddddr elems))) | |
0))))) | |
(display (port-fold + 0 (lambda () (parse-line (current-input-port))))) | |
(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment