immutability

Should I represent database data with immutable or mutable data structures?

会有一股神秘感。 提交于 2019-12-04 18:31:50
问题 I'm currently programming in Scala, but I guess this applies to any functional programming language, or rather, any programming language that recommends immutability and can interact with a database. When I fetch data from my database, I map it to a model data structure. In functional programming, data structures tend to be immutable. But the data in a database is mutable, so I wonder whether or not my model should be mutable as well. In general, what would be a good and well-accepted

Efficiently “modifying” an ImmutableMap

萝らか妹 提交于 2019-12-04 18:15:35
问题 We're currently using Guava for its immutable collections but I was surprised to find that their maps don't have methods to easily create new maps with minor modifications. And on top of that, their builder doesn't allow assigning new values to keys, or removing keys. So if I wanted to modify just one value, here's what I would like to be able to do: ImmutableMap<Guid, ImmutableMap<String, Integer>> originalMap = /* get the map */; ImmutableMap<Guid, ImmutableMap<String, Integer>> modifiedMap

When does pandas do pass-by-reference Vs pass-by-value when passing dataframe to a function?

谁都会走 提交于 2019-12-04 17:18:12
def dropdf_copy(df): df = df.drop('y',axis=1) def dropdf_inplace(df): df.drop('y',axis=1,inplace=True) def changecell(df): df['y'][0] = 99 x = pd.DataFrame({'x': [1,2],'y': [20,31]}) x Out[204]: x y 0 1 20 1 2 31 dropdf_copy(x) x Out[206]: x y 0 1 20 1 2 31 changecell(x) x Out[208]: x y 0 1 99 1 2 31 In the above example dropdf() doesnt modify the original dataframe x while changecell() modifies x. I know if I add the minor change to changecell() it wont change x. def changecell(df): df = df.copy() df['y'][0] = 99 I dont think its very elegant to inlcude df = df.copy() in every function I

Which of these are immutable in Python?

送分小仙女□ 提交于 2019-12-04 16:48:48
I am trying to figure out whether the following are immutable in Sage (which is built on Python so I believe if it is immutable in python, I believe in most cases it will be immutable in Sage) Below are objects e, f, g, i class e: pass f = e() g = pi # (g's "type" in Sage is symbolic expression. It's supposed to be 3.1415....etc) i = lambda x: x*x I gather that e is a class which means it is mutable (Does an immutable class make sense? Can't all classes be modified?). Since f is an instance of a class, I am guessing it is also mutable since classes are mutable. Since numbers are immutable in

Any nice way to make two immutable objects refer to eachother?

牧云@^-^@ 提交于 2019-12-04 16:29:57
问题 Take these two Java classes: class User { final Inventory inventory; User (Inventory inv) { inventory = inv; } } class Inventory { final User owner; Inventory (User own) { owner = own; } } Is there any way without using reflection* to pull this off? I don't actually expect it is, but it can't hurt to ask. Update: Since in bytecode construction has two steps (1. allocate object, 2. call constructor**) could this be (ab)used to do this, with handwritten bytecode or a custom compiler? I'm

Hashing an immutable dictionary in Python

无人久伴 提交于 2019-12-04 16:29:45
问题 Short version: What's the best hashing algorithm for a multiset implemented as a dictionary of unordered items? I'm trying to hash an immutable multiset (which is a bag or multiset in other languages: like a mathematical set except that it can hold more than one of each element) implemented as a dictionary. I've created a subclass of the standard library class collections.Counter , similar to the advice here: Python hashable dicts, which recommends a hash function like so: class FrozenCounter

Are .NET enum types actually mutable value types?

寵の児 提交于 2019-12-04 16:05:30
问题 Looking, with reflection, at the fields of an enum type, I noticed to my surprise that the "backing" instance field that holds the actual value of a particular instance of the enum is not private , as I would have thought, but public . And it was not readonly either. ( IsPublic true, IsInitOnly false.) Many people consider "mutable" value types in the .NET type system "evil", so why are the enum types (as created from C# code for example) just that? Now, as it turns out, the C# compiler has

What are the advantages of built-in immutability of F# over C#?

好久不见. 提交于 2019-12-04 15:24:45
问题 I heard F# has native support for immutability but what about it that can not be replicated in C#? What do you get by an F# immutable data that you don't get from a C# immutable data? Also in F#, is there no way to create mutable data? Is everything immutable? If you use both C# and F# in an application, can you change the immutable F# data in C#? Or do you just have to create new C# types that uses the immutable F# data and replaces the references that points to those data? 回答1: The way F#

Can immutable be a memory hog?

夙愿已清 提交于 2019-12-04 15:16:37
问题 Let's say we have a memory-intensive class like an Image , with chainable methods like Resize() and ConvertTo() . If this class is immutable, won't it take a huge amount of memory when I start doing things like i.Resize(500, 800).Rotate(90).ConvertTo(Gif) , compared to a mutable one which modifies itself? How to handle a situation like this in a functional language? 回答1: If this class is immutable, won't it take a huge amount of memory? Typically your memory requirements for that single

Synchronize to ensure that reference to immutable object will be seen by another thread

泄露秘密 提交于 2019-12-04 13:35:01
I was studying this to understand the behavior of final fields in the new JMM (5 onwards). This concept is clear: guaranteed visibility of initialized final fields to all threads after the object is properly constructed. But then at the end of the section, I read this, which simply confuses me: Now, having said all of this, if, after a thread constructs an immutable object (that is, an object that only contains final fields), you want to ensure that it is seen correctly by all of the other thread, you still typically need to use synchronization. There is no other way to ensure, for example,