immutability

How do I work around mutability in moment.js?

て烟熏妆下的殇ゞ 提交于 2019-11-28 17:17:38
问题 I've run into a problem where I have to store the initial values of a moment object but I'm having some trouble preventing my variable from changing along with the original object. Unfortunately Object.freeze() doesn't work, because moment.js returns an "Invalid date" error when I try to format that. 回答1: There's a Moment.js plugin on NPM called frozen-moment - You could use moment().freeze() in place of Object.freeze(moment()) . Otherwise, vanilla Moment.js has a clone method that should

How does concurrency using immutable/persistent types and data structures work?

前提是你 提交于 2019-11-28 17:14:13
问题 I've read a lot about functional languages recently. Since those use only immutable structures they claim that concurrency issues are greatly improved/solved. I'm having some serious trouble understanding how this can actually be delivered in a real-life context. Let's assume we have a web server with one thread listening on a port (well, IO is another thing i have difficulty wrapping my head around but let's just ignore that for now); On any connection attempt a socket is created and passed

Downsides to immutable objects in Java? [closed]

淺唱寂寞╮ 提交于 2019-11-28 17:13:53
The advantages of immutable objects in Java seem clear: consistent state automatic thread safety simplicity You can favour immutability by using private final fields and constructor injection. But, what are the downsides to favouring immutable objects in Java? i.e. incompatibility with ORM or web presentation tools? Inflexible design? Implementation complexities? Is it possible to design a large-scale system (deep object graph) that predominately uses immutable objects? dfa But, what are the downsides to favouring immutable objects in Java? incompatibility with ORM or web presentation tools?

Is “new String()” immutable as well?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 16:47:15
I've been studying Java String for a while. The following questions are based on the below posts Java String is special Immutability of String in java Immutability: Now, going by the immutability, the String class has been designed so that the values in the common pool can be reused in other places/variables. This holds good if the String was created as String a = "Hello World!"; However, if I create String like String b = new String("Hello World!"); why is this immutable as well? (or is it?). Since this has a dedicated heap memory, I should be able to modify this without affecting any other

What is the data structure behind Clojure's sets?

久未见 提交于 2019-11-28 16:45:13
I recently listened to Rich Hickey's interview on Software Engineering Radio . During the interview Rich mentioned that Clojure's collections are implemented as trees. I'm hoping to implement persistent data structures in another language, and would like to understand how sets and Clojure's other persistent data structures are implemented. What would the tree look like at each point in the following scenario? Create the set {1 2 3} Create the union of {1 2 3} and {4} Create the difference of {1 2 3 4} and {1} I'd like to understand how the three sets generated ( {1 2 3} , {1 2 3 4} , and {2 3

What does the comment “frozen_string_literal: true” do?

﹥>﹥吖頭↗ 提交于 2019-11-28 15:32:56
This is the rspec binstub in my project directory. #!/usr/bin/env ruby begin load File.expand_path("../spring", __FILE__) rescue LoadError end # frozen_string_literal: true # # This file was generated by Bundler. # # The application 'rspec' is installed as part of a gem, and # this file is here to facilitate running it. # require "pathname" ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath) require "rubygems" require "bundler/setup" load Gem.bin_path("rspec-core", "rspec") What is this intended to do? # frozen_string_literal: true # frozen_string

Enums in Javascript with ES6

孤街浪徒 提交于 2019-11-28 14:31:37
问题 I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS. The best I can come up with is: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN: Symbol("green") }; Object.freeze(Colors); The const keeps Colors from being reassigned, and freezing it prevents mutating the keys and values. I'm using Symbols so that Colors.RED is not equal to 0 , or anything else besides itself. Is there a problem with this formulation? Is there a better

Mutability of the **kwargs argument in Python

本小妞迷上赌 提交于 2019-11-28 13:28:47
Consider a case where I change the kwargs dict inside a method: def print_arg(**kwargs): print kwargs.pop('key') If I call the method pop_arg with a dictionary like this: mydict = {'key':'value'} print_arg(**mydict) will mydict be changed by this call? I am also interested in a more detailed explanation of the underlying method calling mechanism that lets mydict change or not. Let's see: import dis dis.dis(lambda: print_arg(**{'key': 'value'})) 6 0 LOAD_GLOBAL 0 (print_arg) 3 BUILD_MAP 1 6 LOAD_CONST 1 ('value') 9 LOAD_CONST 2 ('key') 12 STORE_MAP 13 CALL_FUNCTION_KW 0 16 RETURN_VALUE Let's

Remove element from nested redux state

独自空忆成欢 提交于 2019-11-28 13:01:28
I want to remove a element from my redux state. Im using seamless-immutable and its API to operate on the state. But I couldn't find any nice looking way to delete a element when the state is nested. So the element I want to delete is: state.shoppingcartReducer.products[articleNr] Thanks! import Immutable from 'seamless-immutable' import { addToShoppingcart, createShoppingCart } from '../actions/shoppingcartActions' const CREATE_SHOPPING_CART = 'CREATE_SHOPPING_CART' const ADD_PRODUCT = 'ADD_PRODUCT' const DELETE_PRODUCT = 'DELETE_PRODUCT' const UPDATE_QUANTITY = 'UPDATE_QUANTITY' const

DeepReadonly Object Typescript

让人想犯罪 __ 提交于 2019-11-28 12:06:32
It is possible to create a DeepReadonly type like this: type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]>; }; interface A { B: { C: number; }; D: { E: number; }[]; } const myDeepReadonlyObject: DeepReadonly<A> = { B: { C: 1 }, D: [ { E: 2 } ], } myDeepReadonlyObject.B = { C: 2 }; // error :) myDeepReadonlyObject.B.C = 2; // error :) This is great. Both B and B.C are readonly. When I try to modify D however... // I'd like this to be an error myDeepReadonlyObject.D[0] = { E: 3 }; // no error :( How should I write DeepReadonly so that nested arrays are readonly as well? As of