开发者

Unifying (tweets/recordtypes) from differents sources in F#

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:

  • an interface but then I wil lose data when upcasting ( also I cant seem to get a record type to implement an intertface )
  • Creating automaticaly a record type with Reflection with all proprietes opt开发者_Go百科ion type except the ones that are shared, and then map all the record types to this one, but that does not seem very natural

Any ideas?

Thanks


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>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜