在Haskell标准库中安全`最大`?

时间:2017-05-16 12:44:20

标签: haskell

Haskell的标准库中是否有maximum的安全等价物?

*Main Control.Monad.State Data.List> maximum []
*** Exception: Prelude.maximum: empty list

我试图找到一个(Ord a, Foldable t) => t a -> Maybe a hoogle,但没找到。

3 个答案:

答案 0 :(得分:6)

为了完整起见:将“标准库”要求放宽到“一些非常常用的库”,安全包提供了Safe.Foldable module,其中包含maximumMay功能:

maximumMay :: (Foldable t, Ord a) => t a -> Maybe a

答案 1 :(得分:3)

您可以创建自己的:

maximumMaybe :: (Ord a, Foldable f) => f a -> Maybe a
maximumMaybe xs
  | null xs   = Nothing
  | otherwise = Just $ maximum xs

上一个答案(不可折叠):

maximumMaybe :: Ord a => [a] -> Maybe a
maximumMaybe xs = listToMaybe xs *> Just (maximum xs)

或者如果你喜欢无点风格:

maximumMaybe :: Ord a => [a] -> Maybe a
maximumMaybe = (<*) . Just . maximum <*> listToMaybe

另一个更简单的解决方案是:

maximumMaybe :: Ord a => [a] -> Maybe a
maximumMaybe [] = Nothing
maximumMaybe xs = Just $ maximum xs

答案 2 :(得分:3)

通过将Foldable应用于合适的foldMap选项,您可以轻松为自己Monoid编写一个代码。

The Option monoid获取现有Semigroup并通过毗邻空元素Monoid将其提升为Option NothingfoldMap将返回该元素如果输入Foldable为空。 Ord Semigroup<>的任何实例提升为foldMap,让Foldable选择更大的参数。

通过Option通过MaxsafeMaximum :: (Foldable t, Ord a) => t a -> Maybe a safeMaximum = fmap getMax . getOption . foldMap (Option . Just . Max) ghci> safeMaximum "wowzers" Just 'z' ghci> safeMaximum "" Nothing 的组合对输入def SetThumbSize(self, width, height, border=6): """ Sets the thumbnail size as width, height and border. :param `width`: the desired thumbnail width; :param `height`: the desired thumbnail height; :param `border`: the spacing between thumbnails. """ if width > 350 or height > 280: return self._tWidth = width self._tHeight = height self._tBorder = border self.SetScrollRate((self._tWidth + self._tBorder)/4, (self._tHeight + self._tBorder)/4) self.SetSizeHints(self._tWidth + self._tBorder*2 + 16, self._tHeight + self._tBorder*2 + 8) 进行ping操作,我们得到了您想要的行为。

import wx
import os
import sys

try:
    dirName = os.path.dirname(os.path.abspath(__file__))
except:
    dirName = os.path.dirname(os.path.abspath(sys.argv[0]))

sys.path.append(os.path.split(dirName)[0])

try:
    from agw import thumbnailctrl as TC
except ImportError:  # if it's not there locally, try the wxPython lib.
import wx.lib.agw.thumbnailctrl as TC


class MainFrame(wx.Frame):
def __init__(self, redirect=False, filename=None):
    wx.Frame.__init__(self, None, title="Elephant")

    # self.SetMenuBar(self.CreateMenuBar())

    splitter = wx.SplitterWindow(self, -1, style=wx.CLIP_CHILDREN | wx.SP_3D | wx.WANTS_CHARS | wx.SP_LIVE_UPDATE)
    self.panel = wx.Panel(splitter, -1)

    sizer = wx.BoxSizer(wx.HORIZONTAL)
    scroll = TC.ThumbnailCtrl(splitter, -1, imagehandler=TC.NativeImageHandler)

    scroll.ShowFileNames()
    if os.path.isdir("../bitmaps"):
        scroll.ShowDir(os.path.normpath(os.getcwd() + "/../bitmaps"))
    else:
        scroll.ShowDir(os.getcwd())

    self.TC = scroll

    splitter.SplitVertically(scroll, self.panel, 180)

    splitter.SetMinimumPaneSize(140)
    self.SetMinSize((700, 590))
    self.CenterOnScreen()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    # import wx.lib.inspection
    # wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()
相关问题