[英]Sharing functions across different types
我們有一個etl進程編寫,如果f#采用在關系數據庫中排序的數據並將其轉換為為第三方平台准備的星型模式。 因為我們是非規范化的數據,我們(幾乎)復制了分散在我們系統周圍的對象,類型和屬性。 到目前為止,我對此感到滿意,因為對象的不同足以保證不同的功能,或者我們已經能夠將公共/共享屬性分組到子記錄中。
但是,我們現在添加的對象需要選擇系統的不同部分,而不是屬於現有的公共分組。 在嘗試了幾種不同的風格后,我已經開始使用接口,但是使用它們感覺不對。 有沒有人遇到過這個問題並想出一個不同的方法?
module rec MyModels =
type AccountType1 =
{ Id : int
Error : string option
Name : string option }
// PROBLEM: this get very bulky as more properties are shared
interface Props.Error<AccountType1> with member x.Optic = (fun _ -> x.Error), (fun v -> { x with Error = v })
interface Props.AccountId<AccountType1> with member x.Optic = (fun _ -> x.Id), (fun v -> { x with Id = v })
interface Props.AccountName<AccountType1> with member x.Optic = (fun _ -> x.Name), (fun v -> { x with Name = v })
type AccountType2 =
{ Id : int
Error : string option
AccountId : int
AccountName : string option
OtherValue : string }
interface Props.Error<AccountType2> with member x.Optic = (fun _ -> x.Error), (fun v -> { x with Error = v })
interface Props.AccountId<AccountType2> with member x.Optic = (fun _ -> x.AccountId), (fun v -> { x with AccountId = v })
interface Props.AccountName<AccountType2> with member x.Optic = (fun _ -> x.AccountName), (fun v -> { x with AccountName = v })
interface Props.OtherValue<AccountType2> with member x.Optic = (fun _ -> x.OtherValue), (fun v -> { x with OtherValue = v })
module Props =
type OpticProp<'a,'b> = (unit -> 'a) * ('a -> 'b)
// Common properties my models can share
// (I know they should start with an I)
type Error<'a> = abstract member Optic : OpticProp<string option, 'a>
let Error (h : Error<_>) = h.Optic
type AccountId<'a> = abstract member Optic : OpticProp<int, 'a>
let AccountId (h : AccountId<_>) = h.Optic
type AccountName<'a> = abstract member Optic : OpticProp<string option, 'a>
let AccountName (h : AccountName<_>) = h.Optic
type OtherValue<'a> = abstract member Optic : OpticProp<string, 'a>
let OtherValue (h : OtherValue<_>) = h.Optic
[<RequireQualifiedAccess>]
module Optics =
// Based on Aether
module Operators =
let inline (^.) o optic = (optic o |> fst) ()
let inline (^=) value optic = fun o -> (optic o |> snd) value
let inline get optic o =
let get, _ = optic o
get ()
let inline set optic v (o : 'a) : 'a =
let _, set = optic o
set v
open MyModels
open Optics.Operators
// Common functions that change the models
let error msg item =
item
|> (Some msg)^=Props.Error
|> Error
let accountName item =
match item^.Props.AccountId with
| 1 ->
item
|> (Some "Account 1")^=Props.AccountName
|> Ok
| 2 ->
item
|> (Some "Account 2")^=Props.AccountName
|> Ok
| _ ->
item
|> error "Can't find account"
let correctAccount item =
match item^.Props.AccountName with
| Some "Account 1" -> Ok item
| _ ->
item
|> error "This is not Account 1"
let otherValue lookup item =
let value = lookup ()
item
|> value^=Props.OtherValue
|> Ok
// Build the transform pipeline
let inline (>=>) a b =
fun value ->
match a value with
| Ok result -> b result
| Error error -> Error error
let account1TransformPipeline lookups = // Lookups can be passed around is needed
accountName
>=> correctAccount
let account2TransformPipeline lookups =
accountName
>=> correctAccount
>=> otherValue lookups
// Try out the pipelines
let account1 =
({ Id = 1; Error = None; Name = None } : AccountType1)
|> account1TransformPipeline ()
let account2 =
({ Id = 1; Error = None; AccountId = 1; AccountName = None; OtherValue = "foo" } : AccountType2)
|> account2TransformPipeline (fun () -> "bar")
我試過的其他事情:
我不確定如何使您的解決方案更簡單 - 我認為在您的方法中使用類型非常花哨會使代碼變得非常復雜。 在保持某種打字的同時,可能還有其他簡化方法。 同樣,我認為有些情況下你需要實現的邏輯是相當動態的,然后使用一些更動態的技術可能是值得的,即使在F#中也是如此。
舉一個例子,這里是使用Deedle數據框庫執行此操作的示例 。 這使您可以將數據表示為數據框(列名稱為字符串)。
在數據框中編寫所需的兩個清理操作相對容易 - 庫已針對基於列的操作進行了優化,因此代碼結構與您的有點不同(我們計算新列,然后將其替換為所有行)數據框):
let correctAccount idCol nameCol df =
let newNames = df |> Frame.getCol idCol |> Series.map (fun _ id ->
match id with
| 1 -> "Account 1"
| 2 -> "Account 2"
| _ -> failwith "Cannot find account")
df |> Frame.replaceCol nameCol newNames
let otherValue newValue df =
let newOther = df |> Frame.getCol "OtherValue" |> Series.mapAll (fun _ _ -> Some newValue)
df |> Frame.replaceCol "OtherValue" newOther
然后,您的管道可以記錄,將它們轉換為數據幀並執行所有處理:
[ { Id = 1; Error = None; Name = None } ]
|> Frame.ofRecords
|> correctAccount "Id" "Name"
[ { Id = 1; Error = None; AccountId = 1; AccountName = None; OtherValue = "foo" } ]
|> Frame.ofRecords
|> correctAccount "Id" "AccountName"
|> otherValue "bar"
這種方法比你的方法更不安全,但我相信人們實際上可以閱讀代碼並很好地了解它的作用,這可能值得權衡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.