Hello,
Could you please suggest me a proper way to generate the list of the following values: 1, 1/2, 1/3, ... , 1/1000?
For now I can do it using the following peace of code:
values := (1..1000).toList.map { 1.0f/it.toFloat }
This construction is nice and short but temporary list with 1000 elements is created. Is it possible to avoid creation of temporary objects and preserve short syntax form?
Thanks, Dmitriy
This would work:
values := Float[,] 1000.times |i| { values.add(1.0f / {i+1}.toFloat) }
Is it possible to use With-Blocks here? Something like:
values := Float[,] { 1000.times { add(1.0f / (it + 1).toFloat) } }
Looks like this example confuses fan compiler, because of add ambiguity. Is there any way to resolve this conflict?
add
Andy,
Of course, your solution works, it's also possible to use range here:
values:= Float[,]; (1..1000).each { values.add(1.0f/it.toFloat) }
But it's not a single statement. For example, in python I can use list comprehensions for compact solution:
values = [1.0/i for i in xrange(1, 1000)]
It will be great if Fan can support similar constructions without additional overhead like temporary lists.
Probably it would be simplier just to add some of list methods (like map, find) to range. Or just add nullable arg |Int -> Obj?| to toList method
For the immediate future I think Range.map is best solution. I added this for next build changeset
Range.map
Fan 1.0 will not support list comprehensions. But is something that will almost certainly be tackled for 1.1 (might more like C# LINQ though).
Thanks Brian, this is really good!
Login or Signup to reply.
kds Mon 5 Oct 2009
Hello,
Could you please suggest me a proper way to generate the list of the following values: 1, 1/2, 1/3, ... , 1/1000?
For now I can do it using the following peace of code:
This construction is nice and short but temporary list with 1000 elements is created. Is it possible to avoid creation of temporary objects and preserve short syntax form?
Thanks, Dmitriy
andy Mon 5 Oct 2009
This would work:
Yuri Strot Tue 6 Oct 2009
Is it possible to use With-Blocks here? Something like:
Looks like this example confuses fan compiler, because of
add
ambiguity. Is there any way to resolve this conflict?kds Tue 6 Oct 2009
Andy,
Of course, your solution works, it's also possible to use range here:
But it's not a single statement. For example, in python I can use list comprehensions for compact solution:
It will be great if Fan can support similar constructions without additional overhead like temporary lists.
ivan Tue 6 Oct 2009
Probably it would be simplier just to add some of list methods (like map, find) to range. Or just add nullable arg |Int -> Obj?| to toList method
brian Wed 7 Oct 2009
For the immediate future I think
Range.map
is best solution. I added this for next build changesetFan 1.0 will not support list comprehensions. But is something that will almost certainly be tackled for 1.1 (might more like C# LINQ though).
kds Wed 7 Oct 2009
Thanks Brian, this is really good!