<div dir="ltr"><div><div>My original plan was to try to do some kind of mocking and unit testing in racket.<br><br></div>For example, I have a function that takes a filename and spits out a data structure. I wanted to create some unit tests and this mock the open-input-file function (with a open-input-string implementation).<br>
<br>I originally thought parameterize would allow me write the function as I normally would (i.e., not wrap functions in make-parameter), but it sounds like I can't. I sounds like the best way to test this function would be to pass in a port object and pass in a string port object for testing purposes.<br>
<br></div>- Cristian<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Dec 22, 2012 at 11:14 AM, Carl Eastlund <span dir="ltr"><<a href="mailto:cce@ccs.neu.edu" target="_blank">cce@ccs.neu.edu</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"><div><div><div><div><div><div>If you are working with functions in someone else's code, you are stuck with their definition. When you write your own functions, however, you can always design them as wrappers around parameters. For instance:<br>
<br></div>(define current-add-one (make-parameter add1))<br><br></div>(define (add-one x)<br></div> ((current-add-one) x))<br><br></div><div>(add-one 4)<br></div><div><br></div>(parameterize {[current-add-one (lambda (x) (add1 (add1 x)))]}<br>
</div> (add-one 4))<br><br></div>So now the function and the parameter are separate, but calling the function is always nice and short.<span class="HOEnZb"><font color="#888888"><br></font></span></div><div class="gmail_extra">
<span class="HOEnZb"><font color="#888888"><br clear="all"><div>Carl Eastlund</div>
<br><br></font></span><div class="gmail_quote"><div class="im">On Sat, Dec 22, 2012 at 4:23 AM, Cristian Esquivias <span dir="ltr"><<a href="mailto:cristian.esquivias@gmail.com" target="_blank">cristian.esquivias@gmail.com</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><p>Is this the general practice? It looks rather cumbersome and difficult to plan for.<br>
</p>
<p>Is there anything that would prevent me from having to wrap functions in make-parameter calls? Something like Clojure's binding special form (if you're familiar with it).</p><span><font color="#888888"><p>
- Cristian<br></p></font></span><div>
<div class="gmail_quote">On Dec 21, 2012 6:45 PM, "David Van Horn" <<a href="mailto:dvanhorn@ccs.neu.edu" target="_blank">dvanhorn@ccs.neu.edu</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 12/21/12 9:41 PM, Cristian Esquivias wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm trying to replace a function with another using parameterize.<br>
<br>
For example, when I try:<br>
<br>
(define (add2 n)<br>
(add1 (add1 n)))<br>
<br>
(parameterize ([add1 (λ [n] (+ n 2))])<br>
(add2 2))<br>
<br>
I get an error:<br>
<br>
parameterize: contract violation<br>
expected: parameter?<br>
received: #<procedure:add1><br>
<br>
How do I re-bind functions in Racket?<br>
</blockquote>
<br>
You can only use parameterize with names bound to parameter values. Here's an example:<br>
<br>
#lang racket<br>
(define padd1 (make-parameter add1))<br>
<br>
((padd1) 4)<br>
<br>
(parameterize ([padd1 (λ (n) (+ 2 n))])<br>
((padd1) 4))<br>
<br>
David<br>
<br>
<br>
</blockquote></div>
</div></div>
<br></div></div><div class="im">____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>