Multiple distinct objects
You are encouraged to solve this task according to the task description, using any language you may know.
Create a sequence (array, list, whatever) consisting of n distinct, initialized items of the same type. n should be determined at runtime.
By distinct we mean that if they are mutable, changes to one do not affect all others; if there is an appropriate equality operator they are considered unequal; etc. The code need not specify a particular kind of distinction, but do not use e.g. a numeric-range generator which does not generalize.
By initialized we mean that each item must be in a well-defined state appropriate for its type, rather than e.g. arbitrary previous memory contents in an array allocation. Do not show only an initialization technique which initializes only to "zero" values (e.g. calloc() or int a[n] = {}; in C), unless user-defined types can provide definitions of "zero" for that type.
This task was inspired by the common error of intending to do this, but instead creating a sequence of n references to the same mutable object; it might be informative to show the way to do that as well, both as a negative example and as how to do it when that's all that's actually necessary.
This task is most relevant to languages operating in the pass-references-by-value style (most object-oriented, garbage-collected, and/or 'dynamic' languages).
[edit] Ada
A : array (1..N) of T;
Here N can be unknown until run-time. T is any constrained type. In Ada all objects are always initialized, though some types may have null initialization. When T requires a non-null initialization, it is done for each array element. For example, when T is a task type, N tasks start upon initialization of A. Note that T can be a limited type like task. Limited types do not have predefined copy operation. Arrays of non-limited types can also be initialized by aggregates of:
A : array (1..N) of T := (others => V);
Here V is some value or expression of the type T. As an expression V may have side effects, in that case it is evaluated exactly N times, though the order of evaluation is not defined. Also an aggregate itself can be considered as a solution of the task:
(1..N => V)
[edit] Aime
void
show_sublist(list l)
{
integer i;
i = 0;
while (i < l_length(l)) {
if (i) {
o_space(1);
}
o_integer(l_q_integer(l, i));
i += 1;
}
}
void
show_list(list l)
{
integer i;
i = 0;
while (i < l_length(l)) {
o_text(" [");
show_sublist(l_q_list(l, i));
o_text("]");
i += 1;
}
o_byte('\n');
}
list
multiple_distinct(integer n, object o)
{
list l;
while (n) {
l_append(l, o);
n -= 1;
}
return l;
}
integer
main(void)
{
list l, z;
# create a list of integers - `3' will serve as initializer
l = multiple_distinct(8, 3);
l_clear(l);
# create a list of distinct lists - `z' will serve as initializer
l_append(z, 4);
l = multiple_distinct(8, z);
# modify one of the sublists
l_r_integer(l_q_list(l, 3), 0, 7);
# display the list of lists
show_list(l);
return 0;
}
- Output:
[4] [4] [4] [7] [4] [4] [4] [4]
[edit] ALGOL 68
MODE FOO = STRUCT(CHAR u,l);
INT n := 26;
[n]FOO f;
# Additionally each item can be initialised #
FOR i TO UPB f DO f[i] := (REPR(ABS("A")-1+i), REPR(ABS("a")-1+i)) OD;
print((f, new line))
Output:
AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
[edit] AutoHotkey
a := []
Loop, %n%
a[A_Index] := new Foo()
[edit] BBC BASIC
REM Determine object count at runtime:
n% = RND(1000)
REM Declare an array of structures; all members are initialised to zero:
DIM objects{(n%) a%, b$}
REM Initialise the objects to distinct values:
FOR i% = 0 TO DIM(objects{()},1)
objects{(i%)}.a% = i%
objects{(i%)}.b$ = STR$(i%)
NEXT
REM This is how to create an array of pointers to the same object:
DIM objects%(n%), object{a%, b$}
FOR i% = 0 TO DIM(objects%(),1)
objects%(i%) = object{}
NEXT
[edit] Brat
The wrong way, which creates an array of n references to the same new foo:
n.of foo.new
The right way, which calls the block n times and creates an array of new foos:
n.of { foo.new }
[edit] C
foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
init_foo(&foos[i]);
(Or if no particular initialization is needed, skip that part, or use calloc.)
[edit] C++
By default C++ has value semantics, so this problem does not present itself unless the programmer deliberately choses to refer to objects though a pointer. Examples are given for both cases.
Using only language primitives:
// this assumes T is a default-constructible type (all built-in types are)
T* p = new T[n]; // if T is POD, the objects are uninitialized, otherwise they are default-initialized
//If default initialisation is not what you want, or if T is a POD type which will be uninitialized
for(size_t i = 0; i != n; ++i)
p[i] = make_a_T(); //or some other expression of type T
// when you don't need the objects any more, get rid of them
delete[] p;
Using the standard library
#include <vector>
#include <algorithm>
#include <iterator>
// this assumes T is default-constructible
std::vector<T> vec1(n); // all n objects are default-initialized
// this assumes t is a value of type T (or a type which implicitly converts to T)
std::vector<T> vec2(n, t); // all n objects are copy-initialized with t
// To initialise each value differently
std::generate_n(std::back_inserter(vec), n, makeT); //makeT is a function of type T(void)
In C++ reference semantics are achieved by holding objects by pointer. Here is an example of the error, and a correct way of achieving distinctness.
These examples assume T has a public copy constructor, and that p is a pointer to T;
#include <vector>
#include <tr1/memory>
using namespace std;
using namespace std::tr1;
typedef shared_ptr<T> TPtr_t;
// the following is NOT correct:
std::vector<TPtr_t > bvec_WRONG(n, p); // create n copies of p, which all point to the same opject p points to.
// nor is this:
std::vector<TPtr_t> bvec_ALSO_WRONG(n, TPtr_t(new T(*p)) ); // create n pointers to a single copy of *p
// the correct solution
std::vector<TPtr_t > bvec(n);
for (int i = 0; i < n; ++i)
bvec[i] = TPtr_t(new T(*p); //or any other call to T's constructor
// another correct solution
// this solution avoids uninitialized pointers at any point
std::vector<TPtr_t> bvec2;
for (int i = 0; i < n; ++i)
bvec2.push_back(TPtr_t(new T(*p));
Of course, also in this case one can use the other sequence containers or plain new/delete instead of vector.
[edit] C#
using System;
using System.Linq;
using System.Collections.Generic;
List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();
[edit] Clojure
An example using pseudo-random numbers:
user> (take 3 (repeat (rand))) ; repeating the same random number three times
(0.2787011365537204 0.2787011365537204 0.2787011365537204)
user> (take 3 (repeatedly rand)) ; creating three different random number
(0.8334795669220695 0.08405601245793926 0.5795448744634744)
user>
[edit] Common Lisp
The mistake is often written as one of these:
(make-list n :initial-element (make-the-distinct-thing))
(make-array n :initial-element (make-the-distinct-thing))
which are incorrect since the form (make-the-distinct-thing) is only evaluated once and the single object is put in every position of the sequence. A commonly used correct version is:
(loop repeat n collect (make-the-distinct-thing))
which evaluates (make-the-distinct-thing) n times and collects each result in a list.
It is also possible to use map-into, the destructive map operation, to do this since it may take zero input sequences; this method can produce any sequence type, such as a vector (array) rather than a list, and takes a function rather than a form to specify the thing created:
(map-into (make-list n) #'make-the-distinct-thing)
(map-into (make-array n) #'make-the-distinct-thing)
[edit] D
For reference types (classes):
auto fooArray = new Foo[n];
foreach (ref item; fooArray)
item = new Foo();
For value types:
auto barArray = new Bar[n];
barArray[] = initializerValue;
[edit] Delphi
Same object accessed multiple times (bad)
var
i: Integer;
lObject: TMyObject;
lList: TObjectList<TMyObject>;
begin
lList := TObjectList<TMyObject>.Create;
lObject := TMyObject.Create;
for i := 1 to 10 do
lList.Add(lObject);
// ...
Distinct objects (good)
var
i: Integer;
lList: TObjectList<TMyObject>;
begin
lList := TObjectList<TMyObject>.Create;
for i := 1 to 10 do
lList.Add(TMyObject.Create);
// ...
[edit] E
E needs development of better map/filter/stream facilities. The easiest way to do this so far is with the accumulator syntax, which is officially experimental because we're not satisfied with it as yet.pragma.enable("accumulator")
...
accum [] for _ in 1..n { _.with(makeWhatever()) }
[edit] Factor
clone is the important word here to have distinct objects. This creates an array of arrays.
1000 [ { 1 } clone ] replicate
[edit] Fortran
program multiple
! Define a simple type
type T
integer :: a = 3
end type T
! Define a type containing a pointer
type S
integer, pointer :: a
end type S
type(T), allocatable :: T_array(:)
type(S), allocatable :: S_same(:)
integer :: i
integer, target :: v
integer, parameter :: N = 10
! Create 10
allocate(T_array(N))
! Set the fifth one to b something different
T_array(5)%a = 1
! Print them out to show they are distinct
write(*,'(10i2)') (T_array(i),i=1,N)
! Create 10 references to the same object
allocate(S_same(N))
v = 5
do i=1, N
allocate(S_same(i)%a)
S_same(i)%a => v
end do
! Print them out - should all be 5
write(*,'(10i2)') (S_same(i)%a,i=1,N)
! Change the referenced object and reprint - should all be 3
v = 3
write(*,'(10i2)') (S_same(i)%a,i=1,N)
end program multiple
[edit] F#
The wrong way:
>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]
The right way:
> 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]
[edit] Go
Useful:
func nxm(n, m int) [][]int {
d2 := make([][]int, n)
for i := range d2 {
d2[i] = make([]int, m)
}
return d2
}
Probably not what the programmer wanted:
func nxm(n, m int) [][]int {
d1 := make([]int, m)
d2 := make([][]int, n)
for i := range d2 {
d2[i] = d1
}
return d2
}
[edit] Haskell
Below, we are assuming that makeTheDistinctThing is a monadic expression (i.e. it has type m a where m is some monad, like IO or ST), and we are talking about distinctness in the context of the monad. Otherwise, this task is pretty meaningless in Haskell, because Haskell is referentially transparent (so two values that are equal to the same expression are necessarily not distinct) and all values are immutable.
replicateM n makeTheDistinctThing
in an appropriate do block. If it is distinguished by, say, a numeric label, one could write
mapM makeTheDistinctThing [1..n]
An incorrect version:
do x <- makeTheDistinctThing
return (replicate n x)
[edit] Icon and Unicon
An incorrect approach uses, e.g., the list constructor procedure with an initial value:
items_wrong := list (10, [])
# prints '0' for size of each item
every item := !items_wrong do write (*item)
# after trying to add an item to one of the lists
push (items_wrong[1], 2)
# now prints '1' for size of each item
every item := !items_wrong do write (*item)
A correct approach initialises each element separately:
items := list(10)
every i := 1 to 10 do items[i] := []
[edit] J
i.
J almost always uses pass-by-value, so this topic is not very relevant to J.
[edit] Java
simple array:
Foo[] foos = new Foo[n]; // all elements initialized to null
for (int i = 0; i < foos.length; i++)
foos[i] = new Foo();
// incorrect version:
Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo()); // new Foo() only evaluated once
simple list:
List<Foo> foos = new ArrayList<Foo>();
for (int i = 0; i < n; i++)
foos.add(new Foo());
// incorrect:
List<Foo> foos_WRONG = Collections.nCopies(n, new Foo()); // new Foo() only evaluated once
Generic version for class given at runtime:
It's not pretty but it gets the job done. The first method here is the one that does the work. The second method is a convenience method so that you can pass in a String of the class name. When using the second method, be sure to use the full class name (ex: "java.lang.String" for "String"). InstantiationExceptions will be thrown when instantiating classes that you would not normally be able to call new on (abstract classes, interfaces, etc.). Also, this only works on classes that have a no-argument constructor, since we are using newInstance().
public static <E> List<E> getNNewObjects(int n, Class<? extends E> c){
List<E> ans = new LinkedList<E>();
try {
for(int i=0;i<n;i++)
ans.add(c.newInstance());//can't call new on a class object
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return ans;
}
public static List<Object> getNNewObjects(int n, String className)
throws ClassNotFoundException{
return getNNewObjects(n, Class.forName(className));
}
[edit] JavaScript
var a = new Array(n);
for (var i = 0; i < n; i++)
a[i] = new Foo();
[edit] Mathematica
The mistake is often written as:
{x, x, x, x} /. x -> Random[]
Here Random[] can be any expression that returns a new value which is incorrect since Random[] is only evaluated once. e.g. {0.175125, 0.175125, 0.175125, 0.175125}
A correct version is:
{x, x, x, x} /. x :> Random[]
which evaluates Random[] each time e.g. ->{0.514617, 0.0682395, 0.609602, 0.00177382}
[edit] Maxima
a: [1, 2]$
b: makelist(copy(a), 3);
[[1,2],[1,2],[1,2]]
b[1][2]: 1000$
b;
[[1,1000],[1,2],[1,2]]
[edit] Modula-3
Similar to the Ada version above:
VAR a: ARRAY OF T
This creates an open array (an array who's size is not known until runtime) of distinct elements of type T.
Modula-3 does not define what values the elements of A have, but it does guarantee that they will be of type T.
[edit] OCaml
For arrays:
Incorrect:
Array.make n (new foo);;
(* here (new foo) can be any expression that returns a new object,
record, array, or string *)
which is incorrect since new foo is only evaluated once. A correct version is:
Array.init n (fun _ -> new foo);;
[edit] ooRexx
-- get an array of directory objects
array = fillArrayWith(3, .directory)
say "each object will have a different identityHash"
say
loop d over array
say d~identityHash
end
::routine fillArrayWith
use arg size, class
array = .array~new(size)
loop i = 1 to size
-- Note, this assumes this object class can be created with
-- no arguments
array[i] = class~new
end
return array
[edit] Oz
With lists, it is difficult to do wrong.
declare
Xs = {MakeList 5} %% a list of 5 unbound variables
in
{ForAll Xs OS.rand} %% fill it with random numbers (CORRECT)
{Show Xs}
With arrays on the other hand, it is easy to get wrong:
declare
Arr = {Array.new 0 10 {OS.rand}} %% WRONG: contains ten times the same number
in
%% CORRECT: fill it with ten (probably) different numbers
for I in {Array.low Arr}..{Array.high Arr} do
Arr.I := {OS.rand}
end
[edit] Pascal
See Delphi
[edit] Perl
incorrect:
(Foo->new) x $n
# here Foo->new can be any expression that returns a reference representing
# a new object
which is incorrect since Foo->new is only evaluated once.
A correct version is:
map { Foo->new } 1 .. $n;
which evaluates Foo->new $n times and collects each result in a list.
[edit] Perl 6
Just like in Perl 5, the list repetition operator evaluates the statement only once, so
# WRONG
my @a = Foo.new xx $n;
produces $n references to the same object. This works instead:
my @a = map { Foo.new }, ^$n
[edit] PicoLisp
Create 5 distinct (empty) objects:
: (make (do 5 (link (new))))
-> ($384717187 $384717189 $384717191 $384717193 $384717195)
Create 5 anonymous symbols with the values 1 .. 5:
: (mapcar box (range 1 5))
-> ($384721107 $384721109 $384721111 $384721113 $384721115)
: (val (car @))
-> 1
: (val (cadr @@))
-> 2
[edit] PureBasic
n=Random(50)+25
Dim A.i(n)
; Creates a Array of n [25-75] elements depending on the outcome of Random().
; Each element will be initiated to zero.
For i=0 To ArraySize(A())
A(i)=2*i
Next i
; Set each individual element at a wanted (here 2*i) value and
; automatically adjust accordingly to the unknown length of the Array.
NewList *PointersToA()
For i=0 To ArraySize(A())
AddElement(*PointersToA())
*PointersToA()=@A(i)
Next
; Create a linked list of the same length as A() above.
; Each element is then set to point to the Array element
; of the same order.
ForEach *PointersToA()
Debug PeekI(*PointersToA())
Next
; Verify by sending each value of A() via *PointersToA()
; to the debugger's output.
[edit] Python
The mistake is often written as:
[Foo()] * n # here Foo() can be any expression that returns a new object
which is incorrect since Foo() is only evaluated once. A common correct version is:
[Foo() for i in range(n)]
which evaluates Foo() n times and collects each result in a list. This last form is also discussed here, on the correct construction of a two dimensional array.
[edit] R
The mistake is often written as:
rep(foo(), n) # foo() is any code returning a value
A common correct version is:
replicate(n, foo())
which evaluates foo() n times and collects each result in a list. (Using simplify=TRUE lets the function return an array, where possible.)
[edit] Racket
#lang racket
;; a list of 10 references to the same vector
(make-list 10 (make-vector 10 0))
;; a list of 10 distinct vectors
(build-list 10 (λ (n) (make-vector 10 0)))
[edit] Ruby
The mistake is often written as one of these:
[Foo.new] * n # here Foo.new can be any expression that returns a new object
Array.new(n, Foo.new)
which are incorrect since Foo.new is only evaluated once, and thus you now have n references to the same object. A common correct version is:
Array.new(n) { Foo.new }
which evaluates Foo.new n times and collects each result in an Array. This last form is also discussed here, on the correct construction of a two dimensional array.
[edit] Scala
Yielding a normal class instance here (rather than a case class instance), as case objects are identical if created with the same constructor arguments.
for (i <- (0 until n)) yield new Foo()
[edit] Seed7
The example below defines the local array variable fileArray. The times operator creates a new array value with a specified size. Finally multiple distinct objects are assigned to the array elements.
$ include "seed7_05.s7i";
const func array file: openFiles (in array string: fileNames) is func
result
var array file: fileArray is 0 times STD_NULL; # Define array variable
local
var integer: i is 0;
begin
fileArray := length(fileNames) times STD_NULL; # Array size computed at run-time
for key i range fileArray do
fileArray[i] := open(fileNames[i], "r"); # Assign multiple distinct objects
end for;
end func;
const proc: main is func
local
var array file: files is 0 times STD_NULL;
begin
files := openFiles([] ("abc.txt", "def.txt", "ghi.txt", "jkl.txt"));
end func;
[edit] Smalltalk
|c|
"Create an ordered collection that will grow while we add elements"
c := OrderedCollection new.
"fill the collection with 9 arrays of 10 elements; elements (objects)
are initialized to the nil object, which is a well-defined 'state'"
1 to: 9 do: [ :i | c add: (Array new: 10) ].
"However, let us show a way of filling the arrays with object number 0"
c := OrderedCollection new.
1 to: 9 do: [ :i | c add: ((Array new: 10) copyReplacing: nil withObject: 0) ].
"demonstrate that the arrays are distinct: modify the fourth of each"
1 to: 9 do: [ :i | (c at: i) at: 4 put: i ].
"show it"
c do: [ :e | e printNl ].
[edit] Tcl
Tcl values are implemented using copy-on-write reference semantics with no (exposed) mechanism for determining whether two values are really references to the same value, which makes this task relatively moot. However, in the case where there is a collection of objects it becomes important to perform the construction correctly (i.e., repeatedly) otherwise it is just the name of the object that will be copied when it is written to.
orpackage require TclOO
# The class that we want to make unique instances of
set theClass Foo
# Wrong version; only a single object created
set theList [lrepeat $n [$theClass new]]
# Right version; objects distinct
set theList {}
for {set i 0} {$i<$n} {incr i} {
lappend theList [$theClass new]
}
[edit] XPL0
code Reserve=3, IntIn=10;
char A; int N, I;
[N:= IntIn(8); \get number of items from command line
A:= Reserve(N); \create array of N bytes
for I:= 0 to N-1 do A(I):= I*3; \initialize items with different values
for I:= 0 to N-1 do A:= I*3; \error: "references to the same mutable object"
]
- Programming Tasks
- Object oriented
- Ada
- Aime
- ALGOL 68
- AutoHotkey
- BBC BASIC
- Brat
- C
- C++
- C sharp
- Clojure
- Common Lisp
- D
- Delphi
- E
- E examples needing attention
- Factor
- Fortran
- F Sharp
- Go
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Mathematica
- Maxima
- Modula-3
- Modula-3 examples needing attention
- Examples needing attention
- OCaml
- OoRexx
- Oz
- Pascal
- Perl
- Perl 6
- PicoLisp
- PureBasic
- Python
- R
- Racket
- Ruby
- Scala
- Seed7
- Smalltalk
- Tcl
- TclOO
- XPL0
- GUISS/Omit
- PARI/GP/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit