- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Haskell 中有以下程序:
processDate :: String -> IO ()
processDate date = do
...
let newFlattenedPropertiesWithPrice = filter (notYetInserted date existingProperties) flattenedPropertiesWithPrice
geocodedProperties <- propertiesWithGeocoding newFlattenedPropertiesWithPrice
propertiesWithGeocoding :: [ParsedProperty] -> IO [(ParsedProperty, Maybe LatLng)]
propertiesWithGeocoding properties = do
let addresses = fmap location properties
let batchAddresses = chunksOf 100 addresses
batchGeocodedLocations <- mapM geocodeAddresses batchAddresses
let geocodedLocations = fromJust $ concat <$> sequence batchGeocodedLocations
return (zip properties geocodedLocations)
geocodeAddresses :: [String] -> IO (Maybe [Maybe LatLng])
geocodeAddresses addresses = do
mapQuestKey <- getEnv "MAP_QUEST_KEY"
geocodeResponse <- openURL $ mapQuestUrl mapQuestKey addresses
return $ geocodeResponseToResults geocodeResponse
geocodeResponseToResults :: String -> Maybe [Maybe LatLng]
geocodeResponseToResults inputResponse =
latLangs
where
decodedResponse :: Maybe GeocodingResponse
decodedResponse = decodeGeocodingResponse inputResponse
latLangs = fmap (fmap geocodingResultToLatLng . results) decodedResponse
decodeGeocodingResponse :: String -> Maybe GeocodingResponse
decodeGeocodingResponse inputResponse = Data.Aeson.decode (fromString inputResponse) :: Maybe GeocodingResponse
最佳答案
值得记录的是,这竟然是一个 simple streaming problem因使用 mapM
而引起的和 sequence
, 其中与 replicateM
和 traverse
以及其他让你“从IO中提取列表”的事情总是引起积累的担忧。所以需要绕开流媒体库。所以在 repo 中只需要替换
processDate :: String -> IO ()
processDate date = do
allFiles <- listFiles date
allProperties <- mapM fileToProperties allFiles
let flattenedPropertiesWithPrice = filter hasPrice $ concat allProperties
geocodedProperties <- propertiesWithGeocoding flattenedPropertiesWithPrice
print geocodedProperties
propertiesWithGeocoding :: [ParsedProperty] -> IO [(ParsedProperty, Maybe LatLng)]
propertiesWithGeocoding properties = do
let batchProperties = chunksOf 100 properties
batchGeocodedLocations <- mapM geocodeAddresses batchProperties
let geocodedLocations = fromJust $ concat <$> sequence batchGeocodedLocations
return geocodedLocations
import Streaming
import qualified Streaming.Prelude as S
processDate :: String -> IO ()
processDate date = do
allFiles <- listFiles date -- we accept an unstreamed list
S.print $ propertiesWithGeocoding -- this was the main pain point see below
$ S.filter hasPrice
$ S.concat
$ S.mapM fileToProperties -- this mapM doesn't accumulate
$ S.each allFiles -- the list is converted to a stream
propertiesWithGeocoding
:: Stream (Of ParsedProperty) IO r
-> Stream (Of (ParsedProperty, Maybe LatLng)) IO r
propertiesWithGeocoding properties =
S.concat $ S.concat
$ S.mapM geocodeAddresses -- this mapM doesn't accumulate results from mapquest
$ S.mapped S.toList -- convert segments to haskell lists
$ chunksOf 100 properties -- this is the streaming `chunksOf`
-- concat here flattens a stream of lists of as into a stream of as
-- and a stream of maybe as into a stream of as
ghc
忘记这一切并继续前进:
pipes
来完成。或
conduit
.但在这里我们只需要一点简单的
mapM
/
sequence
/
traverse
/
replicateM
避免和
streaming
对于这种快速的本地重构来说,这可能是最简单的。请注意,这个列表很短,所以想法“但是短列表很酷
mapM
”/
traverse
/etc !"可能是非常错误的。为什么不干脆摆脱它们?每当您要编写列表
mapM f
时,最好考虑
S.mapM f . S.each
(或等效的导管或管道)。您现在将拥有一个流,可以使用
S.toList
或等效项恢复列表,但很可能在这种情况下,您会发现您不需要具体化的累积列表,但可以例如使用一些流处理,如打印到文件或在制作任何需要操作的列表之后,标准输出或将内容写入数据库(这里我们使用例如流
filter
和
concat
来扁平化流列表,并作为一种
catMaybe
)。
关于haskell - 减少 Haskell 程序的内存使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905139/
我希望有人能解释为什么我的应用程序在加载时使用不同数量的 RAM。我说的是直接使用 exe 的编译版本。这是一个非常基本的应用程序,在应用程序的启动中没有条件分支。然而,每次我启动它时,RAM 量都在
我有一个 inode 使用率为 100% 的磁盘驱动器(使用 df -i 命令)。但是在大量删除文件后,使用率仍然是 100%。 那么正确的做法是什么? 磁盘空间使用量较少的磁盘驱动器怎么可能有Ino
假设我与分支有一个智能合约,其中每个分支都有不同数量的操作。 if (someCondition) { // do operations costing 10 gas } else { //d
是否有一种工具可以在提交到 NEAR 网络之前估算合约调用将产生多少 gas? 最佳答案 目前最好的估计是用runtime-standalone ,它可以处理交易而不必担心共识/网络。这意味着您可以创
我正在使用最新的 SDK 开发适用于 Windows Azure 的应用程序。 目前我正在使用缓存实现 session 提供程序,但模拟器完全不成比例: 缓存被实现为“非常小的”辅助角色(最大 768
我正在为我的 Logstash RAM 问题寻找答案,因为它几乎是 100%。我为它做了很多搜索,但他们没有为我工作。下面的代码是我的 logstash.conf 文件。我认为它需要一些小改动。 Lo
我已经阅读了这里有关此问题的所有其他问题以及互联网其他地方的许多文章。根据this site ,最大内存使用量遵循以下公式: Max memory = [-Xmx] + [-XX:MaxPermSiz
我需要以编程方式增加 Android 中的 RAM 使用量,以观察它随时间的变化。我该怎么做? 我尝试生成很多自定义对象,但生成后意外地 RAM 使用量减少了...我更改了对象数量、结构等,但没有任何
我们使用带有 MMFiles 存储引擎的 ArangoDB 3.3.14(社区版)来处理相对较大的数据集(备份时会超过 30 GB)。我们使用 ECS 在 Docker 容器内运行它。我们的主机虚拟机
我收到了 Hostgator 发来的一条说明,表明他们限制了我对 MYSQL 的访问,因为我的网站使用了太多资源。 通常,在这种情况下,我只会恢复备份以查看最近的更改是否会产生错误。但是,除了写了一些
我使用 TMimeMess 来解码基于 SMTP 服务器的传入电子邮件在突触上。 我发现用于解码 50MB MIME 消息(带有附件),TMimeMess 使用了 600-800MB 的内存。 在这里
我正在打包适用于 iOS 和 Android 的 Adobe Air 应用程序,并且我的 (RAM) 内存使用量超过 100MB。我的游戏(RPG)中有大量 Assets 。 Assets 的数量
我是一名优秀的程序员,十分优秀!