[racket] (no subject)

From: Sean McBeth (sean.mcbeth at gmail.com)
Date: Mon May 13 07:00:31 EDT 2013

In your definition for data set, I'm concerned about the call to read-json.
The documentation doesn't say if it raises an exception on malformed json,
but a quick test shows that it does indeed raise an exn:fail:read exception
for bad input. So your call to read-json should include an exception
handler to make sure your input port gets closed when you're done. Perhaps
it should be something like:

(define data-set
  (let* (
         ;; Turn url into a net/url-struct so you can allow a different
search query as well as other customizations
         ;; easily.
         [api-url (string->url "
http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception
")]
         ;; figure out how to use call/input url
         [in (get-pure-port api-url #:redirections 2)]
         [json-blob (with-handlers ([exn:fail:read?
                                     (lambda (e) #f)])
                      (read-json in))])
    (close-input-port in)
    json-blob))


So close-input-port gets called either way, but if the returned json is
malformed, json-blob will be set to #f, so data-set will be set to #f. #f
just needs to be in tail-position for that lambda, you could do whatever
you want before it to inspect the exception or figure out how to stop your
program from continuing.


On Mon, May 13, 2013 at 5:45 AM, Cody Goodman <codygman.consulting at gmail.com
> wrote:

> Hello, can anyone offer an idiom/thinking process/approach critique on my
> code below? I'm new to racket and trying to program in racket, not program
> python in racket syntax ;)
>
> #lang racket
> (require json)
> (require net/url)
>
> (define data-set
>   (let* (
>          ;; Turn url into a net/url-struct so you can allow a different
> search query as well as other customizations
>          ;; easily.
>          [api-url (string->url "
> http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception
> ")]
>          ;; figure out how to use call/input url
>          [in (get-pure-port api-url #:redirections 2)]
>          [json-blob (read-json in)])
>     (close-input-port in)
>     json-blob))
>
> (struct movie (title price link))
> (define movies (hash-ref (hash-ref data-set 'feed) 'entry))
>
> (define (make-movie movie-result)
>   (let* (
>          [title (hash-ref (hash-ref movie-result 'title) '$t)]
>          [price (with-handlers ([exn:fail? (lambda (exn) 0)])
>                   (hash-ref (car (hash-ref (hash-ref movie-result
> 'media$group) 'media$price)) 'price))]
>          [link (hash-ref (car (hash-ref movie-result 'link)) 'href)]
>          [movie-to-return (movie title price link)])
>     movie-to-return))
>
> (define (make-movie-listing movie)
>   (list (movie-title movie) (movie-price movie) (movie-link movie)))
>
> (define (show-all-movie-listings)
> (map make-movie-listing (map make-movie movies)))
>
> (define (get-relevant-movie-listings movie)
>   ;; TODO: Figure out how to pass paramters into a function with map.
> Maybe lambda?
>   (cond
>     [(string=? (movie-title movie) "Inception") movie]))
>
> ;; TODO: Make this into a function after figuring out how to pass
> parameters into a function with map.
> (define find-movie
>   (car(map get-relevant-movie-listings (map make-movie movies))))
>
> (define inception-movie find-movie)
> (movie-title inception-movie)
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130513/12756bdd/attachment-0001.html>

Posted on the users mailing list.