Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (make-data-frame . args)
  4. (let* ([column-names (list->vector (map car args))]
  5. [data-store (list->vector (map (compose list->vector cdr) args))])
  6.  
  7. (define (get-row n)
  8. (vector-map
  9. (λ (v) (vector-ref v n))
  10. data-store))
  11.  
  12. (define (get-data-length)
  13. (vector-length (vector-ref data-store 0)))
  14.  
  15.  
  16. (define (format-row n)
  17. (define (default-format-function x)
  18. (~a x
  19. #:max-width 8
  20. #:min-width 8
  21. #:limit-marker "…"
  22. #:align 'center
  23. #:left-pad-string " "
  24. #:right-pad-string " "))
  25.  
  26. (let* ([nth-row (get-row n)]
  27. [formatted (vector-map default-format-function nth-row)])
  28. (string-append* (vector->list formatted))))
  29.  
  30.  
  31. (define (format-first-rows n)
  32. (string-join
  33. (map format-row (range n))
  34. "\n"))
  35.  
  36. (define (format-column)
  37. (define (default-format-function x)
  38. (~a x
  39. #:max-width 8
  40. #:min-width 8
  41. #:limit-marker "…"
  42. #:align 'center
  43. #:left-pad-string " "
  44. #:right-pad-string " "))
  45.  
  46. (let* ([column-formatted (vector-map default-format-function column-names)]
  47. [column-string (string-append* (vector->list column-formatted))])
  48. (string-append
  49. column-string "\n"
  50. (make-string (string-length column-string) #\―))))
  51.  
  52.  
  53. (define (show-columns)
  54. (display (format-column)) (display "\n"))
  55.  
  56. (define (show-row n)
  57. (display (format-row n))
  58. (display "\n"))
  59.  
  60. (define (show-rows [n 10])
  61. (display (format-first-rows (min n (get-data-length))))
  62. (display "\n"))
  63. (define (show-data [n 10])
  64. (show-columns)
  65. (show-rows n))
  66.  
  67.  
  68.  
  69. (case-lambda
  70. [() (show-data)]
  71.  
  72. [(op)
  73. (match op
  74. ['show-columns (show-columns)]
  75. ['show (show-data)]
  76. ['length (get-data-length)]
  77. ['columns column-names]
  78. ['data-store data-store]
  79. )]
  80.  
  81. [(op n)
  82. (match `(,op ,n)
  83. [(list 'show-row n)
  84. (show-columns)
  85. (show-row (sub1 n))]
  86.  
  87. [(list 'show 'all)
  88. (show-data (get-data-length))]
  89.  
  90. [(list 'show n)
  91. (show-data n)]
  92. )])
  93.  
  94. ))
  95.  
  96.  
  97. (define df (make-data-frame `[x . ,(range 100)] `[y . ,(range 100)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement