Re: [julia-users] for loops

2014-05-17 Thread Cameron McBride
Stefan,

Thank you. Your description really helps clarify things. The issue about
different functionality for "return" in map vs for loops was obviously
something I overlooked here.

And yes, the influence is clearly ruby.

I see how a macro could can duplicate the for loop structure. I guess I'm a
bit surprised that is the only way, and there wasn't a metaprogramming way
to add this functionality to all iterators.

I guess what I was wanting was the ability to use something like "each"
(which functions like a for loop, but looks like map()), and any such
function (enumerate, etc.), which I guess is just more method definitions.

But I probably just have too much baggage from history, so I'll just try
and use natural julia expressions for the time being...  ;)

Cameron





On Fri, May 16, 2014 at 12:58 PM, Stefan Karpinski wrote:

> That form of iteration is common from Ruby, which is where I'm guessing
> this interest is coming from? Ruby has the block/proc/lambda distinction
> that makes the for loop and .each forms behaviorally similar, whereas in
> Julia, there's just the one kind of anonymous function. As a result, if
> this were made to work, enumerate(a) do i,x and for (i,x) in enumerate(a)
> would not behave exactly the same – return in the former would exit the
> current loop iteration whereas return in the latter exits the enclosing
> function. I'm not sure it's a good idea to introduce too many different
> ways to write this. If we were going to implement this, we wouldn't do it
> by adding language features, but by adding methods to iterator-producing
> functions like enumerate. In this case, you could do something like this:
>
> function enumerate(f::Callable, x)
>   for (i,x) in enumerate(x)
> f(i,x)
>   end
> end
>
>
> That pattern could be emitted by a macro, of course.
>


Re: [julia-users] for loops

2014-05-16 Thread Stefan Karpinski
That form of iteration is common from Ruby, which is where I'm guessing
this interest is coming from? Ruby has the block/proc/lambda distinction
that makes the for loop and .each forms behaviorally similar, whereas in
Julia, there's just the one kind of anonymous function. As a result, if
this were made to work, enumerate(a) do i,x and for (i,x) in enumerate(a)
would not behave exactly the same – return in the former would exit the
current loop iteration whereas return in the latter exits the enclosing
function. I'm not sure it's a good idea to introduce too many different
ways to write this. If we were going to implement this, we wouldn't do it
by adding language features, but by adding methods to iterator-producing
functions like enumerate. In this case, you could do something like this:

function enumerate(f::Callable, x)
  for (i,x) in enumerate(x)
f(i,x)
  end
end


That pattern could be emitted by a macro, of course.


Re: [julia-users] for loops

2014-05-15 Thread Mike Innes
Ah, I see what you mean. I'm not sure if it's possible, though.

You'd have to determine whether f() do ... meant "function with do block"
or "iterator with do block", which isn't possible in general. So at least
you'd need special syntax for it, and by that point it's probably easier to
stick with map.


On 15 May 2014 22:33, Cameron McBride  wrote:

> Sure, Mike.  But the idea is to have this for all iterator objects
> intrinsically rather than defining it for each function that returns an
> iterator.
>
> There is likely a way to do this automagically for all iterators, but my
> julia-fu isn't strong enough that it jumped out at me when I looked over
> some source in base/.  I expect it's simple, but I don't have time to
> figure it out today.
>
> Cameron
>
>
>
> On Thu, May 15, 2014 at 4:51 PM, Mike Innes wrote:
>
>> Well, if you want the first syntax you can easily define
>>
>> Base.enumerate(f::Function, args...) = map(t->f(t...), enumerate(args
>> ...))
>>
>> You could always open a pull request if you wanted to see this in Base,
>> too.
>>
>>
>> On Thursday, 15 May 2014 21:18:31 UTC+1, Cameron McBride wrote:
>>
>>> I missed enumerate() for a while,  and was happy I found it.  I find it
>>> amusing how satisfying a few missing keystrokes can be.
>>>
>>> On a related but different note, from a similar influence, I keep
>>> wanting to pass blocks to iterators.  Any chance that will ever happen?
>>>
>>> I realize that do..end blocks are used currently as syntactic sugar for
>>> methods that take a function as the first arg (e.g. open(), map()), and the
>>> same functionality can be achieved with three letters and two braces (map),
>>> but it still seems somewhat cleaner to write:
>>>
>>> enumerate(a) do i,x
>>> ...
>>> end
>>>
>>> over
>>>
>>> map(enumerate(a)) do i,x
>>> ...
>>> end
>>>
>>> which are really just equivalent, as we know, to
>>>
>>> for i,x, in enumerate(a)
>>> ...
>>> end
>>>
>>> Are there technical reasons this is a bad idea to assume?
>>>
>>> Cameron
>>>
>>> On Thu, May 15, 2014 at 1:01 PM, John Myles White 
>>> wrote:
>>>
 I kind of suspect Stefan, like me, would instinctively call this
 operation `each_with_index`.

  -- John

 On May 15, 2014, at 6:33 AM, Kevin Squire  wrote:

 One nice thing about Julia is that she borrows many (though not all) good
 ideas from other languages. In this case, enumerate came from Python
 (although it likely has other incarnations).

 Cheers!
Kevin


 On Thursday, May 15, 2014, Billou Bielour  wrote:

> I was thinking the same thing the other day, when using *for x in xs* I
> often find myself needing an index at some point and then I have to change
> the for loop, or write an index manually.
>
> Enumerate is exactly what I need in this case.
>
> +1 for Julia
>
>

>>>
>


Re: [julia-users] for loops

2014-05-15 Thread Cameron McBride
Sure, Mike.  But the idea is to have this for all iterator objects
intrinsically rather than defining it for each function that returns an
iterator.

There is likely a way to do this automagically for all iterators, but my
julia-fu isn't strong enough that it jumped out at me when I looked over
some source in base/.  I expect it's simple, but I don't have time to
figure it out today.

Cameron



On Thu, May 15, 2014 at 4:51 PM, Mike Innes  wrote:

> Well, if you want the first syntax you can easily define
>
> Base.enumerate(f::Function, args...) = map(t->f(t...), enumerate(args...))
>
> You could always open a pull request if you wanted to see this in Base,
> too.
>
>
> On Thursday, 15 May 2014 21:18:31 UTC+1, Cameron McBride wrote:
>
>> I missed enumerate() for a while,  and was happy I found it.  I find it
>> amusing how satisfying a few missing keystrokes can be.
>>
>> On a related but different note, from a similar influence, I keep wanting
>> to pass blocks to iterators.  Any chance that will ever happen?
>>
>> I realize that do..end blocks are used currently as syntactic sugar for
>> methods that take a function as the first arg (e.g. open(), map()), and the
>> same functionality can be achieved with three letters and two braces (map),
>> but it still seems somewhat cleaner to write:
>>
>> enumerate(a) do i,x
>> ...
>> end
>>
>> over
>>
>> map(enumerate(a)) do i,x
>> ...
>> end
>>
>> which are really just equivalent, as we know, to
>>
>> for i,x, in enumerate(a)
>> ...
>> end
>>
>> Are there technical reasons this is a bad idea to assume?
>>
>> Cameron
>>
>> On Thu, May 15, 2014 at 1:01 PM, John Myles White 
>> wrote:
>>
>>> I kind of suspect Stefan, like me, would instinctively call this
>>> operation `each_with_index`.
>>>
>>>  -- John
>>>
>>> On May 15, 2014, at 6:33 AM, Kevin Squire  wrote:
>>>
>>> One nice thing about Julia is that she borrows many (though not all) good
>>> ideas from other languages. In this case, enumerate came from Python
>>> (although it likely has other incarnations).
>>>
>>> Cheers!
>>>Kevin
>>>
>>>
>>> On Thursday, May 15, 2014, Billou Bielour  wrote:
>>>
 I was thinking the same thing the other day, when using *for x in xs* I
 often find myself needing an index at some point and then I have to change
 the for loop, or write an index manually.

 Enumerate is exactly what I need in this case.

 +1 for Julia


>>>
>>


Re: [julia-users] for loops

2014-05-15 Thread Mike Innes
Well, if you want the first syntax you can easily define

Base.enumerate(f::Function, args...) = map(t->f(t...), enumerate(args...))

You could always open a pull request if you wanted to see this in Base, too.

On Thursday, 15 May 2014 21:18:31 UTC+1, Cameron McBride wrote:
>
> I missed enumerate() for a while,  and was happy I found it.  I find it 
> amusing how satisfying a few missing keystrokes can be.  
>
> On a related but different note, from a similar influence, I keep wanting 
> to pass blocks to iterators.  Any chance that will ever happen?
>
> I realize that do..end blocks are used currently as syntactic sugar for 
> methods that take a function as the first arg (e.g. open(), map()), and the 
> same functionality can be achieved with three letters and two braces (map), 
> but it still seems somewhat cleaner to write: 
>
> enumerate(a) do i,x
> ...
> end
>
> over
>  
> map(enumerate(a)) do i,x
> ...
> end
>
> which are really just equivalent, as we know, to
>
> for i,x, in enumerate(a) 
> ...
> end
>
> Are there technical reasons this is a bad idea to assume?
>
> Cameron
>
> On Thu, May 15, 2014 at 1:01 PM, John Myles White 
> 
> > wrote:
>
>> I kind of suspect Stefan, like me, would instinctively call this 
>> operation `each_with_index`.
>>
>>  -- John
>>
>> On May 15, 2014, at 6:33 AM, Kevin Squire > 
>> wrote:
>>
>> One nice thing about Julia is that she borrows many (though not all) good 
>> ideas from other languages. In this case, enumerate came from Python 
>> (although it likely has other incarnations).
>>
>> Cheers!
>>Kevin 
>>
>> On Thursday, May 15, 2014, Billou Bielour > 
>> wrote:
>>
>>> I was thinking the same thing the other day, when using *for x in xs* I 
>>> often find myself needing an index at some point and then I have to change 
>>> the for loop, or write an index manually.
>>>
>>> Enumerate is exactly what I need in this case. 
>>>
>>> +1 for Julia
>>>
>>>
>>
>

Re: [julia-users] for loops

2014-05-15 Thread Cameron McBride
I missed enumerate() for a while,  and was happy I found it.  I find it
amusing how satisfying a few missing keystrokes can be.

On a related but different note, from a similar influence, I keep wanting
to pass blocks to iterators.  Any chance that will ever happen?

I realize that do..end blocks are used currently as syntactic sugar for
methods that take a function as the first arg (e.g. open(), map()), and the
same functionality can be achieved with three letters and two braces (map),
but it still seems somewhat cleaner to write:

enumerate(a) do i,x
...
end

over

map(enumerate(a)) do i,x
...
end

which are really just equivalent, as we know, to

for i,x, in enumerate(a)
...
end

Are there technical reasons this is a bad idea to assume?

Cameron

On Thu, May 15, 2014 at 1:01 PM, John Myles White
wrote:

> I kind of suspect Stefan, like me, would instinctively call this operation
> `each_with_index`.
>
>  -- John
>
> On May 15, 2014, at 6:33 AM, Kevin Squire  wrote:
>
> One nice thing about Julia is that she borrows many (though not all) good
> ideas from other languages. In this case, enumerate came from Python
> (although it likely has other incarnations).
>
> Cheers!
>Kevin
>
> On Thursday, May 15, 2014, Billou Bielour  wrote:
>
>> I was thinking the same thing the other day, when using *for x in xs* I
>> often find myself needing an index at some point and then I have to change
>> the for loop, or write an index manually.
>>
>> Enumerate is exactly what I need in this case.
>>
>> +1 for Julia
>>
>>
>


Re: [julia-users] for loops

2014-05-15 Thread John Myles White
I kind of suspect Stefan, like me, would instinctively call this operation 
`each_with_index`.

 -- John

On May 15, 2014, at 6:33 AM, Kevin Squire  wrote:

> One nice thing about Julia is that she borrows many (though not all) good 
> ideas from other languages. In this case, enumerate came from Python 
> (although it likely has other incarnations).
> 
> Cheers!
>Kevin 
> 
> On Thursday, May 15, 2014, Billou Bielour  wrote:
> I was thinking the same thing the other day, when using for x in xs I often 
> find myself needing an index at some point and then I have to change the for 
> loop, or write an index manually.
> 
> Enumerate is exactly what I need in this case. 
> 
> +1 for Julia
> 



Re: [julia-users] for loops

2014-05-15 Thread Yakir Gagnon
Love the Lazy thing, I'm real tempted to use lazy more, will do once I'm
more sure of what I'm doing.


Yakir Gagnon
The Queensland Brain Institute (Building #79)
The University of Queensland
Brisbane QLD 4072
Australia

cell +61 (0)424 393 332
work +61 (0)733 654 089


On Thu, May 15, 2014 at 11:39 PM, Yakir Gagnon <12.ya...@gmail.com> wrote:

> OMG. So awesome! Thanks!!!
>
>
> Yakir Gagnon
> The Queensland Brain Institute (Building #79)
> The University of Queensland
> Brisbane QLD 4072
> Australia
>
> cell +61 (0)424 393 332
> work +61 (0)733 654 089
>
>
> On Thu, May 15, 2014 at 11:33 PM, Kevin Squire wrote:
>
>> One nice thing about Julia is that she borrows many (though not all) good
>> ideas from other languages. In this case, enumerate came from Python
>> (although it likely has other incarnations).
>>
>> Cheers!
>> Kevin
>>
>>
>> On Thursday, May 15, 2014, Billou Bielour 
>> wrote:
>>
>>> I was thinking the same thing the other day, when using *for x in xs* I
>>> often find myself needing an index at some point and then I have to change
>>> the for loop, or write an index manually.
>>>
>>> Enumerate is exactly what I need in this case.
>>>
>>> +1 for Julia
>>>
>>>
>


Re: [julia-users] for loops

2014-05-15 Thread Yakir Gagnon
OMG. So awesome! Thanks!!!


Yakir Gagnon
The Queensland Brain Institute (Building #79)
The University of Queensland
Brisbane QLD 4072
Australia

cell +61 (0)424 393 332
work +61 (0)733 654 089


On Thu, May 15, 2014 at 11:33 PM, Kevin Squire wrote:

> One nice thing about Julia is that she borrows many (though not all) good
> ideas from other languages. In this case, enumerate came from Python
> (although it likely has other incarnations).
>
> Cheers!
>Kevin
>
>
> On Thursday, May 15, 2014, Billou Bielour  wrote:
>
>> I was thinking the same thing the other day, when using *for x in xs* I
>> often find myself needing an index at some point and then I have to change
>> the for loop, or write an index manually.
>>
>> Enumerate is exactly what I need in this case.
>>
>> +1 for Julia
>>
>>


Re: [julia-users] for loops

2014-05-15 Thread Mike Innes
You may also be interested in zip:

for (file, i) in zip(files, 1:length(files))
  println(file, " ", i)
end

# Even better:
using Lazy

for (file, i) in zip(files, range())
  println(file, " ", i)
end

Enumerate is definitely the best solution here, but zip is more general if 
you find yourself bumping against similar problems.


On Thursday, 15 May 2014 14:00:09 UTC+1, Michele Zaffalon wrote:
>
> What about enumerate: 
> http://docs.julialang.org/en/latest/stdlib/base/?highlight=enumerate#Base.enumerate?
>
>
> On Thu, May 15, 2014 at 2:48 PM, Yakir Gagnon <12.y...@gmail.com
> > wrote:
>
>> I love the 
>> for file in files
>> ... do something with file ...
>> end
>>
>> syntax. But sometimes it's really useful to be able to have an iterator 
>> accessible in the for loop, like:
>>
>> for file in files
>> ... do something with file ...
>> ... and with i that equals find(file == files) ...
>> end
>>
>>
>> Is there something built in like that, other than the usual way of:
>>
>> for i = 1:length(files)
>> ... do something with files(i) ...
>> ... and with i ...
>> end
>>
>> ?
>>
>
>

Re: [julia-users] for loops

2014-05-15 Thread Kevin Squire
One nice thing about Julia is that she borrows many (though not all) good
ideas from other languages. In this case, enumerate came from Python
(although it likely has other incarnations).

Cheers!
   Kevin

On Thursday, May 15, 2014, Billou Bielour  wrote:

> I was thinking the same thing the other day, when using *for x in xs* I
> often find myself needing an index at some point and then I have to change
> the for loop, or write an index manually.
>
> Enumerate is exactly what I need in this case.
>
> +1 for Julia
>
>


Re: [julia-users] for loops

2014-05-15 Thread Billou Bielour
I was thinking the same thing the other day, when using *for x in xs* I 
often find myself needing an index at some point and then I have to change 
the for loop, or write an index manually.

Enumerate is exactly what I need in this case. 

+1 for Julia



Re: [julia-users] for loops

2014-05-15 Thread Michele Zaffalon
What about enumerate:
http://docs.julialang.org/en/latest/stdlib/base/?highlight=enumerate#Base.enumerate?


On Thu, May 15, 2014 at 2:48 PM, Yakir Gagnon <12.ya...@gmail.com> wrote:

> I love the
> for file in files
> ... do something with file ...
> end
>
> syntax. But sometimes it's really useful to be able to have an iterator
> accessible in the for loop, like:
>
> for file in files
> ... do something with file ...
> ... and with i that equals find(file == files) ...
> end
>
>
> Is there something built in like that, other than the usual way of:
>
> for i = 1:length(files)
> ... do something with files(i) ...
> ... and with i ...
> end
>
> ?
>