主 - 细节场景。一直在附近。好吧,如果不是永远的话,至少我在70年代用穿孔卡做了FORTRAN的主要细节。它仍然存在 - STO的大量细节问题。Master-Detail场景所需的F#优雅
我找了一个很好的方法来在F#中做主 - 细节识别器,但没有找到它。道歉,如果我错过了,如果是这样,有人可以只回复sto线程或网址?谢谢
这里是我的F# - 新手在F#中做主 - 细节识别器。即:将主/从列表的主/从列表减少为F#列表,其中主从字符串与其详细字符串列表配对。
不寻找代码高尔夫在这里的人。优雅。我本来希望最终得到一些优雅的东西,但下面只是一个简单的递归列表。我的F#新手大脑未能看到如何在这里很好地利用折叠,理解,折叠,地图,活动模式,计算表达式等。
让我们继续讨论在F#中可以做什么。如果在.Net中有一个预先构建的平面文件主细节XML数据加载器,该加载器通过单行.Net调用将master-detail .txt文件转换为.Net XML,这非常有趣,因为它可以用于F#。作为一个具有很长的命令式编程历史的人,我试图坚持不变的F#来练习。但是如果在F#中浸入命令式或可变式代码真的是最好的方式,请解释一下。输出可以是一个元组列表,记录的序列,元组的阵列等
任何意见/反馈....感谢
let testInput =
["master Homer" ; "Doh.."; "Doh!!" ;
"master Has none" ;
"master JoyJoyJoy"; "Yaa!" ; "Yaa!!!"; "Yaa!!!!!!"]
type md = {m: string; d: string list}
member x.addDetail newd = {m = x.m; d = x.d @ [newd]}
static member noMaster = {m = "" ; d = []} // master records can never be null-strings, so "" works here
static member isMaster (L:string) = L.StartsWith("master ")
static member isDetail (L:string) = not (md.isMaster L) // There is no third kind of record - if not a master then it is a detail
let rec masterDetails flatList currentMaster =
if md.noMaster = currentMaster then
match flatList with
| [] -> [] // If no master and no more input: input list was empty and the empty list is the overall result
| h :: t -> if md.isMaster h then // If no master, then head becomes the first master of the run
masterDetails t {m = h; d = []}
else
failwith "Bad input: First record must be a master record"
else
match flatList with
| [] -> [currentMaster] // End of input; return current master as a one-entry-list
| h :: t -> if md.isMaster h then // Head will now replace the current master as the new master
[currentMaster] @ masterDetails t {m = h; d = []}
else // Keep current master; and add detail record to current master's detail list
masterDetails t (currentMaster.addDetail h)
let testSolution = // Required: 1) Preserve order of the master sets. 2) Preserve sort order of details-within-masters.
[{m = "master Homer" ; d = ["Doh.."; "Doh!!" ]};
{m = "master Has none" ; d = [ ]};
{m = "master JoyJoyJoy"; d = ["Yaa!"; "Yaa!!!"; "Yaa!!!!!!"]} ]
let tryIt = masterDetails testInput md.noMaster
let testTry = (tryIt = testSolution)
感谢Tomas。是的,我不喜欢我在帖子中的那两个。 我认为你的版本比稍微简单一些:你撕掉了两个@,撕掉了外部的if-then,将两个匹配合并为一个匹配。增加理解/收益/收益!还有一些当守卫。我的代码路径不需要考虑大约6个代码路径,而是大约4个代码路径。我更喜欢你的。非常感谢您的回复。 是的,它是我正在考虑的.txt文件。在旧相册程序中导出数据以将数据移动到新的Photoshop Elements中。 Voila,每个文件夹的主细节数据的一个txt文件。 – brucer10