How to resolve the algorithm Multiple distinct objects step by step in the Nim programming language
How to resolve the algorithm Multiple distinct objects step by step in the Nim programming language
Table of Contents
Problem Statement
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). See also: Closures/Value capture
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multiple distinct objects step by step in the Nim programming language
Source code in the nim programming language
import sequtils, strutils
# Creating a sequence containing sequences of integers.
var s1 = newSeq[seq[int]](5)
for item in s1.mitems: item = @[1]
echo "s1 = ", s1 # @[@[1], @[1], @[1], @[1], @[1]]
s1[0].add 2
echo "s1 = ", s1 # @[@[1, 2], @[1], @[1], @[1], @[1]]
# Using newSeqWith.
var s2 = newSeqWith(5, @[1])
echo "s2 = ", s2 # @[@[1], @[1], @[1], @[1], @[1]]
s2[0].add 2
echo "s2 = ", s2 # @[@[1, 2], @[1], @[1], @[1], @[1]]
# Creating a sequence containing pointers.
proc newInt(n: int): ref int =
new(result)
result[] = n
var s3 = newSeqWith(5, newInt(1))
echo "s3 contains references to ", s3.mapIt(it[]).join(", ") # 1, 1, 1, 1, 1
s3[0][] = 2
echo "s3 contains references to ", s3.mapIt(it[]).join(", ") # 2, 1, 1, 1, 1
# How to create non distinct elements.
let p = newInt(1)
var s4 = newSeqWith(5, p)
echo "s4 contains references to ", s4.mapIt(it[]).join(", ") # 1, 1, 1, 1, 1
s4[0][] = 2
echo "s4 contains references to ", s4.mapIt(it[]).join(", ") # 2, 2, 2, 2, 2
You may also check:How to resolve the algorithm Named parameters step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Multi-dimensional array step by step in the D programming language
You may also check:How to resolve the algorithm Subtractive generator step by step in the Python programming language
You may also check:How to resolve the algorithm Substring step by step in the HicEst programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Fortran programming language