Multiple distinct objects: Difference between revisions

Content added Content deleted
(Added Rust)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 237: Line 237:


(Or if no particular initialization is needed, skip that part, or use <tt>calloc</tt>.)
(Or if no particular initialization is needed, skip that part, or use <tt>calloc</tt>.)

=={{header|C sharp|C#}}==

<lang csharp>using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 296: Line 304:
</lang>
</lang>
Of course, also in this case one can use the other sequence containers or plain new/delete instead of <tt>vector</tt>.
Of course, also in this case one can use the other sequence containers or plain new/delete instead of <tt>vector</tt>.

=={{header|C sharp|C#}}==

<lang csharp>using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 397: Line 397:
Randoms = [random:uniform(1000) || _ <- lists:seq(1,10)].
Randoms = [random:uniform(1000) || _ <- lists:seq(1,10)].
</pre>
</pre>

=={{header|F_Sharp|F#}}==
The wrong way:
<lang fsharp>>List.replicate 3 (System.Guid.NewGuid());;

val it : Guid list =
[485632d7-1fd6-4d9e-8910-7949d7b2b485; 485632d7-1fd6-4d9e-8910-7949d7b2b485;
485632d7-1fd6-4d9e-8910-7949d7b2b485]</lang>

The right way:
<lang fsharp>> List.init 3 (fun _ -> System.Guid.NewGuid());;

val it : Guid list =
[447acb0c-092e-4f85-9c3a-d369e4539dae; 5f41c04d-9bc0-4e96-8165-76b41fe8cd93;
1086400c-72ff-4763-9bb9-27e17bd4c7d2]</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 432: Line 447:
list each: drop . 1301600
list each: drop . 1301600
</lang>
</lang>



=={{header|Fortran}}==
=={{header|Fortran}}==
Line 481: Line 495:
</lang>
</lang>


=={{header|F_Sharp|F#}}==
The wrong way:
<lang fsharp>>List.replicate 3 (System.Guid.NewGuid());;

val it : Guid list =
[485632d7-1fd6-4d9e-8910-7949d7b2b485; 485632d7-1fd6-4d9e-8910-7949d7b2b485;
485632d7-1fd6-4d9e-8910-7949d7b2b485]</lang>

The right way:
<lang fsharp>> List.init 3 (fun _ -> System.Guid.NewGuid());;

val it : Guid list =
[447acb0c-092e-4f85-9c3a-d369e4539dae; 5f41c04d-9bc0-4e96-8165-76b41fe8cd93;
1086400c-72ff-4763-9bb9-27e17bd4c7d2]</lang>
=={{header|Go}}==
=={{header|Go}}==
Useful:
Useful:
Line 664: Line 664:
<lang JavaScript>[{"index":0}, {"index":1}, {"index":2}, {"index":3},
<lang JavaScript>[{"index":0}, {"index":1}, {"index":2}, {"index":3},
{"index":4}, {"index":5}, {"index":6}]</lang>
{"index":4}, {"index":5}, {"index":6}]</lang>

=={{header|Julia}}==
A potential mistake would be writing:
<lang Julia>
foo() = rand() # repeated calls change the result with each call
repeat([foo()], outer=5) # but this only calls foo() once, clones that first value
</lang>
If the effect of calling foo() with every iteration is desired, better to use:
<lang Julia>
[foo() for i in 1:5] # Code this to call the function within each iteration
</lang>


=={{header|jq}}==
=={{header|jq}}==
Line 697: Line 686:


Array("object"; 4)</lang>
Array("object"; 4)</lang>

=={{header|Julia}}==
A potential mistake would be writing:
<lang Julia>
foo() = rand() # repeated calls change the result with each call
repeat([foo()], outer=5) # but this only calls foo() once, clones that first value
</lang>
If the effect of calling foo() with every iteration is desired, better to use:
<lang Julia>
[foo() for i in 1:5] # Code this to call the function within each iteration
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 1,044: Line 1,044:
<lang perl>map { Foo->new } 1 .. $n;</lang>
<lang perl>map { Foo->new } 1 .. $n;</lang>
which evaluates <tt>Foo->new</tt> <var>$n</var> times and collects each result in a list.
which evaluates <tt>Foo->new</tt> <var>$n</var> times and collects each result in a list.

=={{header|Perl 6}}==

Unlike in Perl 5, the list repetition operator evaluates the left argument thunk each time, so

<lang perl6>my @a = Foo.new xx $n;</lang>

produces <code>$n</code> distinct objects.


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,170: Line 1,162:
(build-list 10 (λ (n) (make-vector 10 0)))
(build-list 10 (λ (n) (make-vector 10 0)))
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)

Unlike in Perl 5, the list repetition operator evaluates the left argument thunk each time, so

<lang perl6>my @a = Foo.new xx $n;</lang>

produces <code>$n</code> distinct objects.


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 1,236: Line 1,237:
(0 1 2 3 4)
(0 1 2 3 4)
</pre>
</pre>



=={{header|Seed7}}==
=={{header|Seed7}}==