我正在Haskell中学习'Either'数据结构,并编写了以下内容:
let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData)
if var bptr = bufferPointer {
for i in 0..<(Int(frameCount)) {
let oneSampleI16 = bptr.assumingMemoryBound(to: Int16.self).pointee
// do something with the audio sample
bptr += 1
}
}
是否有任何改进使这段代码更具可读性?
我很欣赏这是一个微不足道的例子,但我只是在这个阶段学习。
答案 0 :(得分:2)
这是
if foldRes == True then
偶然,或者你发现它比仅仅
更具可读性if foldRes then
在后一种情况下,我建议
if (foldRes == True) == True then
,通过扩展,它必须更具可读性。
答案 1 :(得分:1)
我会这样写:
import Data.Maybe (isJust)
checkPhoneBook :: String -> PhoneBook -> PresentInPhoneBook
checkPhoneBook name pb = isJust (lookup name pb)
没有理由让checkPhoneBook
返回Either IsPresent IsNotPresent
。只需返回PresentInPhoneBook
。在这种情况下,也可以返回Bool
。另请注意https://www.xxxx.com/api/pay?paymentId=PAY-8SE27150X9535431TK7PDQJA&token=EC-6GU51565SC426212J&PayerID=HQV9ASU3RRW5S(我在上面使用)。它返回Maybe
值,因此您可以使用它来查找名称以及确定电话簿中是否存在名称。