下面提供了問題域的描述
給定一個TimeStamp定義為...
type TimeStamp = Int
一個DataPoint定義為...
data DataPoint a = DataPoint {index :: TimeStamp, value :: a} deriving (Show, Eq)
instance Foldable DataPoint where
foldMap f (DataPoint _ y) = f y
一個Series定義為...
data Series a = Series [DataPoint a]
instance Foldable Series where
foldMap f (Series xs) = foldMap (foldMap f) xs
length = size
和以下助手:
emptySeries :: Series a
emptySeries = Series []
timeSeries :: [(TimeStamp, a)] -> Series a
timeSeries xs = Series $ map (uncurry DataPoint) xs
size :: Series a -> Int
size (Series xs) = length xs
timeSeries給定一組不規則的資料,我如何創建一個指定的解析度。結果timeSeries應包含給定解析度下每個時間點的最新值;為了提高可訪問性,最新資料位于timeSeries. 例如...
如果irrData = [(98,5), (96,4), (93,9)],結果timeSeries將是[(98,5), (97,4), (96,4), (95,9), (94,9), (93,9)]
可以的話加分...
- 使用相同的函式將 a 重新采樣
timeSeries到不同的解析度 - 正確決議以任何順序提供的資料(例如 [(98,5), (101,2), (93,4)])。
下面提供了我當前的解決方案嘗試,并且會隨著時間的推移而改變,因為我正在努力實作它的最終形式
以下解決方案按預期運行;我仍然想清理代碼,因為一行很長。有關詳細資訊,請參閱發布的答案。
resample :: Int -> [(Int, v)] -> [(Int, v)]
resample _ [] = []
resample _ [x] = [x]
resample r xs = foldr (\i acc -> (i, snd $ head (filter (\x -> (fst x) <= i) s)):acc) [] [(fst $ head s), (fst $ head s) - r .. (fst $ last s)]
where
s = sortBy (flip $ on compare fst) xs
uj5u.com熱心網友回復:
resample :: Int -> [(Int, v)] -> [(Int, v)]
resample _ [] = []
resample _ [x] = [x]
resample r xs = foldr (\i acc -> (i, snd $ head (filter (\x -> (fst x) <= i) s)):acc) [] [(fst $ head s), (fst $ head s) - r .. (fst $ last s)]
where
s = sortBy (flip $ on compare fst) xs
給定一個Int解析度和[(Int, v)]系列,它將回傳一個重新采樣的系列。結果系列受到原始系列的限制,點結構代表新的解析度;每個點的值代表原始系列的最新值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490529.html
標籤:哈斯克尔
