Artifacts: Files - racket/racket GitHub Wiki

This page captures useful code snippets that are too small to be a package.

There is also an GitHub organisation for larger examples https://github.com/racket-artifacts

Please contribute your own!

Create a large file

Quickly create a large file (e.g. for testing, or to reserve disk space for later use)

(file-position (open-output-file (make-temporary-file)) (* 1024 1024)) ;; 1MB zero-filled file

Merge text files line by line

#lang racket

;; merge files a.txt and b.txt and write ab.txt where each line is constructed from the line in a.txt, immediately followed by the corresponding line in b.txt
(with-output-to-file "ab.txt"
  (lambda ()
    (printf
     (apply
      ~a
      (map
       (λ (a b)  (~a a b "\n"))
       (file->lines "a.txt")
       (file->lines "b.txt")))))
  #:mode 'text	 	 	 	 
  #:exists 'replace)