在 Excel-Dna 中排序 Excel "variants" 的 F# 函数
F# function to sort Excel "variants" in Excel-Dna
尝试对一维变体数组进行排序时(这里的 "variant" 我指的是所有 Excel 类型,例如 bool、double(和日期)、字符串、各种错误...)具有以下功能:
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
arr
|> Array.sort
我收到以下错误:Error FS0001 The type 'obj' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface
,可能意味着没有适用于所有 obj 类型的通用排序函数。
但是 Excel 有一个自然的排序功能,我想效仿(至少大概)。例如 double (and dates) < string < bool < error...
我的问题:在 F# / Excel-Dna 中对 "variants" 数组进行排序的惯用方法是什么? (我正在寻找一个函数,它接受一个 obj[]
和 return 一个 obj[]
,没有别的,不是宏...)
我的(临时的?)解决方案:
我创建了一个“受歧视的联盟”类型
type XLVariant = D of double | S of string | B of bool | NIL of string
(不太确定 NIL 是否必要,但它没有坏处。另外在我的现实生活代码中,我添加了一个 DT of DateTime
实例,因为我需要区分日期和双打)。
let toXLVariant (x : obj) : XLVariant =
match x with
| :? double as d -> D d
| :? string as s -> S s
| :? bool as b -> B b
| _ -> NIL "unknown match"
let ofXLVariant (x : XLVariant) : obj =
match x with
| D d -> box d
| S s -> box s
| B b -> box b
| NIL _ -> box ExcelError.ExcelErrorRef
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
arr
|> Array.map toXLVariant
|> Array.sort
|> Array.map ofXLVariant
(为简单起见,我漏掉了Errors类型,但思路是一样的)
这对我来说似乎更明确一些,因为它只是坚持 CLR 类型系统:
// Compare objects in the way Excel would
let xlCompare (v1 : obj) (v2 : obj) =
match (v1, v2) with
| (:? double as d1), (:? double as d2) -> d1.CompareTo(d2)
| (:? double), _ -> -1
| _, (:? double) -> 1
| (:? string as s1), (:? string as s2) -> s1.CompareTo(s2)
| (:? string), _ -> -1
| _, (:? string) -> 1
| (:? bool as b1), (:? bool as b2) -> b1.CompareTo(b2)
| (:? bool), _ -> -1
| _, (:? bool) -> 1
| _ -> 2
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
Array.sortWith xlCompare arr
尝试对一维变体数组进行排序时(这里的 "variant" 我指的是所有 Excel 类型,例如 bool、double(和日期)、字符串、各种错误...)具有以下功能:
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
arr
|> Array.sort
我收到以下错误:Error FS0001 The type 'obj' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface
,可能意味着没有适用于所有 obj 类型的通用排序函数。
但是 Excel 有一个自然的排序功能,我想效仿(至少大概)。例如 double (and dates) < string < bool < error...
我的问题:在 F# / Excel-Dna 中对 "variants" 数组进行排序的惯用方法是什么? (我正在寻找一个函数,它接受一个 obj[]
和 return 一个 obj[]
,没有别的,不是宏...)
我的(临时的?)解决方案: 我创建了一个“受歧视的联盟”类型
type XLVariant = D of double | S of string | B of bool | NIL of string
(不太确定 NIL 是否必要,但它没有坏处。另外在我的现实生活代码中,我添加了一个 DT of DateTime
实例,因为我需要区分日期和双打)。
let toXLVariant (x : obj) : XLVariant =
match x with
| :? double as d -> D d
| :? string as s -> S s
| :? bool as b -> B b
| _ -> NIL "unknown match"
let ofXLVariant (x : XLVariant) : obj =
match x with
| D d -> box d
| S s -> box s
| B b -> box b
| NIL _ -> box ExcelError.ExcelErrorRef
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
arr
|> Array.map toXLVariant
|> Array.sort
|> Array.map ofXLVariant
(为简单起见,我漏掉了Errors类型,但思路是一样的)
这对我来说似乎更明确一些,因为它只是坚持 CLR 类型系统:
// Compare objects in the way Excel would
let xlCompare (v1 : obj) (v2 : obj) =
match (v1, v2) with
| (:? double as d1), (:? double as d2) -> d1.CompareTo(d2)
| (:? double), _ -> -1
| _, (:? double) -> 1
| (:? string as s1), (:? string as s2) -> s1.CompareTo(s2)
| (:? string), _ -> -1
| _, (:? string) -> 1
| (:? bool as b1), (:? bool as b2) -> b1.CompareTo(b2)
| (:? bool), _ -> -1
| _, (:? bool) -> 1
| _ -> 2
[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
Array.sortWith xlCompare arr