Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - Flatten List/Range

Tags:

flatten

f#

I'm new to F# and am wondering how I would go about flattening a list.

Essentially in the database I store a record with a min_age and max_age range (this is a fictitious example for the sake of brevity - i am not agist!). My fields look something like the following:

id, cost, savings, min_age, max_age

I essentially have an F# class that acts as a one-to-one mapping with this table - i.e. all properties are mapped exactly to the database fields.

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

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 would like a list containing items like this:

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 any in-built mechanism to flatten a list in this manner and/or does anyone know how to achieve this? Thanks in advance,

JP

like image 308
JP. Avatar asked Jan 21 '23 05:01

JP.


1 Answers

You might want to use Seq.collect. It concatenates sequences together, so in your case, you can map a function over your input that splits a single age range record to a sequence of age records and use Seq.collect to glue them together.

For example:

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

Edit: you can also use a sequence generator with yield!

let thisAlsoWorks = 
    seq { for r in records do yield! splitRecord r }  
like image 73
cfern Avatar answered Jan 29 '23 18:01

cfern