我想根據記錄中的特定值執行排序。因此,我正在考慮將鏡頭傳遞到lensSort
函數,但我一直無法使其工作。將鏡頭傳遞到函數
理想的情況下,我可以做這樣的事情
lensSort :: HasLens a => Lens' a b -> a -> a -> -> Ordering
lensSort lens x y | x ^. lens > y ^. lens = GT
| x ^. lens < y ^. lens = LT
| otherwise = GT
並能夠用東西把它像
data Rectangle = Rectangle { _height :: Int, _width :: Int }
makeLenses'' Rectangle
let foo = [Rectangle 1 2, Rectangle 2 1]
sortBy (lensSort height) foo
我無法得到這個工作,並擔心我可能會吠叫完全錯誤的樹,我還是Haskell的新手。
用'比較'做這件事很棒,謝謝 – eddiec