immutability

Combining two Immutable lists with inject

爱⌒轻易说出口 提交于 2019-12-11 17:34:37
问题 I have two lists that consist of strings, both immmutable: def list1 = [ "A", "B", "C" ] list2 = ["D", "E", F"] List 2 is being returned by a custom function I have made. Is there a way to take these two immutable lists and combine both of their elements with inject? I have tried numerous combinations without success. I have googled extensively for this. I cannot change this to a mutable list. I am aware that it would be much easier to simply combine two lists then make them immutable, but

My Face class does not seem to be immutable even though I already declared it as final, how do I correct it?

南笙酒味 提交于 2019-12-11 15:58:13
问题 I am trying to make my Face class immutable such that my Face object will not change once it has been initialized. This is what I have so far: public class Face{ protected final int[][] grid; protected Face half; public Face(int[][] grid){ this.grid = grid; } public Face rotateRight(){ int rows = 3; int cols = 3; int[][] transposedArray = new int[3][3]; for (int i = 0; i<rows; i++){ for (int j = 0; j<cols; j++){ transposedArray[i][j]=grid[rows-j-1][i]; } } return new Face(transposedArray); }

How do I remove an element from a Vector?

回眸只為那壹抹淺笑 提交于 2019-12-11 13:12:17
问题 This is what I'm doing now: private var accounts = Vector.empty[Account] def removeAccount(account: Account) { accounts = accounts.filterNot(_ == account) } Is there a more readable solution? Ideally, I'd like to write accounts = accounts.remove(account) . 回答1: I'd use this: accounts filterNot account.== Which reads pretty well to me, but ymmv. I'd also like a count that doesn't take a predicate, but the collection library is really lacking in specialized methods where one with a predicate

How to synchronize unmodifiable collections

扶醉桌前 提交于 2019-12-11 10:42:04
问题 I want to return an unmodifiable view of the class (that maintain a collection of items ) to outside clients . So to protect concurrent access, I need to wrap the collection in a synchronized wrapper first, then put an unmodifiable wrapper around the version I return to outside threads. So I wrote the following code and unfortunately it is throwing a ConcurrentModificationException. . import java.util.*; public class Test { public static void main(String[] args) { // assume c1 is private,

How make a variable public final in Ruby

江枫思渺然 提交于 2019-12-11 10:38:32
问题 I would like to create a class that during initialization of an Object of this class would assign provided value to one of the variables, in such way it can't be changed. For example: person = Person.new("Tom") person.name #=> Tom person.name = "Bob" this should raise an error or: person.name #=> Tom -> still 回答1: class Person def initialize name @name = name end attr_reader :name end person = Person.new("Tom") person.name #=> Tom begin person.name = "Bob" rescue puts $!.message # =>

React - Game of Life - Update state of specific elements in 2D array depending on which elements are clicked by user

笑着哭i 提交于 2019-12-11 09:39:29
问题 I'm still learning React and working on coding John Conway's Game of Life. I have created a game board for the game using a 2D array in state. I have this 2D array stored in state in my App.js . I also have two functional components, Gameboard.js and Square.js receiving props and creating the game board. Gameboard.js creates a table and passed props to Square.js letting it know which squares should be highlighted and which shouldn't. In other words it let's each individual square know if the

ngrx- default values being overwritten through spread operator?

此生再无相见时 提交于 2019-12-11 08:46:03
问题 I am working on an ngrx selector that converts a section of the state tree into a view model. As a simple example, in my state tree I have an array of manager . Thus I have the following setup for my state tree, and view model- export interface Manager { id: string, name: string } export interface AppState { managers: Manager[] } export interface ManagersVM { byId: {[key: string]: Manager}, allIds: string[] } export const defaultManagersVM: ManagersVM { byId: {}, allIds: [] }; Then in my

Why does this function mutate data?

三世轮回 提交于 2019-12-11 06:18:54
问题 function bubbleSort(toSort) { let sort = toSort; let swapped = true; while(swapped) { swapped = false; for(let i = 0; i < sort.length; i++) { if(sort[i-1] > sort[i]) { let temp = sort[i-1]; sort[i-1] = sort[i]; sort[i] = temp; swapped = true; } } } return sort; } let asdf = [1,4,3,2]; let asd = bubbleSort(asdf); console.log(asdf, asd); The output to this code is: [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]. What I would expect: [ 1, 4, 3, 2 ] [ 1, 2, 3, 4 ]. What I'm wondering, is why does this mutate the

Clone and then mutate approach in redux

时间秒杀一切 提交于 2019-12-11 06:13:59
问题 I've been reading stuff on redux for a while. And there is a strange thing for me. In most examples people give, all the copying logic is handled via reducers. I'm using typescript and want to adopt a more class-based approach. But maybe I'm missing something. Let's say I have a shopping Cart class. Along with cart reducer and cart actions. It looks the following way: export class Cart { private items:{[key:string]:number} = {}; constructor(items:{[key:string]:number} = {}) { Object.assign

Cannot get model based test working

与世无争的帅哥 提交于 2019-12-11 05:54:34
问题 As an exercise I wanted to implement a 2-3 finger tree. That should be the perfect opportunity to try out FsCheck's model-based testing. I decided to try the newer experimental version. So far I only coded one command for the test machine because I already fail at making that work—one the other hand it keeps the post short. The full code is available on GitHub. open CmdQ open Fuchu open FsCheck open FsCheck.Experimental type TestType = uint16 type ModelType = ResizeArray<TestType> type