Coder Social home page Coder Social logo

Comments (5)

ndsvw avatar ndsvw commented on August 28, 2024

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.

phillip-haydon avatar phillip-haydon commented on August 28, 2024

I think I prefer approach 1.

from fluent-random-picker.

phillip-haydon avatar phillip-haydon commented on August 28, 2024

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.

ndsvw avatar ndsvw commented on August 28, 2024

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.

ndsvw avatar ndsvw commented on August 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.