How to resolve the algorithm Deepcopy step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Deepcopy step by step in the Wren programming language

Table of Contents

Problem Statement

Demonstrate how to copy data structures containing complex heterogeneous and cyclic semantics. This is often referred to as deep copying, and is normally required where structures are mutable and to ensure that independent copies can be manipulated without side-effects. If this facility is not built into the language, it is permissible to use functions from a common library, or a coded procedure.

The task should show:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Deepcopy step by step in the Wren programming language

Source code in the wren programming language

import "./trait" for Cloneable, CloneableSeq
import "./seq" for Lst

class MyMap is Cloneable {
    construct new (m) {
        if (m.type != Map) Fiber.abort("Argument must be a Map.")
        _m = m
    }

    m { _m }

    toString { _m.toString }

    clone() {
        // Map keys are always immutable built-in types so we only need to worry about
        // their values which can be anything.
        var m2 = {}
        for (me in _m) {
            var v = me.value
            m2[me.key] = (v is List) ? Lst.clone(v) :
                         (v is Cloneable || v is CloneableSeq) ? v.clone() : v
        }
        return MyMap.new(m2)
    }
}

var my = MyMap.new({"a": 0, "b": 1, "c": [2, 3], "d": MyMap.new({"e": 4})})
var my2 = my.clone()
System.print("Before any changes:")
System.print("  my  = %(my)")
System.print("  my2 = %(my2)")
// now change my2
my2.m["a"] = 5
my2.m["b"] = 6
my2.m["c"][0] = 7
my2.m["c"][1] = 8
my2.m["d"].m["e"] = 9
my2.m["d"].m["f"] = 10
System.print("\nAfter changes to my2:")
System.print("  my  = %(my)")
System.print("  my2 = %(my2)")


  

You may also check:How to resolve the algorithm Successive prime differences step by step in the Prolog programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Rust programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Go programming language
You may also check:How to resolve the algorithm Permutation test step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Digital root step by step in the NetRexx programming language