Without using a do-block, you would need to pass in a function as the first
argument to 'map'.
'open' has a variant where the first argument is again a function that
accepts an open handle.

The do-block syntax in this case just allows you to define the said
function.


On Mon, Apr 28, 2014 at 8:55 AM, Peter Simon <[email protected]> wrote:

> In the Julia manual, the second example in
> block-syntax-for-function-arguments<http://docs.julialang.org/en/latest/manual/functions/#block-syntax-for-function-arguments>
>  contains
> the following do block:
>
>     open("outfile", "w") do f
>         write(f, data)
>     end
>
> and the documentation states that "The function argument to open receives a 
> handle to the opened file."  I conclude from this that the return value 
> (i.e., the file handle) of the open function is passed to this function f -> 
> write(f, data) that is used as the first argument of open.  So far, so good 
> (I think).  But now I go back and take another look at the first do block 
> example:
>
> map([A, B, C]) do x
>     if x < 0 && iseven(x)
>         return 0
>     elseif x == 0
>         return 1
>     else
>         return x
>     endend
>
> I try to interpret this example in light of what I learned from the second 
> example.  The map function has a return value, consisting of the array [A, B, 
> C], modified by applying the function in the do block to each element.  If 
> this example behaved like in the second example, then the output of the map 
> function should be passed as an input to the function defined in the do 
> block.  Clearly this doesn't happen, so the lesson I learned from the second 
> example doesn't apply here, apparently.  Why not?  Under what conditions is 
> the output of the outer function passed as an input to the inner function?
>
> I must be looking at this wrong and would appreciate some help in getting my 
> mind right :-).
>
> Thanks,
>
> Peter
>
>

Reply via email to