Differences between ways of creating ImmutableList [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
What is the differences between ways of creating ImmutableList?
List<int> numbers = new List<int>(){0, 1, 2, 3};
ImmutableList<int> immutableList = numbers.ToImmutableList();
ImmutableList<int> immutableList = ImmutableList.Create<int>(0, 1, 2, 3);
ImmutableList<int> immutableList = ImmutableList.CreateRange<int>(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int>.Builder builder = ImmutableList.CreateBuilder<int>();
builder.AddRange(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int> immutableList = builder.ToImmutableList();
Which is the faster and usabel?

Let's take a look at how each of those is implemented.
First off, the ToImmutableList() extension method involves first trying to cast to ImmutableList<T>, and then falling back to ImmutableList<T>.Empty.AddRange() otherwise. ImmutableList.Create() and ImmutableList.CreateRange() also call ImmutableList<T>.Empty.AddRange().
Finally, the builder is the only one that has some key difference, although you're using it wrong: you should be using ToImmutable(), because it involves doing fewer unecessary copies. ToImmutableList() just uses the above extension method, and therefore also uses ImmutableList<T>.Empty.AddRange().
Really, if you're building a possibly large immutable list as you go, you should use a builder, and then freeze it with ToImmutable() when you're done modifying it. Otherwise, all the other methods are functionally identical, so pick whichever is clearest to you.

Related

Pairing random items in lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to make a program to randomize entries for an event. I have the program working well enough for entering the entries but I'm stuck at randomizing it.
I have 2 lists, let's call one Head and the other one Heel. I have the lists as follows:
Head: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Heel: [1a, 2a, 3a, 4a, 5a, 6a, 7a, 8a, 9a, 10a]
I want each item in the Head to have 2 random partners from Heel, but no value from Heel can be matched more than twice at the end of it all. In the final output, each entry should be listed twice and only twice.
Hopefully someone understands and is able to help me, thanks.
I feel like I need to go take a long bath, I feel so dirty for having this piece of code see the light of day, but something about OP's logic was mezmerizing. Anyway, 3 AM me thinks this should work:
var head = new List<char>("abcdef");
var heel = new List<char>("123456");
heel = heel.Concat(heel);
var randomer = new Random();
foreach (var knownItem in head)
{
var idx1 = randomer.Next(heel.Count);
var pair1 = heel[idx1];
heel.RemoveAt(idx1);
char pair2='\0';
while (true)
{
var idx2 = randomer.Next(heel.Count);
pair2 = heel[idx2];
if (pair2 != pair1)
{
heel.RemoveAt(idx2);
break;
}
}
//DoTheDew
}
Next steps for tomorrow: dieharder test the results of this version vs #Arj
Here's a possible solution. Since there's no code to start with, I've done it in pseudocode for now.
Create two Lists, each having all of the values in heel; call them heel1 and heel2
For each element i in head:
Generate a random number j where 0 <= j < heel1.size. Remove the element at heel1[j] - that's your first pairing
Repeat with heel2 (for a different generated j). Remove that element at heel2[j] - that's the second pairing
Store i along with the two removed values
By the end you will have two numbers for each value in head, with no value from heel appearing more than twice. Since we are removing any used values each time, we don't need to check for your "more than two pairings" rule.

Does .NET have a way to sort a list between bounds? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
Simple question so I don't have to reinvent the wheel if I don't have to.
Does .NET have a method equivalent to
// sorts a list in-place between bounds [a, b)
public static void SortBounds<T>(this List<T> list, int a, int b)
{
// ...
}
(possibly with an optional predicate)?
Example:
var list = {1, 5, 3, 1, 9, 2, 4 }
list.SortBounds(0, 4);
// now list is {1, 1, 3, 5, 2, 4 }
var list = new List<int> { 1, 5, 3, 1, 9, 2, 4 };
list.Sort(0, 4,Comparer<int>.Default);

Arrays, Jagged arrays, multidimensional arrays Performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'll make it short:
int[,,,] Stats = new int [6, 4, 30, 10];
This will have 6 * 4 * 30 * 4 = 7200 elements, right?
And performance should be as good as it gets right?
If I loop through that and look for specific numbers by
if (x = y)
this should be faster than doing that with List or other things, right, because internally all is handled like the basic array, if I understand that all correctly?
Arrays are constant, vs List that is dynamic, which means when you make a new array, c# allocate memory for all those elements (in this case 7200x(int size)).
In a list, whenever you make a new entry, c# has to update the list, allocate new memory for your new int and "connect" it to the list, therefore will be slower always.
SO, if you don't care about "wasting" memory, array will be your best choice for performance.

How in c# to convert a string of comma-separated bracket-enclosed (nested) comma-separated values into a List of Lists? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a string like
"(5678, 2, 41, 3), (4852, 8, 3, 97), (4562, 85, 3, 7)"
How in c# can I turn that into a List (or array) of the three parent values, each of which being a List (or array) of its four child values?
I'm thinking that I want to end up with a List<List<int>> if that makes sense.
May not be the best solution but it will work. Here is the code:
string value = "(5678, 2, 41, 3), (4852, 8, 3, 97), (4562, 85, 3, 7)";
var result = value
.Split(')')
.Select(i => i.Replace('(', ' '))
.Select(i => i.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.ToList()).ToList();
I would use a regex to split the string up into (p1), (p2), (p3) and then a simple string.Split() to access to the child values for each parent.

Select Many in Rx [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Please any one let me know how the SelectMany operator in Rx works. I don't know more about this operator in Linq either.
Please explain this with the help of a simple example, and also in what occasion we will use this operator in Rx.
SelectMany is just:
source.Select(selector).Merge();
In other words, it selects the source input into a stream of Observables, then flattens each Observable into a stream of results.
SelectMany combines projection and flattening into a single step. Suppose you have a number of lists like { {1, 2}, {3, 4, 5}, { 6, 7 } } you can use SelectMany to flatten it into a single list like: { 1, 2, 3, 4, 5, 6, 7}
SelectMany in Rx can flatten multiple sequences into one observable (there are actually several overloads).
For a practical example, suppose you have a function DownloadFile(filename) which gives you an Observable which produces a value when the file completes downloading. You can now write:
string[] files = { "http://.../1", "http://.../2", "http://.../3" };
files.ToObservable()
.SelectMany(file => DownloadFile(file))
.Take(3)
.Subscribe(c => Console.WriteLine("Got " + c) , ()=> Console.WriteLine("Completed!"));
All 3 observables of DownloadFile are flattened into one, so you can wait for 3 values to arrive to see that all downloads are completed.
I found this short video helpful in understanding SelectMany for Rx (and as a more advanced use of marble diagrams): http://channel9.msdn.com/Blogs/J.Van.Gogh/Reactive-Extensions-API-in-depth-SelectMany

Categories

Resources