I seem to write a lot of code which looks like: (coerce (blah blah blah blah) 'int)
The more stuff I have in the middle the harder it is for me to keep track of what I'm coercing the expression into.For example, here I'm reading a number from the input, then reading that number of bytes, and converting the bytes to a string: (def read-bytes ()
(let n (coerce (readline) 'int)
(coerce (map [coerce _ 'char] (n-of n (readb))) 'string)))
Here's an "as" macro, which swaps the arguments to "coerce", and quotes the type: (mac as (type x)
`(coerce ,x ',type))
I find this version easier to understand: (def read-bytes ()
(let n (as int (readline))
(as string (map [as char _] (n-of n (readb))))))
|