- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近开始使用计算表达式来简化我的代码。到目前为止,对我唯一有用的是 MaybeBuilder,定义如下:
type internal MaybeBuilder() =
member this.Bind(x, f) =
match x with
| None -> None
| Some a -> f a
member this.Return(x) =
Some x
member this.ReturnFrom(x) = x
但我想探索其他用途。一种可能是我目前面临的情况。我有一些定义对称矩阵的供应商提供的数据。为了节省空间,只给出了矩阵的三角形部分,因为另一边只是转置。所以如果我在 csv 中看到一行
abc, def, 123
这意味着行 abc 和列 def 的值为 123。但是我不会看到这样的行
def, abc, 123
因为矩阵的对称性已经给出了这些信息。
我已将所有这些数据加载到 Map<string,Map<string,float>>
中我有一个函数可以为我获取任何条目的值,如下所示:
let myLookupFunction (m:Map<string,Map<string,float>>) s1 s2 =
let try1 =
match m.TryFind s1 with
|Some subMap -> subMap.TryFind s2
|_ -> None
match try1 with
|Some f -> f
|_ ->
let try2 =
match m.TryFind s2 with
|Some subMap -> subMap.TryFind s1
|_ -> None
match try2 with
|Some f -> f
|_ -> failwith (sprintf "Unable to locate a value between %s and %s" s1 s2)
既然我了解了计算表达式,我怀疑可以隐藏匹配语句。我可以像这样使用 MaybeBuilder 稍微清理一下
let myFunction2 (m:Map<string,Map<string,float>>) s1 s2 =
let maybe = new MaybeBuilder()
let try1 = maybe{
let! subMap = m.TryFind s1
return! subMap.TryFind s2
}
match try1 with
|Some f -> f
|_ ->
let try2 = maybe{
let! subMap = m.TryFind s2
return! subMap.TryFind s1
}
match try2 with
|Some f -> f
|_ -> failwith (sprintf "Unable to locate a value between %s and %s" s1 s2)
这样一来,我的匹配语句从 4 个减少到 2 个。是否有一种(非人为的)方法可以通过使用计算表达式进一步清理它?
最佳答案
首先,每次需要时创建一个新的MaybeBuilder
有点浪费。你应该这样做一次,最好是紧挨着 MaybeBuilder
本身的定义,然后在任何地方都使用同一个实例。这就是大多数计算构建器的工作方式。
第二:如果您只是将“try”逻辑定义为一个函数并重用它,您可以减少困惑的数量:
let myFunction2 (m:Map<string,Map<string,float>>) s1 s2 =
let try' (x1, x2) = maybe{
let! subMap = m.TryFind x1
return! subMap.TryFind x2
}
match try' (s1, s2) with
|Some f -> f
|_ ->
match try' (s2, s1) with
|Some f -> f
|_ -> failwith (sprintf "Unable to locate a value between %s and %s" s1 s2)
第三,注意您正在使用的模式:试试这个,如果不行就试试那个,如果不行就试试另一个,等等。模式可以抽象为函数(这就是全部!),所以让我们这样做:
let orElse m f = match m with
| Some x -> Some x
| None -> f()
let myFunction2 (m:Map<string,Map<string,float>>) s1 s2 =
let try' (x1, x2) = maybe{
let! subMap = m.TryFind x1
return! subMap.TryFind x2
}
let result =
try' (s1, s2)
|> orElse (fun() -> try' (s2, s1))
match result with
|Some f -> f
|_ -> failwith (sprintf "Unable to locate a value between %s and %s" s1 s2)
最后,我认为你的做法是错误的。您真正想要的似乎是具有两部分对称 key 的字典。那为什么不这样做呢?
module MyMatrix =
type MyKey = private MyKey of string * string
type MyMatrix = Map<MyKey, float>
let mkMyKey s1 s2 = if s1 < s2 then MyKey (s1, s2) else MyKey (s2, s1)
let myFunction2 (m:MyMatrix.MyMatrix) s1 s2 =
match m.TryFind (MyMatrix.mkMyKey s1 s2) with
| Some f -> f
| None -> failwith (sprintf "Unable to locate a value between %s and %s" s1 s2)
这里,MyKey
是一种封装一对字符串的类型,但保证这些字符串“按顺序”——即第一个字符串在字典序上比第二个字符串“少”。为了保证这一点,我将类型的构造函数设为私有(private),而是公开了一个函数 mkMyKey
来正确构造 key (有时称为 "smart constructor" )。
现在您可以自由地使用MyKey
来构建和查找 map 。如果你输入 (a, b, 42)
,你会得到 (a, b, 42)
和 (b, a, 42)
.
旁白:我在您的代码中看到的一般错误是未能使用抽象。您不必在最低级别处理每条数据。该语言允许您定义更高级别的概念,然后根据它们进行编程。使用该能力。
关于F# 计算表达式 : Can one be used to simplify this code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699277/
我是 CAN 协议(protocol)的新手,正在阅读 Robert Bosch 的 CAN 规范 ver2.0 B 部分。我无法理解第 63 页上的以下几行 ”注意:启动/唤醒:如果在启动期间只有一
我用 C 写了一些代码来读取 CAN 总线数据。当我读取 11 位 CAN ID 时一切正常。一旦我尝试读取 29 位 ID,它就会错误地显示 ID。 示例: 接收29位ID的消息: 0x01F0A0
如果这看起来与另一个问题相似或者看起来已经得到回答,我提前道歉。我觉得它非常详细,足以证明自己的问题。 我正在尝试寻找一个虚拟的 CAN 总线模拟器(或一些可以轻松制作模拟器的方法),它只会生成 CA
我的问题涉及 GNU 的品牌。 如果您有一系列命令可用作多个目标的配方,则 canned recipe派上用场了。我可能看起来像这样: define run-foo # Here comes a #
您好,我是一名学习canopen的学生。Canopen中的COB-ID和CAN标识符有什么关系?我在CIA主页上看到COB-ID不是CAN ID,但我不明白。 例如,如果 PDO 通过 CAN 总线传
我知道一个显性确认位是由另一个节点传输的消息的接收器发送的。 我无法理解的是,接收方是在接收到整个消息后发送单个显性位,还是接收者发送相同的消息,其中 ACK 位字段为显性? 或者是接收器在发送器传输
我是 CAN 协议(protocol)的新手,我正在尝试通过 Linux 的 SocketCAN 使用它。然而,我对可用的 2 种不同的 CAN 套接字(RAW 和广播管理器 (BCM))感到困惑。
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在尝试制作一个在 Windows 下运行并与 ELM327 设备通信的软件。我创建了第一个版本,然后我进入了我的 SMART ForTwo (SMART 451) 车辆,我设法连接了仪表盘(发送
我知道在 CAN Controller 中,如果错误计数达到某个阈值(比如 255),就会发生总线关闭,这意味着特定的 CAN 节点将从 CAN 网络中关闭。所以根本不会有任何交流。但是,如果上述情况
我正在使用 ELM327,我希望能够设置要发送的 CAN 消息的 header 和数据部分。我看到有一个代码用于设置消息的标题 SH xxyyzz 但是我很难找出如何设置数据部分并控制何时发送消息。
我想做的是: 将数据插入具有两列的表中,并在同一 PHP 页面中显示更新的值。我能够获取数据并显示它,但无法插入任何数据。请指导我。 文件名为 mypage.php 到目前为止我的代码:
(这个问题是关于 Android 11 的) 我想将崩溃日志打印到其他应用程序可以读取的文件中(具体来说,我希望能够导航到该文件并使用"file"应用程序查看数据)。 我看过很多关于这个问题的答案,但
这会产生“ fatal error :无法解开Optional.None”,我似乎不明白为什么 var motionManager = CMMotionManager() motionManager.
在 Java 中,我经常遇到带有后缀 -able 的接口(interface),例如可序列化、可迭代等。这表明实现这些接口(interface)的对象具有可以对其执行某些操作的特性,例如该对象可以被序
我正在阅读 CanJS API 文档并遇到 can.Construct.extend http://canjs.com/docs/can.Construct.extend.html .我知道 can.
我正在使用 C 语言在 STM32F1xx 上进行开发,直到现在我都在尝试使用“CANopenNode-master”实现 CANopen 堆栈,并且我正在使用 2 个中断。 第一个是用于处理 SYN
我一直在使用 SocketCAN,尤其是 Virtual CAN vcan。但是,到目前为止,我从未使用过 CAN FD(灵活数据速率)。 好吧,我今天早上用 can-utils 试了一下: cans
我正在运行一个带有两个 CAN channel 的程序(使用 TowerTech CAN Cape TT3201)。 两个 channel 是 can0 (500k) 和 can1 (125k)。 c
存储由序列字符组成的字符串的 %s 格式说明符可以存储整数序列吗?如果是的话..你能解释一下吗? 最佳答案 无论如何,数字都是用字符表示的,所以是的,您可以使用 "%s" 说明符读取数字并将其存储在
我是一名优秀的程序员,十分优秀!