mutability

Scala: Mutable vs. Immutable Object Performance - OutOfMemoryError

故事扮演 提交于 2021-02-17 21:23:07
问题 I wanted to compare the performance characteristics of immutable.Map and mutable.Map in Scala for a similar operation (namely, merging many maps into a single one. See this question). I have what appear to be similar implementations for both mutable and immutable maps (see below). As a test, I generated a List containing 1,000,000 single-item Map[Int, Int] and passed this list into the functions I was testing. With sufficient memory, the results were unsurprising: ~1200ms for mutable.Map,

How do I create a struct of references to traits when one object might implement multiple of the traits?

╄→гoц情女王★ 提交于 2020-05-31 04:13:12
问题 I have a struct which manages several sensors. I have a gyroscope, accelerometer, magnetometer, barometer, and thermometer. All of which are traits. pub struct SensorManager { barometer: Barometer + Sized, thermometer: Thermometer + Sized, gyroscope: Gyroscope + Sized, accelerometer: Accelerometer + Sized, magnetometer: Magnetometer + Sized } I need to make it modular so in the configuration file you can specify which sensors you are using. The problem is that some of the sensors overlap. For

NSMutableArray can't be added to

自古美人都是妖i 提交于 2020-01-21 15:47:03
问题 I've had this sort of problem before, and it didn't get a satisfactory answer. I have a viewcontroller with a property called "counties" that is an NSMutableArray. I'm going to drill down a navigation screen to a view that is about selecting the counties for a geographical search. So the search page drills down to the "select counties" page. I pass NSMutableArray *counties to the second controller as I push the second one on the navigation stack. I actually set that second controller's

NSMutableArray can't be added to

眉间皱痕 提交于 2020-01-21 15:45:03
问题 I've had this sort of problem before, and it didn't get a satisfactory answer. I have a viewcontroller with a property called "counties" that is an NSMutableArray. I'm going to drill down a navigation screen to a view that is about selecting the counties for a geographical search. So the search page drills down to the "select counties" page. I pass NSMutableArray *counties to the second controller as I push the second one on the navigation stack. I actually set that second controller's

What are the semantic implications of :volatile-mutable versus :unsynchronized-mutable?

我只是一个虾纸丫 提交于 2020-01-14 08:55:16
问题 I was studying a clojure lib when I noticed that a mutable field was annotated with ^:unsynchronized-mutable. Mutable is mutable, but I had no idea what the unsynchronized part meant, so I read the docs, which contain: Note well that mutable fields are extremely difficult to use correctly, and are present only to facilitate the building of higher level constructs, such as Clojure's reference types, in Clojure itself. They are for experts only - if the semantics and implications of :volatile

Is there a way to make an immutable reference mutable?

一曲冷凌霜 提交于 2020-01-14 03:46:07
问题 I want to solve a leetcode question in Rust (Remove Nth Node From End of List). My solution uses two pointers to find the Node to remove: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>>, } impl ListNode { #[inline] fn new(val: i32) -> Self { ListNode { next: None, val } } } // two-pointer sliding window impl Solution { pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> { let mut dummy_head = Some(Box:

Representing an amount of money with specific bills

笑着哭i 提交于 2020-01-05 07:59:53
问题 I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0) . I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest. (define (calc n xs) (cond ((null? xs) (list)) ((not (pair? xs)) (define y n)

Scala - covariant type in mutable collections

六眼飞鱼酱① 提交于 2020-01-04 07:37:07
问题 I am new in Scala world and now I am reading the book called "Scala in Action" (by Nilanjan Raychaudhuri), namely the part called "Mutable object need to be invariant" on page 97 and I don't understand the following part which is taken directly from the mentioned book. Assume ListBuffer is covariant and the following code snippet works without any compilation problem: scala> val mxs: ListBuffer[String] = ListBuffer("pants") mxs: scala.collection.mutable.ListBuffer[String] = ListBuffer(pants)

How does interior mutability work for caching behavior?

十年热恋 提交于 2020-01-02 04:08:21
问题 I'm trying to create a struct that takes a Path and, on demand, loads the image from the path specified. Here's what I have so far: extern crate image; use std::cell::{RefCell}; use std::path::{Path}; use image::{DynamicImage}; pub struct ImageCell<'a> { image: RefCell<Option<DynamicImage>>, image_path: &'a Path, } impl<'a> ImageCell<'a> { pub fn new<P: AsRef<Path>>(image_path: &'a P) -> ImageCell<'a>{ ImageCell { image: RefCell::new(None), image_path: image_path.as_ref() } } //copied from

How does the Rust compiler know `Cell` has internal mutability?

蓝咒 提交于 2019-12-19 07:27:10
问题 Consider the following code (Playground version): use std::cell::Cell; struct Foo(u32); #[derive(Clone, Copy)] struct FooRef<'a>(&'a Foo); // the body of these functions don't matter fn testa<'a>(x: &FooRef<'a>, y: &'a Foo) { x; } fn testa_mut<'a>(x: &mut FooRef<'a>, y: &'a Foo) { *x = FooRef(y); } fn testb<'a>(x: &Cell<FooRef<'a>>, y: &'a Foo) { x.set(FooRef(y)); } fn main() { let u1 = Foo(3); let u2 = Foo(5); let mut a = FooRef(&u1); let b = Cell::new(FooRef(&u1)); // try one of the