F # - Smooth List / Range

I'm new to F # and I wonder how I'm going to flatten the list.

Essentially in the database I store a record with the range min_age and max_age (for brevity, this is a dummy example - I don't mind!). My fields look something like this:

ID, Cost, Savings, min_age, max_age

I essentially have an F # class that acts as a one-to-one mapping with this table โ€” that is, all properties map exactly to the database fields.

What I would like to do is smooth this range. So, instead of a list containing such items:

saving_id = 1, cost = 100, savings = 20, min_age = 20, max_age = 26
saving_id = 2, cost = 110, savings = 10, min_age = 27, max_age = 31

I need a list containing the following items:

saving_id = 1, cost = 100, savings = 20, age = 20
saving_id = 1, cost = 100, savings = 20, age = 21
etc.
saving_id = 2, cost = 110, savings = 10, age = 27
saving_id = 2, cost = 110, savings = 10, age = 28
etc.

Is there a built-in mechanism for smoothing the list this way and / or does anyone know how to achieve this? Thanks in advance,

In JP

+3
2

, Seq.collect. , , Seq.collect .

:

type myRecord =
{ saving_id: int;
  cost: int;
  savings: int;
  min_age: int;
  max_age: int }

type resultRecord =
    { saving_id: int;
      cost: int;
      savings: int;
      age: int }

let records = 
    [ { saving_id = 1; cost = 100; savings = 20; min_age = 20; max_age = 26 }
      { saving_id = 2; cost = 110; savings = 10; min_age = 27; max_age = 31 } ]

let splitRecord (r:myRecord) =
    seq { for ageCounter in r.min_age .. r.max_age -> 
            { saving_id = r.saving_id;
              cost = r.cost;
              savings = r.savings;
              age = ageCounter }
    }

let ageRanges = records |> Seq.collect splitRecord

: !

let thisAlsoWorks = 
    seq { for r in records do yield! splitRecord r }  
+8

cfern, , "" . โ€‹โ€‹ splitRecord, . , Seq.unfold.

let splitRecord (r:myRecord) = 
    Seq.unfold (fun curr_age ->
                    if curr_age <= r.max_age then
                        Some({  saving_id = r.saving_id; 
                                cost = r.cost; 
                                savings = r.savings; 
                                age = curr_age } ,
                                curr_age + 1) 
                    else None)
                r.min_age
+1
source

Source: https://habr.com/ru/post/1771237/


All Articles