Re: [racket-users] Code critique (of naive code) would be very appreciated

2017-06-22 Thread Glenn Hoetker
Thank you, everyone!! I've gained some nice big picture lessons and appreciate 
the welcome to the community.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Code critique (of naive code) would be very appreciated

2017-06-21 Thread Jon Zeppieri
And I should heed my own advice about testing.


On Wed, Jun 21, 2017 at 8:40 PM, Jon Zeppieri  wrote:
>
> #lang at-exp racket
>
> (define (normalize-keywords in)
>   (regexp-replace*
>@pregexp|{Keywords ?= ?\{\s*([^}]*)\}}|
>in
>(λ (_ s)

You may have noticed that I left out part of the replacement string. :)

>  (string-join
>   (map capitalize-first (regexp-split #px", *" s))
>   ", "

Ahem...

   (λ (_ s)
 (string-append
  "Keywords = {"
  (string-join
   (map capitalize-first (regexp-split #px", *" s))
   ", ")
  "}"))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Code critique (of naive code) would be very appreciated

2017-06-21 Thread Jon Zeppieri
On Wed, Jun 21, 2017 at 8:40 PM, Jon Zeppieri  wrote:
>
> #lang at-exp racket
>
> ;; normalize-keywords: string? -> string?
> #lang at-exp racket
>

Sorry, I made some bad edits here. Should have been:

#lang at-exp racket

;; normalize-keywords: string? -> string?
(define (normalize-keywords in)
 ...)


By the way, I failed to mention the point of `at-exp` in the #lang
line. That allows the module to use at-expressions, which, in this
case, I use for writing the first regexp, since they allow me to elide
a layer of escape syntax.

- Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Code critique (of naive code) would be very appreciated

2017-06-21 Thread Jon Zeppieri
Hi Glenn,

This is pretty good! I added some remarks below:

On Wed, Jun 21, 2017 at 3:48 PM, Glenn Hoetker  wrote:
>
> ;;;
> #lang racket
>
> (define in (open-input-file "/Users/me/BibDeskPapers/oldBib.bib"))
> (define out (open-output-file "/Users/me/BibDeskPapers/newBib.bib" #:exists 
> 'replace))
> (define keyword-prefix "\tKeywords = {")
>
> ;; Return a string with only the first word capitalized and all else in lower 
> case
> ;; Courtesy https://groups.google.com/forum/m/#!topic/racket-users/gw8Ivm5HSZQ
> (provide
>  (contract-out
>   [string-upcase-first-word (-> string? string?)]))

If you want your use of `string-upcase-first-word` to be protected by
this contract, then you'll either need to:
- provide it, like you do here, from a *different* module, and require
it in this one, or
- define the function in this module with `define/contract`.

What you're doing:
   (provide (contract-out ...))

... means that other modules that require this one will get the
contract-protected version of the function, but within this module,
the function has no contract. You can read about contract boundaries
here [http://docs.racket-lang.org/guide/contract-boundaries.html].

>
> (define (string-upcase-first-word s)
>   (apply
>string
>(for/list ([c (in-string s)]
>   [i (in-naturals)])
>  (if (= i 0)
>  (char-upcase c)
>  (char-downcase c)
>
> (module+ test
>   (require rackunit)
>   (check-equal? (string-upcase-first-word "") "")
>   (check-equal? (string-upcase-first-word "Cat Dog") "Cat dog")
>   (check-equal? (string-upcase-first-word "cat dog") "Cat dog"))
>
> ;; Main program
> (for ([aLine (in-lines in)])

Racketeers typically use hyphen-delimited identifiers instead of camel
case (though in this particular situation, I think it would be more
common to use `line` instead of either `aLine` or  `a-line`).

>   (cond
> [(string-prefix? aLine "\tKeywords = {") ; Identify Keywords lines
>  (define keywords-as-list (string-split (string-replace aLine "\tKeywords 
> = {" "") ","))
>  (define cleaned-keywords (map (lambda (aString)
>  (string-upcase-first-word
>   (string-trim aString)))
>keywords-as-list))
>  (display keyword-prefix out)
>  (display (string-join cleaned-keywords ", ") out)
>  (display "," out)
>  (newline out)]
> [else (display aLine out) (newline out)]))

Usually, you'd put separate sequential expressions on separate lines.
Since the second expression is just meant to add a newline to the end
of the string you just displayed, you can use `displayln` instead:

  [else (displayln line)]

>
> ;;;

One general piece of advice: abstract your main loop into a procedure
that accepts input and output ports. That will make it much easier to
test.

I gave some thought to how I'd accomplish the same thing and came up
with the following. This version assumes you're going to read the
entire file into memory as a string. Of course, you can substitute
your favorite version of the capitalization procedure:

#lang at-exp racket

;; normalize-keywords: string? -> string?
#lang at-exp racket

(define (normalize-keywords in)
  (regexp-replace*
   @pregexp|{Keywords ?= ?\{\s*([^}]*)\}}|
   in
   (λ (_ s)
 (string-join
  (map capitalize-first (regexp-split #px", *" s))
  ", "

(define (capitalize-first s)
  (cond
[(non-empty-string? s)
 (string-append
  (string-upcase (substring s 0 1))
  (string-downcase (substring s 1)))]
[else
 ""]))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Code critique (of naive code) would be very appreciated

2017-06-21 Thread Deren Dohoda
Hi Glenn,

Because of the submodule system it's not necessarily a good idea to put
running code in the file directly. If you want something to run when the
file is the main file (e.g. run in drracket) you can use (module* main #f
...). This way (module+ test ...) doesn't execute these things, and if the
file is required for any items it provides that code isn't run, either.

Consider this example:
#lang racket/base

(define (add2 x)
  (and (number? x)
   (+ 2 x)))
(displayln "in file")

(module* main #f
  (displayln "in main")
  (add2 3))

(module+ test
  (require rackunit)
  (displayln "in test")
  (check-= (add2 3) 5 0)
  (check-false (add2 'q)))
;
Welcome to DrRacket, version 6.9 [3m].
Language: racket/base, with debugging; memory limit: 128 MB.
in file
in test
in main
5
>

Two things to notice: 1) "test" is run before "main"; 2) all the code from
"in file" is run before any "test" or "main". If you're counting on your
tests to tell you whether it's ok to run the code then I hope you see my
point from the first paragraph that such code should be moved to a
different module or the "main" submodule.

Deren

On Wed, Jun 21, 2017 at 3:48 PM, Glenn Hoetker  wrote:

> With help from this group, I've written my first baby-program in Racket
> and would really appreciate any feedback on how to make it more idiomatic,
> efficient and well-formatted (I struggle with where to break lines). Thank
> you in advance.
>
> The purpose is to fix the irregular capitalization of entries in the
> "Keywords" field of a large BibLaTeX (.bib) file. A sample entry in the
> input file might be
>
> @article{Ender-2016-Review-00,
> Author = {Ender, P. B.},
> Keywords = {Book Review, Behaviorial sciences, ANALYSIS OF
> VARIANCE, experimental design},
> ...}
>
> I want non-Keyword lines passed unchanged to the output file and each
> keyword entry (potentially multiword) changed to have only its first word
> capitalized. Thus
>
> @article{Ender-2016-Review-00,
> Author = {Ender, P. B.},
> Keywords = {Book review, Behaviorial sciences, Analysis of
> variance, Experimental design},
> ...}
>
>
> Here is what I came up with.
>
> ;;;
> #lang racket
>
> (define in (open-input-file "/Users/me/BibDeskPapers/oldBib.bib"))
> (define out (open-output-file "/Users/me/BibDeskPapers/newBib.bib"
> #:exists 'replace))
> (define keyword-prefix "\tKeywords = {")
>
> ;; Return a string with only the first word capitalized and all else in
> lower case
> ;; Courtesy https://groups.google.com/forum/m/#!topic/racket-users/
> gw8Ivm5HSZQ
> (provide
>  (contract-out
>   [string-upcase-first-word (-> string? string?)]))
>
> (define (string-upcase-first-word s)
>   (apply
>string
>(for/list ([c (in-string s)]
>   [i (in-naturals)])
>  (if (= i 0)
>  (char-upcase c)
>  (char-downcase c)
>
> (module+ test
>   (require rackunit)
>   (check-equal? (string-upcase-first-word "") "")
>   (check-equal? (string-upcase-first-word "Cat Dog") "Cat dog")
>   (check-equal? (string-upcase-first-word "cat dog") "Cat dog"))
>
> ;; Main program
> (for ([aLine (in-lines in)])
>   (cond
> [(string-prefix? aLine "\tKeywords = {") ; Identify Keywords lines
>  (define keywords-as-list (string-split (string-replace aLine
> "\tKeywords = {" "") ","))
>  (define cleaned-keywords (map (lambda (aString)
>  (string-upcase-first-word
>   (string-trim aString)))
>keywords-as-list))
>  (display keyword-prefix out)
>  (display (string-join cleaned-keywords ", ") out)
>  (display "," out)
>  (newline out)]
> [else (display aLine out) (newline out)]))
>
> ;;;
>
> Thank you again.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.