English 中文(简体)
我如何以类似方式命令档案 筛选等过滤器
原标题:How do I order files with like sortOn and filters like select
  • 时间:2012-01-16 01:27:34
  •  标签:
  • haskell

我在开展一个项目时,利用外来图书馆和Haskell语言创建人民国防军,我对设立命令档案的职能有一些怀疑。 我的工作产生于人民抵抗力量现在缺乏分类职能。 例如,我的达特案档案中载有这些档案(肌肉、录像)。

Files {files = [{filename = Video "-4th_dan trailer.mp4" size = "15 MB" ftype = "MP4" copyright = "-" height = "-", width = "720"}
Video {filename = "TheLostInterview.mp4" size = "73 MB" ftype = "MP4" copyright = "-" height = "Bruce_Lee_-_The_Lost_Interview.avi" width = "240"}
Audio {filename = "8bp017-08-nullsleep-humdrumz.mp3" size = "1984 kb", ftype = "MPG / 3" copyright = "-", title = "humdrumz" artist = "nullsleep", year = "2001"}
Audio {filename = "8bp017-04-nullsleep-fluffy_nougat.mp3" size = "1501 kb", ftype = "MPG / 3" copyright = "-", title = "fluffy nougat," artist = "nullsleep" year = "2001"}

我现在通过<代码>等职能。 (Ord b) => (a -> b) -> [a] -> [a]select(((> 500) 体积(dat file)得按大小、年份、艺术家......编排这些档案。

Now one problem is the size because I want to sort by size and the size he is set to "15 MB" the MB has to be able to sort out there to `

最佳回答

If I understood correctly, you want 15 MB to be considered bigger than 1984 kb.

这样做的一种方式是使用<代码>ortBy。 而不是<代码> 关于。 sortBy 采用了比较功能和分类。 因此,你只能写出一种能够适当分类数值的职能,如<代码>15 mb。

然而,这不是这样做的最佳方式。 相反,我建议你把所有规模都正常化到一个单位(kb,或许)。 因此,通过并转换15 MB,将其储存成编号。 然后,当你需要印刷尺寸时,其功能需要一些千兆字,并预先确定。 这将使你能够轻松地确定规模。

这样做的方法是建立<条码>。 类型:

newtype Size  = Size Integer deriving (Eq, Ord)

接着,您可以把它作为<代码>Show的一个实例,以便进行大体印刷工作:

instance Show Size where
  show (Size s)
    | s < 1000 = show s ++ " kB"
    | s < 1000000 = show (s `div` 1000) ++ " mB"
    | otherwise = show (s `div` 1000000) ++ " gB"

为了从投入体中抽取规模,你可以把它作为<代码>的范例。 见。

instance Read Size where
  readsPrec _ str = do (size, rest)  <- reads str
                       (unit, rest ) <- lex rest
                       let multiplier = fromMaybe 1 $ lookup unit unitSizes
                       return (Size $ multiplier * size, rest )
    where unitSizes = [("mB", 1000), ("gB", 1000000)]

仅通过在<代码>unitSizes上添加更多的奶制品,即可列入单位名称的变数(例如“mB”和“MB”)。 名单上没有的单位缩略语只是被忽视的。

Edit: 采用Daniel Wagner的建议,使代码变得更糟。

附加说明:

一旦您界定了<代码>Size,并使其成为所有此类类别的一个实例,你就可以使用正常类型。 您可使用<条码>>><>>>>>>10 mB>等物品,以达到尺寸。 如果您在原始数据类型上使用deriving (show),那么在您用取代你的方位时,该方仍应继续工作。 缩略语

问题回答

暂无回答




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?