Getting prices and volume I'm learning F# and have a couple of routines much of whose functionality looks common so I am looking to refactor them together.

Here are the routines (which for the record I lifted from elsewhere:

let Prices time ID (polling:float) = 
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event&lt;int&gt;() 
  let raiseEvent (value:int) = sync.Post((fun _ -&gt; obs.Trigger(value)), null)  
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop nextTime= async { 
    // Generate next value (on the GUI thread)    
    do getPrices nextTime
      |&gt; List.filter (fun price -&gt; price.ID = ID)
      |&gt; List.iter (fun price -&gt; raiseEvent price.Price)                        
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |&gt; Async.Start
    obs.Publish


and 

let Volumes time ID (polling:float) = 
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event&lt;VolumeDTO&gt;() 
  let raiseEvent (value:VolumeDTO) = sync.Post((fun _ -&gt; obs.Trigger(value)),  null)  
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop nextTime= async { 
    // Generate next value (on the GUI thread)    
    do getVolumes nextTime
      |&gt; List.filter (fun volume -&gt; volume.ID = ID)
      |&gt; List.iter (fun volume -&gt; raiseEvent volume)                        
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |&gt; Async.Start 
    obs.Publish


The external function calls are hopefully reasonable self explanatory retrieving data from a db

let getPrices lastTime =
  createList&lt;PriceDTO&gt;((getPriceSql lastTime), priceReader)

let getVolumes lastTime =
  createList&lt;VolumeDTO&gt;((getVolumeSql lastTime), volumeReader)


The types are very simple, but I'm not sure I've got this 'right':

type IID =
  abstract member ID : int64

type PriceDTO(ID:int64, Price:int) = 
  interface IID with
    member this.ID = ID
  member x.ID = ID
  member x.Price = Price

type VolumeDTO(ID:int64, Amount:decimal, Price:int32) = 
  interface IID with
    member this.ID = ID
  member x.ID = ID
  member x.Amount = Amount
  member x.Price = Price


I've played around with something but I'm not at all there as yet:

let publish&lt;'a&gt; time ID (polling:float) dataRetriever = 
  let sync = System.Threading.SynchronizationContext.Current
  let obs = new Event&lt;'a&gt;() 
  let raiseEvent (value:'a) = sync.Post((fun _ -&gt; obs.Trigger(value)), null)  
  let interval = TimeSpan.FromSeconds(polling)
  let rec loop (nextTime:DateTime)= async { 
    // Generate next value (on the GUI thread)    
    do dataRetriever nextTime
      |&gt; List.filter (fun item -&gt; item.ID = ID)
      |&gt; List.iter (fun item -&gt; raiseEvent item)                        
    // Wait some short time
    do! Async.Sleep(1000)
    // Continue looping
    do! loop (nextTime.Add(interval)) }
    loop time |&gt; Async.Start 
    obs.Publish


I may well have not quite implemented this correctly. I'm obviously getting a list of prices or volumes filtering them and publishing them. In order to achieve the filtering I've created an interface, but I'm not really happy with this since I'd prefer to keep the DTOs as simple as possible. Intellisense is telling me that my dataRetriever function takes a DateTime and returns an 'a list, but I'm not sure I quite want that since I need to cast it to an IID interface for filtering and then back to the underlying DTO for publishing.

Is there a more 'functional' (or perhaps just better) way to do this?
This is a bit difficult to answer, because you did not share code sample that fully type-checks (and so it is hard to make sure the answer is correct). However, I think you're very close. When refactoring two similar functions, you just need to abstract out the bits that differ.

In your case, there are two places:


One is the function that is used to retrieve the data (either getPrices or getVolumes) 
Second is the projection that is used when triggering the event (either item or item.Price)


If you take these two operations as functions, then you can write the following:

open System

type IID = 
  abstract member ID : int64 

let Publish (dataRetriever:TimeSpan -&gt; list&lt;#IID&gt;) valueSelector time ID (polling:float) =  
  let sync = System.Threading.SynchronizationContext.Current 
  let obs = new Event&lt;int&gt;()  
  let raiseEvent (value:int) = sync.Post((fun _ -&gt; obs.Trigger(value)), null)   
  let interval = TimeSpan.FromSeconds(polling) 
  let rec loop nextTime= async {  
    // Generate next value (on the GUI thread)     
    dataRetriever nextTime 
    |&gt; List.filter (fun item -&gt; item.ID = ID) 
    |&gt; List.iter (fun item -&gt; raiseEvent (valueSelector item))                         
    // Wait some short time 
    do! Async.Sleep(1000) 
    // Continue looping 
    do! loop (nextTime.Add(interval)) } 
  loop time |&gt; Async.Start 
  obs.Publish 


The type annotation TimeSpan -&gt; list&lt;#IID&gt; is interesting. It says that the argument is a function that returns a list of some values that implement the IID interface - but it turns this type into a generic type argument that is then passed to the other function (valueSelector), so when you use the function, the compiler knows that the argument of valueSelector has the same type as the thing that you obtain from dataRetriever. The annotation allows you to write item.ID in the filtering function.

The two functions can now be defined as follows:

let Prices = Publish getPrices (fun price -&gt; price.Price)
let Volumes = Publish getVolumes id