Sha256: ca6c73cc9c055379a218b9c9c68bcb7821502095bbc5f7c9e1e2a082e1169ded

Contents?: true

Size: 966 Bytes

Versions: 180

Compression:

Stored size: 966 Bytes

Contents

#lang racket

(provide grep-file)

(define (grep-file f pattern)
  (with-input-from-file f
    (lambda ()
      (for/list ([line (in-lines)] ;; Iterate over lines in file, lazily
                 [line-number (in-naturals 1)] ;; Iterate over natural numbers, lazily
                 #:when (regexp-match? pattern line))
        (cons line-number line)))))

;; =============================================================================

;; Code inside the "main" module is run in Dr.Racket or on the command-line,
;;  but not when the file is included as a library.
(module+ main
  (require racket/cmdline)
  (command-line
   #:program "racket-grep"
   #:args (pat-str . file*)
   (begin
     (define pattern (regexp pat-str))
     (for ([f (in-list file*)])
       (for ([num+str (in-list (grep-file f pattern))])
         (printf "~a\n~a\n" f (make-string (string-length f) #\-))
         (printf "~a : ~a\n" (car num+str) (cdr num+str))
         (newline))))))

Version data entries

180 entries across 180 versions & 1 rubygems

Version Path
trackler-2.1.0.24 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.23 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.22 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.21 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.20 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.19 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.18 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.17 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.16 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.15 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.14 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.13 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.12 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.11 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.10 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.9 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.8 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.7 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.6 tracks/racket/exercises/grep/example.rkt
trackler-2.1.0.5 tracks/racket/exercises/grep/example.rkt