<div dir="ltr"><div>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:<br>
<br>(define data-set<br> (let* (<br> ;; Turn url into a net/url-struct so you can allow a different search query as well as other customizations<br> ;; easily.<br> [api-url (string->url "<a href="http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception">http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception</a>")]<br>
;; figure out how to use call/input url<br> [in (get-pure-port api-url #:redirections 2)]<br> [json-blob (with-handlers ([exn:fail:read?<br> (lambda (e) #f)])<br>
(read-json in))])<br> (close-input-port in)<br> json-blob))<br><br><br></div>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.<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 13, 2013 at 5:45 AM, Cody Goodman <span dir="ltr"><<a href="mailto:codygman.consulting@gmail.com" target="_blank">codygman.consulting@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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 ;)<br>
<br>#lang racket<br>(require json)<br>
(require net/url)<br><br>(define data-set<br> (let* (<br> ;; Turn url into a net/url-struct so you can allow a different search query as well as other customizations<br> ;; easily.<br> [api-url (string->url "<a href="http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception" target="_blank">http://gdata.youtube.com/feeds/api/videos/?alt=json&v=2&category=Movies&q=Inception</a>")]<br>
;; figure out how to use call/input url<br> [in (get-pure-port api-url #:redirections 2)]<br> [json-blob (read-json in)])<br> (close-input-port in)<br> json-blob))<br><br>(struct movie (title price link))<br>
(define movies (hash-ref (hash-ref data-set 'feed) 'entry))<br><br>(define (make-movie movie-result)<br> (let* (<br> [title (hash-ref (hash-ref movie-result 'title) '$t)]<br> [price (with-handlers ([exn:fail? (lambda (exn) 0)])<br>
(hash-ref (car (hash-ref (hash-ref movie-result 'media$group) 'media$price)) 'price))]<br> [link (hash-ref (car (hash-ref movie-result 'link)) 'href)]<br> [movie-to-return (movie title price link)])<br>
movie-to-return))<br><br>(define (make-movie-listing movie)<br> (list (movie-title movie) (movie-price movie) (movie-link movie)))<br><br>(define (show-all-movie-listings)<br>(map make-movie-listing (map make-movie movies)))<br>
<br>(define (get-relevant-movie-listings movie)<br> ;; TODO: Figure out how to pass paramters into a function with map. Maybe lambda?<br> (cond<br> [(string=? (movie-title movie) "Inception") movie]))<br><br>
;; TODO: Make this into a function after figuring out how to pass parameters into a function with map.<br>(define find-movie <br> (car(map get-relevant-movie-listings (map make-movie movies))))<br><br>(define inception-movie find-movie)<br>
(movie-title inception-movie)<br></div>
<br>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>