gpt4 book ai didi

f# - 摆脱 F# 中的 "pyramid of doom"

转载 作者:行者123 更新时间:2023-12-03 18:35:50 34 4
gpt4 key购买 nike

我有几个口头表达,我已经打包成一个函数:

open FsVerbalExpressions
open FsVerbalExpressions.VerbalExpression
open System.Text.RegularExpressions
open System

let createOrVerbExFromList (verbExList: VerbEx list) =
let orVerbEx =
verbExList
|> List.reduce (fun acc thing -> verbExOrVerbEx RegexOptions.IgnoreCase acc thing) //simpleVerbEx

orVerbEx

let k12VerbEx =
let kTo12 = ["SCHOOL"; "DIST"; "SD"; "HS"; "BD OF ED"]
kTo12
|> List.map (fun word -> VerbEx(word))
|> createOrVerbExFromList

let twoYearCollegeVerbEx =
VerbEx("2 Year College")

let universityVerbEx =
VerbEx("UNIV")

let privateSchoolVerbEx =
VerbEx("ACAD")

//Here there be dragons:
let newInst (x: string) =
match (isMatch x k12VerbEx) with
| true -> "K - 12"
| _ -> match (isMatch x twoYearCollegeVerbEx) with
| true -> "2 Year College"
| _ -> match (isMatch x universityVerbEx) with
| true -> "University"
| _ -> match (isMatch x privateSchoolVerbEx) with
| true -> "Private / Charter School"
| _ -> "Other"

我想重写 newInst功能,使其不再是“末日金字塔。我的问题是我怎样才能摆脱厄运金字塔?我可以摆脱它吗?我怀疑这将是某种 async 工作流程或其他计算表达式,但这些对我来说都很新。

最佳答案

如果你只匹配 bool 值,那么 if ... elif足够了:

let newInst (x: string) =
if isMatch x k12VerbEx then
"K - 12"
elif isMatch x twoYearCollegeVerbEx then
"2 Year College"
elif isMatch x universityVerbEx then
"University"
elif isMatch x privateSchoolVerbEx then
"Private / Charter School"
else
"Other"

更灵活的可能性是创建一个事件模式:
let (|IsMatch|_|) f x =
if isMatch x f then Some () else None

let newInst (x: string) =
match x with
| IsMatch k12VerbEx -> "K - 12"
| IsMatch twoYearCollegeVerbEx -> "2 Year College"
| IsMatch universityVerbEx -> "University"
| IsMatch privateSchoolVerbEx -> "Private / Charter School"
| _ -> "Other"

关于f# - 摆脱 F# 中的 "pyramid of doom",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858643/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com