Multiple distinct objects: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added Rust) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 237:
(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++}}==
Line 296 ⟶ 304:
</lang>
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}}==
Line 397:
Randoms = [random:uniform(1000) || _ <- lists:seq(1,10)].
</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}}==
Line 432 ⟶ 447:
list each: drop . 1301600
</lang>
=={{header|Fortran}}==
Line 481 ⟶ 495:
</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}}==
Useful:
Line 664:
<lang JavaScript>[{"index":0}, {"index":1}, {"index":2}, {"index":3},
{"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}}==
Line 697 ⟶ 686:
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}}==
Line 1,044:
<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.
=={{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}}==
Line 1,170 ⟶ 1,162:
(build-list 10 (λ (n) (make-vector 10 0)))
</lang>
(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}}==
Line 1,236 ⟶ 1,237:
(0 1 2 3 4)
</pre>
=={{header|Seed7}}==
|