mutable

Java: state sharing between threads in functional programming

梦想的初衷 提交于 2019-12-12 20:24:08
问题 My question is a more specific instantiation of this question: Functional programming: state vs. reassignment I'm a newbee to FP and trying to understand it through Java. I have the following class whose object is shared between multiple threads: public class Bank { private double[] accounts = new double[1000]; public synchronized void transfer(int from, int to, double amount) { account[from] -= amount; account[to] += amount; } } (This is a very simplified example, hence other details such as

Optimizing mutable array state heavy manipulation code

感情迁移 提交于 2019-12-12 09:57:44
问题 I've been trying to complete this exercise on hackerrank in time. But my following Haskell solution fails on test case 13 to 15 due to time out. My Haskell solution import Data.Vector(Vector(..),fromList,(!),(//),toList) import Data.Vector.Mutable import qualified Data.Vector as V import Data.ByteString.Lazy.Char8 (ByteString(..)) import qualified Data.ByteString.Lazy.Char8 as L import Data.ByteString.Lazy.Builder import Data.Maybe import Control.Applicative import Data.Monoid import Prelude

Implementing a Number System in Java: Mutable vs. Immutable

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:38:29
问题 I am implementing classes for rational numbers, but the problems and issues are essentially the same for complex numbers as well as other classes intended to be used in applications with a significant number of calculations performed on a given mathematical object. In the libraries distributed with the JRE and in many third-party libraries, number classes are immutable. This has the advantage that "equals" and "hashcode" can be reliably implemented together as intended. This will enable

F# Multiple Attributes CLIMutable DataContract

血红的双手。 提交于 2019-12-12 03:59:13
问题 I am running into an issue with combining attributes when using ServiceStack.Redis with f#. Maybe I am thinking about this wrong but right now I'd like my type to be seralized to JSON but also passable to ServicvStack. The issue with f# types is that there is no default constructor and this the data added to my Redis instance is emtpy, well the record is there but none of the data inside it is there. Here is an example: open System open ServiceStack.Redis [<CLIMutable>] [<DataContract>] type

C++11: Does std::initializer_list store anonymous array? Is it mutable?

天涯浪子 提交于 2019-12-12 03:44:17
问题 Does the C++ standard say that std::initializer_list<T> is a reference to a local anonymous array? If it says, then we should never return such an object. Any section in the standard say so? Another question, are the underlying objects of a std::initializer_list<T> mutable? I tried to modify it: #include <initializer_list> int main() { auto a1={1,2,3}; auto a2=a1;//copy or reference? for(auto& e:a1) ++e;//error for(auto& e:a2) cout<<e; return 0; } But compiled with error : error: increment of

Multiple Instances of a Python Object are acting like the same instance

狂风中的少年 提交于 2019-12-12 02:06:59
问题 I have my class template here: import sqlite3 class Patron(object): #Let's set some basic attributes attributes = { "patron_id" : None, "name" : None, "address" : None, "phone" : None, "email" : None, "fee_balance" : None, "fees_per_day" : None, "books_checked_out" : [], "books_overdue" : []} def __init__(self): #Create a empty instance pass def new(self, patron_id, name, address, phone, email): #Create an instance with new values self.attributes["patron_id"] = patron_id self.attributes["name

Problems drawing a bitmap on a layout

北城以北 提交于 2019-12-12 01:39:15
问题 I create the Layout programatically in onCreate: new Handler().postDelayed(new Runnable() { @Override public void run() { myView = new MyView(SketchActivity.this, layout.getWidth(), layout.getHeight()); layout2.addView(myView); layout2.bringToFront(); } }, 50); The View where i create the (mutable?) bitmap: public MyView(Context c, int width, int height) { super(c); WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int w =

List of lists changes reflected across sublists unexpectedly

送分小仙女□ 提交于 2019-12-11 22:01:55
问题 I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of the innermost values: myList[0][0] = 5 Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it? 回答1: When you write [x]*3 you get, essentially, the list [x, x, x] . That is, a list with

Python Bit Operations on a list of Int's

心不动则不痛 提交于 2019-12-11 19:14:04
问题 What I want to do that does not work: List of ints... BebossArray=[0 for i in xrange(1024)] My bit functions (There are all the usual set,clear,toggle, test but here is set) def setBit(dint, offset): mask = 1 << offset dint=(dint|mask) return so I would like to set a bit in one of the ints in the list... setBit(DebossArray[index],3) This, of course, does not work. The assignment in setBit just creates a new object and DebossArray[index] stays put as expected. So I get that INT's are

Python subclass tuple object with ability to reinstantiate self internally

拜拜、爱过 提交于 2019-12-11 09:46:56
问题 I understand the concept of mutable v. immutable objects in Python, no problem. While any immutable object's intrinsic value cannot be modified directly, any instance of an immutable object can be reinstantiated with different values. What I would like to do is build an internal function on a subclass of tuple that can in a controlled fashion, reassign it's own value. This could be basic functionality that I just can't seem to find and would appreciate any assistance. For example, here is