Comment detail

タブ区切りデータの処理 (Nested Flatten)
オブジェクト指向っぽく書いてみたつもりです…
(with-open-file (in "doukaku-209.data")
  (update-file (make-instance 'doukaku-209) in *standard-output*))
;>>>
ID Forename Surname Age
0 Taro Suzuki 19
1 Hanako Sato 18
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(defpackage :doukaku-209 (:use :cl :split-sequence))
(in-package :doukaku-209)

(defclass file-op () ())

(defgeneric file-to-data (file-op stream))
(defgeneric swap-colum (file-op data))
(defgeneric sort-data (file-op data))
(defgeneric update-datum (file-op data))
(defgeneric format-out-data (file-op stream data))
(defgeneric update-file (file-op in-stream out-stream))

(defmethod update-file ((op file-op) (in stream) (out stream))
  (format-out-data op out
    (swap-colum op 
      (destructuring-bind (title &rest data) (file-to-data op in)
        `(,title ,@(sort-data op (loop :for line :in data 
                                       :collect (update-datum op line))))))))
                     
(defclass doukaku-209 (file-op) ())

(defmethod file-to-data ((op doukaku-209) (in stream))
  (loop :for line := (read-line in nil nil) :while line
        :collect (split-sequence #\Tab line)))

(defmethod swap-colum ((op doukaku-209) (data list))
  (loop :for xx :in (copy-list data) 
        :do (rotatef (nth 1 xx) (nth 2 xx)) 
        :collect xx))

(defmethod sort-data ((op doukaku-209) (data list))
  (sort (copy-list data) #'< :key #'first))

(defmethod format-out-data ((op doukaku-209) (out stream) (data list))
  (format out "~{~{~A~^    ~}~%~}" data))

(defmethod update-datum ((op doukaku-209) (row list))
  (destructuring-bind (id sur fore age) row
    (list (parse-integer id)
          sur
          fore
          (1+ (parse-integer age)))))

Index

Feed

Other

Link

Pathtraq

loading...