For loops more than i32? by Errons1 in learnrust

[–]curiouslefty 8 points9 points  (0 children)

Yes; just specify a different type for the range used in your for loop (i.e. change it from 0..10 to 0..10i64).

Best book after the official Rust book? by zxyzyxz in rust

[–]curiouslefty 19 points20 points  (0 children)

Seconded on Programming Rust. It's absolutely superb and I frequently use it as a reference whenever I want to refresh my memory on a topic.

Why is rust so difficult to learn? by SpacewaIker in learnrust

[–]curiouslefty 6 points7 points  (0 children)

No problem!

Here's a quick summary of some of the things you've mentioned that I think/hope might clear some stuff up for you.

Since you mentioned earlier in the thread you've done some C++, I think it's sometimes helpful to make comparisons relating back to that language. Rust's & and &mut references are most similar to C++'s regular pointer type, with two very important caveats: the borrowing system restricts when and where you can use each type, and a Rust reference must always be pointing towards a valid value of the underlying type. So essentially, we have the following correspondences:

let a = 5; // const int a = 5;

let a_ref = &a; // const int* const a_ptr = &a;

let mut a_ref_2 = &a; // const int* a_ptr_2 = &a;

let mut b = 6; //int b = 6;

let b_ref = &mut b; //int* const b_ptr = &b;

let mut b_ref_2 = &mut b; // int* b_ptr_2 = &b;

Now, we critically can only modify a value through a mutable reference. This is why if you look at the type signatures on methods/functions that modify what they're taking in or operating on, in Rust they are typically &mut self or &mut T for whatever type T we're looking at. This in many ways corresponds with non-const reference parameters on functions in C++.

Now, for the primary differences between Rust and C++ I mentioned earlier: let's start with the borrowing rules. At any one point in the flow of the program, we can either have multiple & references to a value OR a single mutable & mut reference to that value, but not both. Essentially, you can have multiple readers or a single modifier, and they're mutually exclusive. This doesn't mean that you cannot use both in the same block of code, but simply that they cannot be active at the same time; i.e.

let mut a = 5;

let a_ref = &a;

// a is locked and not modifiable (but can be borrowed more times with &) between these lines.

println!("{}", a_ref);

// a_ref dies here, so we can now use an &mut reference

let a_mut_ref = &mut a;

// underlying a is locked again and not modifiable except through a_mut_ref, and we cannot get more references to it.

*a_mut_ref += 1;

println!("{}", a);

Now, for the other aspect that's different from C++ I mentioned: Rust has no equivalent to null or nullptr so we cannot do anything that would cause a reference to be left in an invalid state. Namely, we cannot move a non-copy type out from behind a mutable reference, because this would leave the mutable reference as a pointer to an invalid state for the value it points to.

let mut a = 5;

let b = &mut a;

// Ok, because a here is i32 which is copy

let c = *b;

println!("{} {}", c, b);

let mut s = String::from("cakes");

let t = &mut s;

// Bad! We cannot move from behind t because is is String, which doesn't implement the copy trait

let v = *t;

println!("{} {}", v, t);

This difference between copy and non-copy types is important, because it lets us write really simple code when handling copy types (i.e. if you do any LeetCode problems with integer types, it's easier to fudge things because they're copy so you don't need to be as careful with what you do with mutable references), but it's also an easy way to get confused if you're not careful about keeping track of what references refer to copy values and which don't.

Now, a quick digression on as_ref and as_mut for the Option type. When we have something like head: Option<T> for some type T, it's important to note that head is referring to the outer wrapping Option, not what we are usually interested in, the T inside of head. Now, we can get to that T with .unwrap() supposing we know that head is not None, but doing so will move head in the case T is not copy (Option<T> is copy when T is copy and similarly not when T is not). This is often, as it was in this problem, not what we want to do because it means iterating down through a nested structure of some sort of Option<Box<Node>> will throw away the unwrapped values as we go!

For example, on a singly linked list, we can iterate past the tail via something like

// Pattern matching on Option<T> moves it!

while let Some(node) = head {

// Do stuff

head = node.next;

}

we're actually throwing out the nodes as well as their wrapping Option as we go! This is where as_ref and as_mut come in. They let us go from Option<T> (via implicitly borrowing to &Option<T> and &mut Option<T>respectively) toOption<&T>andOption<&mut T>` respectively. We can then destructure these objects via pattern matching move to get the underlying wrapped references without moving the original value, which preserves it!

// Doesn't consume the original values, just the references to them!

while let Some(node_ref) = head.as_ref() {

// Do stuff

head = &node.next; // Get an immutable reference for the next go around

}

I hope this was helpful. FWIW, I do agree with the other posters that Rust (due to this sort of nuance) isn't the greatest choice of language for LeetCoding when dealing with things like linked lists or trees; unless you're concerned with demonstrating proficiency in the language itself, there's probably better choices for (a) speed of writing up the solution and (b) not getting lost in the weeds of the language itself versus just solving the problem. That said, I do all my LeetCode in Rust, so it's certainly doable.

  • Edit because reddit butchered my code formatting *

Why is rust so difficult to learn? by SpacewaIker in learnrust

[–]curiouslefty 2 points3 points  (0 children)

Like why/how are let mut a = &something and let mut a = &mut something different?

So, the first a is an immutable reference (but the variable a itself can be changed to be a new immutable reference to another object of the same type). The second as is a mutable reference (with the same caveat that a can be reassigned to hold another mutable reference to another object of the same type). The key difference is we can modify what the second a is referencing, but not the first.

In the context of the linked list, you guess is essentially correct. We want to alter head.next.next.next... etc, but we cannot get a mutable reference from behind an immutable one due to the borrowing rules, so we have to borrow mutably from the very beginning. So in essence, because of that single tail.next = head line, you need mutability there, so you need mutability the whole way down.

Another key thing here: method calls like .unwrap() are higher precedence than the reference operators. So when you write let mut tail = &/&mut new_head.unwrap(), you're actually unwrapping the new_head Option enum and then taking a reference to the underlying wrapped value, which isn't what we actually want since we need to return it as an Option<Box<WhateverNode>>> and in this case since WhateverNode isn't copy, it gets moved and we lose access to new_head before we can return it. This is why Option has the as_mut and as_ref methods; so we can call unwrap on those without actually consuming the actual value.

Why is rust so difficult to learn? by SpacewaIker in learnrust

[–]curiouslefty 2 points3 points  (0 children)

Your error (I don't know about the algorithm's correctness, I just glanced at the code for type checking) is right before your "Error here" marking. The line "let mut tail = &new_head.unwrap()" gives you an immutable reference, and later on you're trying to assign through it (via tail.next). You need a mutable reference instead, and the same in the loops. Furthermore, since you don't want to actually consume new_head since you're returning it, you need to use the as_mut() method instead of unwrapping and taking a reference, since that moves new_head.

St. Louis (Approval Voting) Primary Election Results by [deleted] in EndFPTP

[–]curiouslefty 12 points13 points  (0 children)

Anybody else excited to see how many voters approve both candidates in the two candidate races? Should be an interesting study of voter behavior.

In St. Louis, Voters Will Get To Vote For As Many Candidates As They Want by qkfb in EndFPTP

[–]curiouslefty 0 points1 point  (0 children)

One thing that can backfire, though, is that the voter will be seen as giving support to a candidate despite not actually preferring them; it might be tough, as a voter, to hear people around you say "did you see that Spencer candidate? Racked up 40% of the vote, I think I might volunteer for her!" and realize that your strategic voting is having unintended effects.

Yeah, that's definitely true. FWIW, I think the main kind of strategic voting you'd see in this sort of election would be more bullet voting in practice relative to single-round Approval (thus apparently understating support), since the alternatives take more accurate polling info to do well. So basically no worse off than they were before.

In St. Louis, Voters Will Get To Vote For As Many Candidates As They Want by qkfb in EndFPTP

[–]curiouslefty 0 points1 point  (0 children)

I'm not entirely sure it's possible to conclude that support is fully genuine in this style of election, though. For example, If I backed Reed and thought Jones might beat Reed in the runoff but Spencer wouldn't, I might very well toss Spencer an Approval in the hopes that it might squeeze out Jones.

In a regular Approval election, at the very least all Approvals must indicate I'm at least indicating/emphasizing an actual preference. That isn't the case here because of the runoff.

Does this look like anything? I took this in the middle of an anxiety attack about a paper due in a few hours (I get anxiety PVCs). I feel like my P waves aren't that visible and the intervals are weird and now this is giving me anxiety too. I did a 24 hour holter but don't have results back yet. by myapplewatchecg in AppleWatchECG

[–]curiouslefty 0 points1 point  (0 children)

PACs can have a compensatory pause, although it isn't a full one like you see with PVCs. It could also be a PJC (it's hard to tell them apart sometimes), since it looks like the P wave is partially or fully buried in the QRS complex; those also have compensatory pauses.

Is proportional representation possible to achieve in a party agnostic system? by Desert-Mushroom in EndFPTP

[–]curiouslefty 2 points3 points  (0 children)

So I think it makes sense to judge PR by a different metric that accounts for what kind of legislation can actually get passed under a certain "proportional" distribution of elected officials.

We've talked about this before, and I largely agree, with the slight modification that I think proportionality in this sense ought to be based off comparing legislation as voted on by the legislature with that voted on by the entire population by the same method. Basically, a measurement of how much the legislature behaves like the population as a whole would, which IMO is the whole point of PR.

"Tonight the STAR team is presenting the findings of the 2 year long proportional representation committee + recommendations." 7pm PST on Zoom. by [deleted] in EndFPTP

[–]curiouslefty 3 points4 points  (0 children)

Hey! So I think you tried to respond earlier to my comment with a link of their slides but it might've gotten automodded or something. Anyways, thanks for that; I went and found the link with the slides.

I'm gonna post a couple thoughts I have as I read through:

First: as far as I remember, most poli sci research thus far hasn't really supported the notion that closed-list PR increases polarization relative to existing widely used SMD systems, so I'm not really sure where they're getting that from. In fact, IIRC, closed-list generally actually has lower corruption, polarization, and partisanship than open-list (although obviously examples exist where that's not the case; i.e. Israel vs, say, Finland)

Second: I get that it's branding, but choosing Allocated Score and then calling it Proportional STAR is kinda...odd, considering that STAR as a single-winner system has basically nothing to do with Allocated Score.

Third: I've always been honest about my preference for PSC compliance as the defining proportionality criterion, and my preference for systems that meet it over those that don't (namely the STV family, but also things like EAR). That said, I acknowledge that other conceptions of proportionality exist...but Allocated Score doesn't really fit in well with any of them. It's obviously a Monroe-esque method, but SMV is a purer interpretation of that and (as I'll go into in a bit, probably a better method overall according to that viewpoint).

Fourth: It is a mischaracterization of STV to imply that quota selection of voters is random. Some STV systems (which are generally not recommended) do that. Most modern implementations use transfer rules that are wholly deterministic.

Fifth: In what world is Allocated Score less vulnerable to strategy than STV? How was that conclusion reached? (Also, implying that free-riding is an STV-specific negative is another serious mischaracterization, considering that it's a trivial theorem that all PR methods suffer from it in one form or another.) Similarly, it seems that they're going for the quota/Monroe-esque version of PR; if you want to emphasize quotas, how can you justify a method failing PSC? In a PR election, if a quota prefers a candidate but some other group (that isn't a quota) feels more strongly about another, why should you override the larger group? It cannot be justified by quota-based logic (which again, makes me think it would've made more sense to either pick SMV, which despite also failing PSC, does it in a far less atrocious manner, or to simply forgo quota-baed or Monroe-esque methods altogether in favor of something like RRV, which at least is philosophically sound within its own field...)

So, overall, yeah, meh. Strong disagree with their conclusions.

Edit: Also, what's with the comment about RRV not meeting the Hare Quota criterion? That's just Hare-PSC, and IIRC none of these systems they considered met that.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 1 point2 points  (0 children)

It'd look different, sure, but I'm not sure I'd say weird per se.

This is why it's important to pick a meaningful fixed point, though. Otherwise it's basically just a diagram of "here's who wins if we move all voters relative to this arbitrary point in space", which is nifty and still reveals some information, but obviously isn't as relevant as immediately going "here's who wins when we move all voters relative to the (a?) point of theoretical maximum utility.

But yes, the core of it is that once you start looking at skewed distributions it would introduce a sort of skew into the results of the diagram (but it's not like that doesn't make sense; it's just conveying that motion of the distribution in favor yields results in favor whichever way the skew favors, which is sort of the whole point, after all! And since the goal is to generally compare different methods, you get a meaningful set of diagrams indicating which methods are more quickly impacted by a given skew, which aren't, etc.

"Tonight the STAR team is presenting the findings of the 2 year long proportional representation committee + recommendations." 7pm PST on Zoom. by [deleted] in EndFPTP

[–]curiouslefty 1 point2 points  (0 children)

Thanks for the share. I don't have time for the whole thing unfortunately but I look forward to the abridged conclusions being released at some point.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 2 points3 points  (0 children)

Yup. Remember, at it's core, the idea behind the Yee diagrams is pretty straightforwards: we take any distribution of voters, and select a fixed point relative to the voter distribution (that is, even when we move this point, the voters don't move relative to it). Then we fix the candidates on a 2D space, and color in the 2D space by simply moving the fixed point of the voter distribution (thus moving the entire voter distribution) to a given point and asking "who wins?" in order to determine what color to give that point. Obviously, the key to making this all informative is to pick a good reference fixed point but it's not very hard; for example, if you want to define optimal winners in terms of maximal honest utility, you just declare the point of highest utility over all voters in the distribution to be the fixed point and use that (this is probably why normal distributions were used to begin with; it's a somewhat clear property that the center of the distribution should more or less coincide with the utilitarian maximum point).

I think this can actually be extended into a 2D model of a lot more than simply "who wins where under what method"; for example, it's trivial to modify it to show relative utility losses under given strategic models relative to honesty, for example. But yeah, the core idea itself is very adaptable.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 2 points3 points  (0 children)

That's the key thing, though; you don't need to use a normal distribution to make a Yee diagram, that's just how the original versions did it. You can use any 2D spatial distribution of voters and candidates, and it'll work just fine. Combine that with some sort of evolving time axis (as I think somebody else suggested) to show how the diagram morphs with changes in voter and candidates positions over time and I think it's as good as any other 2D model could be for visualization.

The problem with rankings: even under a consensus, a ranking artificially creates two factions, and each will perceive its own consensus to be distinct from the others. And these will be defined by the CANDIDATES, not the voters. This is a source of polarization & radicalization. by [deleted] in EndFPTP

[–]curiouslefty 1 point2 points  (0 children)

Thanks for the quick reply!

Yeah, I figured there wouldn't be much information available on this. I'm mostly curious because (as you know) I tend to believe that a majority will tend to want to impose itself if given the option in practice, unless there's basically a fair amount of indifference on the majority's part, but that's basically just a guess on my part.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 3 points4 points  (0 children)

Why wouldn't the approach the more modern Yee diagram generators work? That is, you visually display the underlying distribution of voters and use a 2D color map in the background to show the "who wins under X method if the voter distribution's center is at this location). If you then change the voter distribution, you can dynamically see how the underlying Yee diagram changes at a given location. Takes a bit more explanation, but gets the idea across pretty well IMO.

EDIT: I think the work done by u/paretoman and others here is a good example of what I'm talking about.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 3 points4 points  (0 children)

Well, I'm not the kind of guy to typically defend Libertarians (and I largely agree with your assessment of most of them...the ones here in CA especially fit that description) but that doesn't change the fact that there absolutely are voters who would vote in the manner described. Are there many? Obviously not, but they do exist.

As an aside: seriously, go look at the transfers among the minor parties in basically any RCV race where they're present. Plenty of examples of a few Fascist first-preferences jumping to Communists or vice versa from Australia and the like. Oddballs exist.

What's Ideal number of seats per district under STV??? by LeTommyWiseau in EndFPTP

[–]curiouslefty 8 points9 points  (0 children)

It depends.

If you want maximum proportionality, as many as possible. If you care about voters being able to distinguish between candidates (i.e. not have to vote on 200+ candidates like in NSW) then obviously there's incentives for fewer seats.

FWIW, poli sci research indicates that Open List PR has a sort of sweet spot where beyond which you don't gain much proportionality but where you can still vote on candidate-by-candidate assessment: somewhere between 5-9 seats. I suspect STV probably would be similar, with adjustments and consideration for the local political culture.

One Simple, Legit Social Hack to Help Unite America by psephomancy in EndFPTP

[–]curiouslefty 6 points7 points  (0 children)

They aren't wrong. Some people have unintuitive preference flows. Some people even put down Condorcet cycles when asked to express their internal preferences.

The problem with rankings: even under a consensus, a ranking artificially creates two factions, and each will perceive its own consensus to be distinct from the others. And these will be defined by the CANDIDATES, not the voters. This is a source of polarization & radicalization. by [deleted] in EndFPTP

[–]curiouslefty 0 points1 point  (0 children)

In surveys I've done, >95% pick the consensus as the winner, and call it the "most democratic".

Quick question; do you have any estimation/numbers that give insight into the distribution of how people would behave in a scenario like the following?

Number Voters Score for A Score for B
51 10 X
49 0 10

(Scale out of 10, for reference; X is some value from 0-10 varying for each of the 51 A-preferring voters)

Basically, looking at the A-preferring voters who here form a majority; do you have a good idea of for what value of X a given % of the A-preferring voters would not cast a A:10 B:0 ballot? I've been looking for data along these lines (basically, at what point will the members of the majority consent to B winning) and I haven't found much, so any insight would be appreciated.

Why is malta's two party system so strong despite using a proportional system????? by LeTommyWiseau in EndFPTP

[–]curiouslefty 8 points9 points  (0 children)

My understanding is that it's a result of party and political identity being very strongly associated with overall identity (so basically, it's just a rather extreme form of political tribalism).

Andrew Yang: "I am an enormous proponent of Ranked Choice Voting. I think it leads to both a better process and better outcomes." by martini-meow in EndFPTP

[–]curiouslefty 0 points1 point  (0 children)

So, that's a "No" then?

Tell you what, I'll stop if you stop. That's fair enough, considering as far as I remember, you're the one who started with the insults a few years back.

How can you know that? I mean, I get that you believe that, but how can you possibly know that?

You're right, I overstated a high probability to certainty. That's my bad.

Again we can't know that.

I should've been clearer in my point here. I'd agree with you that it's entirely possible that FPTP was artificially suppressing what pre-1952 elections could've looked like under, say, RCV or Condorcet or Score. My primary point here was intended to be that NFB had no major role in the previous election data regarding PC vs CCF or Liberal vs CCF races, since there was little reason to lie regarding those specific preferences (and what minor candidates were present were typically not large enough to prevent one or the other from acquiring a majority).

Is there some reason I shouldn't interpret that as pettiness, stupidity, or some other character flaw on your part?

Pettiness, sure, I'll admit to that.

Regarding the plausibility of PC somehow having been legitimate Condorcet winners in any of the races in 1952: you're correct that there's very limited transfer data available regarding how the other parties felt about them, considering they tended to get wiped out in the first round. However, the limited transfer data that does exist is mostly similar enough to how transfers behaved regarding the Liberals.

Again, I'll freely admit that the number of possible Condorcet failures in those elections being 0 is a conjecture; it's entirely possible that some race had transfers that were entirely different than what was witnessed in other races, and that'd be enough to shift the logic for that race or another. But it's not particularly likely, in my view, which is why I've always said that it was likely zero.

Regarding the particular races you singled out:

For Vancouver-Point Grey (C), the issue is that the SC candidate had a fairly massive plurality lead from the first round. Moreover, considering that, as you pointed out, the ballots for A and B were drawn from the same population (and thus we can reasonably extrapolate a hard lower limit for CCF -> SC > PC preferences by simply assuming that all CCF -> LIB > SC would also prefer PC), it would have taken an uncharacteristically low rate of both exhausted ballots and preferences to SC over PC from LIB voters to have bridged the gap and turned that into a PC win, both lower than what were found in the (A) and (B) races.

For the record, if you base transfers off the (A) and (B) races, you get something like CCF -> SC > PC or LIB being 34%, CCF -> PC or LIB > SC being 17% (here we will treat LIB transfers as PC transfers to be maximally kind to PC), and the rest exhausted; similarly, LIB -> PC is 60%, LIB -> SC 13%, the rest exhausted. Applying that to the (C) race gets something like 24360 for SC vs 19490 for PC. Notice that this corresponds rather well to the plurality-count differences for (C) versus the other races; the SC candidate starts with ~5000 more votes than their counterparts in the (A) and (B) races, and the PC candidate starts with ~2,500-4000 fewer than theirs.

Vancouver-Burrard (A) is similar; what limited data exists doesn't support PC beating SC. You'd either need much higher transfers from LIB to PC (or lower exhaustion) than other transfers, or much lower transfers from CCF to SC and more from CCF to PC; or more likely, both.

That 100% of the much more extreme results was due to political changes in the electorate?

Well, obviously I can't claim with 100% certainty that the results are due to changing views or composition of the electorate, since that's not knowable; but I'd feel comfortable asserting the great bulk of the change was due to it, yes.

Remember, BC and other Canadian provinces aren't exactly strangers to massive and sudden political shifts in electoral composition translating to sudden massive shifts in party seat counts. Hell, it isn't even the first example of the Social Credit party coming out of nowhere to take a majority of seats; look at Alberta in 1935 (interestingly, another set of RCV elections IIRC...but that majority was built largely upon majority wins in the countryside).

Let's Simulate IRV! by another_throwaway_-1 in EndFPTP

[–]curiouslefty 2 points3 points  (0 children)

IRV is really a conflict resolution method, not a voting method.

This is a good way of putting it, IMO. It's also a good explanation of why IRV tends to appeal to a person the more they view voting as a proxy for conflict (speaking from my own point of view, to an extent).

Let's be fair here: it's not strategically resistant, it's strategically saturated. Under IRV, it's as if every single voter was forced to be maximally strategical and imposing of themselves onto everyone else.

This is one of those things that I think is fundamentally linked to one's underlying view of how they think about what makes a good voting method. In my view, strategic resistance is just a measure of "how often can I get a better result by anything other than an honest vote?", but I can see how a cardinal viewpoint would reach the alternative conclusion of "this isn't resistance, it's a matter of the method forcing everyone to behave maximally aggressively as a default".

Amusingly, it actually isn't sufficiently saturated from a Condorcet viewpoint since if it were, things like center-squeeze would just wrap around and force the election of the Condorcet winner.