Reputation: 1437
I am getting tweets from three different sources, and each source has more or less data, each source gives me a seq of record types. Let's say:
type Tweet1 = { id:int64; text:string;}
type Tweet2 = { id:int64; text:string; user_name:string}
type Tweet3 = { id:int64; text:string; user_name:string; date:DateTime}
How does one go about to unify the three sequences, seq<Tweet1>, seq<Tweet2>, seq<Tweet3>
?
I was thinking about using:
Any ideas?
Thanks
Upvotes: 0
Views: 89
Reputation: 243051
Probably the easiest option is to define a record that contains all information, but those that may be missing are stored as option
values (and will be filled with None
when the information is not available):
type Tweet =
{ id:int64; text:string;
user_name:option<string>; date:option<DateTime> }
Another way I can think of is to store information about tweets using a data structure like this:
type TweetInfo =
| Tweet of int64 * string
| WithUser of string * TweetInfo
| WithDate of DateTime * TweetInfo
The values from the first record would be turned into Tweet(id, text)
and the values of the last record would be turned into WithDate(posted, WithUser(user, Tweet(id, text)))
. A collection of type list<TweetInfo>
can combine both plain tweets as well as tweets with additional information.
Architecturally, this is a bit similar to the decorator design pattern, because you can add additional information to the basic tweet structure. This probably isn't more useful than the basic record, but you can use this trick to add more generality.
You can also add members to extract the properties (using the member syntax). A date and user information would probably return option<DateTime>
and option<string>
.
Upvotes: 2