[julia-users] Re: Using generators

2016-10-03 Thread harven
Thanks for the answers, the examples are enlightening. I thought at first 
that a generator could be used whenever an array was expected, I get it now.



Re: [julia-users] Re: Using generators

2016-10-03 Thread Stefan Karpinski
There's nothing you can do with comprehensions or generators that you can't
do with for loops or functional programming. But there are situations where
they are considerably more convenient syntactically. Consider a case where
you have several nested for clauses and a filter clause involving all of
those variables, such as:

julia> [(a,b,c,d) for a=1:n for b=a:n for c=b:n for d=c:n if a*d < b*c]
418-element Array{Tuple{Int64,Int64,Int64,Int64},1}:
 (1,2,2,2)
 (1,2,2,3)
 (1,2,3,3)
 (1,2,3,4)
 (1,2,3,5)
 ⋮
 (8,9,9,9)
 (8,9,9,10)
 (8,9,10,10)
 (8,10,10,10)
 (9,10,10,10)


That's really annoying to express with loops or functional constructs.
Lambdas force you to keep giving the variables names over and over, even
though you really just want to use the same names throughout the
construction. The flattened nested iteration can be expressed with higher
order constructs, but it's a serious pain to write and not pretty.

The same argument applies to generators, except that you'll want to use
them in cases where you'd like to iterate a complex set of values like that
without actually constructing an intermediate array of values. For example,
let's say you wanted to make an IntSet of the possible values of a*d +
b*c in the above cases:

julia> IntSet(a*d + b*c for a=1:n for b=a:n for c=b:n for d=c:n if a*d <
b*c)
IntSet([6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 88, 89, 90, 91, 92, 93, 94, 96, 97, 99, 100, 101, 102,
103, 104, 106, 108, 109, 110, 111, 112, 113, 114, 117, 118, 120, 121, 122,
123, 124, 126, 127, 130, 131, 132, 135, 140, 141, 142, 144, 150, 151, 153,
160, 161, 170, 180, 190])


Instead of creating an intermediate set of all tuples and then passing that
to the IntSet, this iterates through the possible values, inserting them
into the IntSet as it goes. Since there are only 127 of these values, it's
more efficient.

On Sun, Oct 2, 2016 at 8:23 PM, Steven G. Johnson 
wrote:

>
>
> On Sunday, October 2, 2016 at 6:02:13 AM UTC-4, harven wrote:
>>
>> I see that julia v0.5 has now generators. That looks promising but the
>> example given in the docs is not really interesting,
>>
>>   sum(1/n^2 for n = 1:1000)
>>
>> since we have been able to write from start the shorter  and I think as
>> efficient
>>
>>   sum(n->1/n^2, 1:1000)
>>
>> My question is, where may/should I use generators?
>>
>
> Generator expressions produce an iterator, so they work with any function
> that accepts iterables.
>


[julia-users] Re: Using generators

2016-10-02 Thread Steven G. Johnson


On Sunday, October 2, 2016 at 6:02:13 AM UTC-4, harven wrote:
>
> I see that julia v0.5 has now generators. That looks promising but the 
> example given in the docs is not really interesting,
>
>   sum(1/n^2 for n = 1:1000)
>
> since we have been able to write from start the shorter  and I think as 
> efficient
>
>   sum(n->1/n^2, 1:1000)
>
> My question is, where may/should I use generators?
>

Generator expressions produce an iterator, so they work with any function 
that accepts iterables.