seamless-immutable array property is not immutable - seamless-immutable

I wonder if there is a way to convert an object using seamless-immutable where the resultant immutable's array properties are also immutable, currently
a = {hey: [1,2,3], ho: {hi:'there'}}
ia = Immutable(a)
ia // immutable
ia.hey // not immutable
ia.ho //immutable
what I want is ia.hey, an array, being immutable as well, does anyone know how ?

Unless I'm misunderstanding your question, you can accomplish what you're asking about by using seamless-immutable in development mode. See here:
https://github.com/rtfeldman/seamless-immutable#performance
In the development build, objects are frozen. (Note that Safari is relatively slow to iterate over frozen objects.) The development build also overrides unsupported methods (methods that ordinarily mutate the underlying data structure) to throw helpful exceptions.
As it notes, this comes with a performance penalty (which is actually quite significant, but most likely not relevant in most scenarios).
See this JSFiddle: https://jsfiddle.net/pvqzh9yj/
var array = Immutable(["totally", "immutable", {hammer: "Can’t Touch This"}]);
array[1] = "I'm going to mutate you!";
array[2].hammer = "hm, surely I can mutate this nested object...";
for (var index in array) { console.log(array[index]); }

Related

Why Val and Var behave same for arrays in koltin?

fun Sample(){
val x = 10
x = 11 // will give error as it cannot be reassigned
val arr = arrayOf(1,2,3)
arr[0] = 5 // will not give any error but why ? aren't they supposed to be final?
}
Why val doesn't works for arrays?
arr itself is immutable and can't be reassigned....the contents of array are not. For example you could not do:
val arr1 = arrayOf(1,2,3)
val arr2 = arrayOf(4,5,6)
arr1 = arr2 // error
An alternative if you want a "read-only" list is to use listOf()
By declaring and array as with val, you basically declare the reassigning for the array isn't possible, but array items are still reassignable
vals aren't really immutable data, it's better to think of them as read-only references. You can change the value of a var, but you can't change the thing a val points at. But that doesn't mean the thing itself can't change!
If the object itself is mutable, or has mutable var properties, or has val properties that point to other mutable things... you get the idea. You can't guarantee absolute immutable state, it's just not baked into the language (or Java itself - final just means you can't reassign the variable!)
That's why Kotlin has a bunch of features to try and help you avoid mutable state, or at least to prevent you from changing things. Collections (and the return types of the functions that operate on them) are usually immutable by default, and you have to explicitly ask for a mutableList or whatever. That's enforcing immutability by stopping you from accessing methods like add and remove, but it still doesn't stop you from putting mutable objects in there!
data classes are another tool, and that's sort of aimed at a more functional, data-driven approach. The idea is that ideally, you'll just put vals in them, and any non-primitive data types you use will also comprise of vals - so when you drill down, everything ends in immutable variables, so the whole thing is immutable overall. Things like the copy method allow you to "change" the data without actually mutating it, similar to a Copy On Write approach.
I'd also say that in my experience, it's not uncommon for arrays specifically to always be treated as a mutable type, though. Like there's no immutable version of an array in Kotlin, and things like F# (which encourages functional transformations on its types) explicitly has setters on its Array types. So if you need a read-only array... use a List!

How do deal with nested Arrays/objects in BehaviorSubjects, Observables?

I generally have problems using rxjs with nested Objects or Arrays.
My current use-case is this:
{a: [
{b: 0, c:[{d:1}]},
{b: 1, e:[{f: 'someString'}]}
]
Task: Get and set the Observable or value of a,b,c,d,e,f. I also want to be able to subscribe to each property.
I had this Problem in a similar use-case with an Array of BehaviorSubjects:
Efficiently get Observable of an array BehaviorSubjects
I generally have problems to use the basic functionality of nested arrays/objects in rxjs.
The basic functionality I mean includes:
Array:
getting Element by Index
using for of/in on Arrays
setting an Element by Index
push, pop, shift, slice, splice, ...
Object:
getting Value by Property name
going into the nested tree: object.key1.key2.key3[3].key4 ...
setting Value by Property name
assign
for of/in loops
Generally:
Destructuring: e.g.: let [variable1, variable2] = someObject;
Maybe other stuff I forgot.
I dont know if and which functions are possible for which rxjs Objects and which make sense (for example you should be able to set values in an Observable directly). But coming from a background without rxjs, I have trouble to manage my rxjs Objects properly.
I think reason for this besides my lack of knowledge and understanding is, that
a. The rxjs Objects don't provide the functionality as I'm used to from normal arrays and objects. e.g.:
let variable1 = array[1].property;
//becomes this (see related stack-Question I mentioned earlier)
let variable2 = array.pipe(mergeMap(d=> d[index].pipe(map(d1 => d1[property]));
// -> what happens here? You first need to know what mergeMap,
// map is doing and you have 5 levels of nested inline functions.
b. To implement the those mentioned functionalities I need to go over the .pipe() function and use some function like mergeMap, map, pluck, ... Functions that aren't directly indicating that you can get the Observable of let's say 'e' in my example. Making something like object.a[1].e wierd to implement (at least I don't know how to do that yet)
EDIT:
I also want to note, that I still love the idea of rxjs which works well in angular. I just have problems using it to it's full extend, as I'm a bit new to angular and consequently rxjs.
I thin RX is mainly focus on dealing with async operations. Mutation of array and object we can perfectly use the methods comes natively with javascript if theres no existing operators. or you can create your own operator for mutation/iteration etc.
Will try to answer some of your question on array/objects mutation, they are actually very straight forward.
Array:
getting Element by Index
map(arr=>arr[index])
using for of/in on Arrays
map(arr=>arry.map(item=>....))
setting an Element by Index
tap(arr=>arr[index]=somevalue)
Object:
getting Value by Property name
pluck('name')
going into the nested tree: object.key1.key2.key3[3].key4 ...
pluck('key1','key2')
setting Value by Property name
map(obj=>({a:value,obj...}))
assign
lets say your really want some pick array index method as rxjs operator you can create something like, same as for..in operations.
const pluckIndex=(index)=>source=>source.pipe(map(arr=>arr[index]))
const source = of([2,3])
source.pipe(pluckIndex(1)).subscribe(x => console.log(x));

Custom object change tracking in Typescript

I need to implement change tracking for state of any object.
How would I implement it?
For example:
let complexObject = {
/// ... mant many arrays and propertiess
}
let hash = convertToHash(complexObject)
let trackingArray = []
trackingArray.push(hash);
/// what from here and how to imlement it?
I would recommend you to use https://facebook.github.io/immutable-js/ when you change something you will get a new object. It will be much more efficient than creating a hash because you will not need to iterate the entire object graph.
Also it shares some memory internally so it will be more efficient than storing full clones in memory.
Assuming you are implementing change tracking for purpose of undo/redo functionality.
One way is to use immutable objects and store on stack old object each time when there is action invoked.
In order to capture change to objects you could use https://en.wikipedia.org/wiki/Command_pattern or redux (widely popular in react, implementation for angular).

Normal array in Swift vs 'NSMutableArray'?

So in Swift, what's the difference between
var arr = ["Foo", "Bar"] // normal array in Swift
and
var arr = NSMutableArray.array() // 'NSMutableArray' object
["Foo", "Bar"].map {
arr.addObject($0)
}
other than being different implementations of the same thing.
Both appear to have all the basic features that one might need (.count, the ability to insert/remove objects etc.).
NSMutableArray was invented back in the Obj-C days, obviously to provide a more modern solution instead of the regular C-style arrays. But how does it compare to Swift's built-in array?
Which one is safer and/or faster?
The most important difference, in my opinion, is that NSMutableArray is a class type and Array is a value type. Ergo, an NSMutableArray will be passed as a reference, whereas a Swift Array will be passed by value.
Furthermore NSMutableArray is a subclass of NSObject whereas Array has no parent class. - this means that you get access to all NSObject methods and other 'goodies' when utilising NSMutableArray.
An NSMutableArray will not be copied when you amend it, a Swift Array will be.
Which one is best really depends on your application.
I find (when working with UIKit and Cocoa touch) that NSMutableArray is great when I need a persistent model, whereas Array is great for performance and throw away arrays.
These are just my initial thoughts, I'm sure someone from the community can offer much deeper insight.
Reference Type When:(NSMutableArray)
Subclasses of NSObject must be class types
Comparing instance identity with === makes sense
You want to create shared, mutable state
Value Type When: (Swift array)
Comparing instance data with == makes sense (Equatable protocol)
You want copies to have independent state
The data will be used in code across multiple threads (avoid explicit synchronization)
Interestingly enough, the Swift standard library heavily favors value types:Primitive types (Int, Double, String, …) are value types
Standard collections (Array, Dictionary, Set, …) are value types
Aside from what is illustrated above, the choice really depends on what you are trying to implement. As a rule of thumb, if there is no specific constraint that forces you to opt for a reference type, or you are not sure which option is best for your specific use case, you could start by implementing your data structure using a value type. If needed, you should be able to convert it to a reference type later with relatively little effort.
Conclusion:
Reference types incur more memory overhead, from reference counting and also for storing its data on the heap.
It's worth knowing that copying value types is relatively cheap in Swift,
But it’s important to keep in mind that if your value types become too large, the performance cost of copying can become greater than the cost of using reference types.

How do I resize an array in Scala

I am trying to create a DB management tool in Scala, and I want to be able to draw from this database into Arrays, whose size can shift based on the data being passed to them. I know how to do this in C, PHP, VB, etc. but can't seem to figure out the syntax for Scala.
I'm sure this should be a simple problem, so any help would be appreciated
Collections by default in Scala tend to be immutable. Operations will create new immutable collections from existing collections (by adding/removing elements etc.). The benefit of this is that collections don't change under iteration and writing multi-threaded applications tends to be easier (lots of caveats/assumptions with how you write standard Java apply here!).
Having said all that, if you need a mutable array, have you looked at an ArrayBuffer (a mutable collection with an underlying array implementation) ?
e.g.
val a = new scala.collection.mutable.ArrayBuffer[String]()
a += "A"
a += "B"
a(1) // gives you 'B'
You could use System.copy for this task, if you really want to use an array, or you could directly use a container that will resize itself automatically, such as ListBuffer or ArrayList.

Resources