Comments (5)
Possibility 1:
var elements = new List<MyValuePriorityPair>();
Out.Of().PrioritizedElements(elements)
.WithValueSelector(x => x.Value)
.AndPercentageSelector(x => x.Percentage)
.PickOne();
Out.Of().PrioritizedElements(elements)
.WithValueSelector(x => x.Value)
.AndWeightSelector(x => x.Weight)
.PickOne();
... and in case someone wants the whole MyValuePriorityPair instance to be returned ...
var elements = new List<MyValuePriorityPair>();
Out.Of().PrioritizedElements(elements)
.WithPercentageSelector(x => x.Percentage)
.PickOne();
Out.Of().PrioritizedElements(elements)
.WithWeightSelector(x => x.Weight)
.PickOne();
- (+) Good readability
- (+) More fluency (it's a fluency project)
- (+) Performance: optimal
- (+-) Difficulty: intermediate
Possibility 2:
var elements2 = new List<(string Value, int Priority)>();
Out.Of().WeightedValues(elements2)
.PickOne();
Out.Of().PercentageValues(elements2)
.PickOne();
- (+) Good readability
- (-) Makes the API harder to use (Out.Of().<Value,Values,WeightedValues,PercentageValues>)
- (+) Performance: optimal
- (+) Difficulty: very easy
Possibility 3:
var elements3 = new List<X>();
Out.Of().WeightedValues(elements3.Select(x => new WeightedValue(x.Value, x.Weight)))
.PickOne();
Out.Of().PercentageValues(elements3.Select(x => new PercentageValue(x.Value, x.Percentage)))
.PickOne();
- (-) Not good readability
- (-) Makes the API harder to use (Out.Of().<Value,Values,WeightedValues,PercentageValues>)
- (+-) Performance: Maybe redundant instantiations
- (+) WeightedValue/PercentageValue could possibly be used internally, too (see ValuePriorityPair
- (-) Difficulty: more difficult (one had to know that the types WeightedValue/PercentageValue exist)
from fluent-random-picker.
I think I prefer approach 1.
from fluent-random-picker.
Is it possible to return the whole element maybe?
Like for example given:
public class Element : IWeightedValue
{
public string Name {get;set;}
public int Value {get;set;}
public int Weight {get;set;}
}
Then you could do something like:
var elements = new List<IWeightedValues>();
Out.Of().Values(elements)
.PickOne();
or without the interface
var elements = new List<Element>();
Out.Of().WeightedValues(elements)
.WithWeightFrom(x => x.Weight)
.PickOne();
And have the element
returned where it can contain all the data.
from fluent-random-picker.
What's possible at the moment:
var vals = new List<IWeightedValues>();
var x = Out.Of().Values(vals).WithWeights(vals.Select(x => x.Weight)).PickOne();
x
is a whole IWeightedValue
.
With the new feature of this issue, this could be possible:
var elements = new List<IWeightedValue>();
var x = Out.Of().PrioritizedElements(elements)
.WithValueSelector(x => x)
.AndWeightSelector(x => x.Weight)
.PickOne();
Maybe, the WithValueSelector should be optional, so that it's not required to write x => x
if you want to return the whole element:
var elements = new List<IWeightedValue>();
var x = Out.Of().PrioritizedElements(elements)
.WithWeightSelector(x => x.Weight)
.PickOne();
x
is a whole IWeightedValue
again.
from fluent-random-picker.
New feature is available. It works with version 3.3.0.
Example:
class Item {
public int Rarity { get; set; }
public string Name { get; set; }
}
var items = new Item[]
{
new Item { Name = "Stone", Rarity = 5 }, // common
new Item { Name = "Silver helmet", Rarity = 2 }, // uncommon
new Item { Name = "Gold sword", Rarity = 1 }, // rare
};
var itemName = Out.Of()
.PrioritizedElements(items)
.WithValueSelector(x => x.Name)
.AndWeightSelector(x => x.Rarity)
.PickOne();
// itemName is "Stone" in 5/8 of the cases, "Silver helmet" in 2/8 of the cases and "Gold sword" in 1/8 of the cases.
// If no value selector is specified, the whole item object will be returned instead of only its name.
from fluent-random-picker.
Related Issues (18)
- Weight doesn't influence random values correctly.
- Percentages less than 1 HOT 7
- Get rid of multiple enumerations of IEnumerables HOT 1
- Allow chaining Values(...).AndValues(...) ?
- Weights using long HOT 3
- Priorities don't matter in rare cases HOT 1
- OverflowException in SecureRandomNumberGenerator.NextInt()
- Support Span, ReadonlySpan, Memory, ReadOnlyMemory, ... HOT 1
- Use Reservoir sampling when 1 element (equal priorities) has to be picked
- Create implementation of `IRandomNumberGenerator` that is cryptographically secure HOT 1
- How can I give a small pool of reference types and pick a large number of items? HOT 3
- NotEnoughValuesToPickException when only one value is passed HOT 2
- Improve documentation in code
- Consider extension methods
- Try improving implementation of StochasticAcceptanceBasedWeightedLeftShuffle HOT 3
- Plan how to name the fluency interfaces consistently. HOT 1
- Improve performance with parallelization HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluent-random-picker.