tags:

views:

99

answers:

2

Hello, 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

+4  A: 

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 }  
cfern
Nice answer. I guess where the OP asks for a "built-in mechanism" that would really only work if there weren't any custom record types involved, but as it is this solution looks as elegant as it gets.
Alexander Rautenberg
+1  A: 

Agreeing with cfern's answer, but was wondering if this might benefit from seeing another "built-in" function used. Here's an alternative version of the splitRecord function that shows the library call for unfolding a sequence. No gain here other than having an example for 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
Alexander Rautenberg
Interesting solution :) Of course I find in almost all cases, `while whatever do yield value` is almost always more readable than Seq.unfold :)
Juliet
@Juliet I find that trying to see a case for library functions everywhere helps remember them should they become useful. :)
Alexander Rautenberg