我正在实现一个haskell程序,它将文件的每一行与文件中的每一行进行比较.其中可以实现单线程如下
distance :: Int -> Int -> Int
distance a b = (a-b)*(a-b)
sumOfDistancesOnSmallFile :: FilePath -> IO Int
sumOfDistancesOnSmallFile path = do
fileContents <- readFile path
return $ allDistances $ map read $ lines $ fileContents
where
allDistances (x:xs) = (allDistances xs) + ( sum $ map (distance x) xs)
allDistances _ = 0
Run Code Online (Sandbox Code Playgroud)
这将在O(n ^ 2)时间内运行,并且必须始终在内存中保留完整的整数列表.在我的实际程序中,该行包含更多数字,其中我构造了比Int更复杂的数据类型.这给了我必须处理的数据的内存错误.
因此,对上述单线程解决方案有两个改进.首先,加快实际运行时间.其次,找到一种不将整个列表保留在内存中的方法.我知道这需要解析整个文件n次.因此将进行O(n ^ 2)比较,并解析O(n ^ 2)行.这对我来说没问题,因为我宁愿选择一个成功慢的程序而不是一个失败的程序.当输入文件足够小时,我总能驻留在更简单的版本中.
为了使用多个cpu核心,我从Real World Haskell中获取了Mapreduce实现(第24章,可在此处获得).
我修改了书中的分块功能,而不是将整个文件分成块,返回与行相同的块,每个块代表一个元素
tails . lines . readFile
Run Code Online (Sandbox Code Playgroud)
因为我希望程序也可以在文件大小上进行扩展,所以我最初使用的是惰性IO.然而,这与"太多打开的文件"失败了,我在上一个问题中 …
我有一个简单的算法来实现:比较每一行与每一行.每行包含一个数字,比较函数是距离.所有距离的总和是最终结果.
这可以简单地实现如下:
sumOfDistancesOnSmallFile :: FilePath -> IO Integer
sumOfDistancesOnSmallFile path = withFile path ReadMode $ \h->do
distances <- liftM ( (map textRead) ) $ hListToEOF Text.hGetLine h
let subSet = drop offset distances
let emptySubSet = null subSet
return $ if (emptySubSet)
then (0)
else (distancesManyToMany subSet)
hListToEOF :: (Handle -> IO a) -> Handle -> IO [a]
hListToEOF func h = do
element <- func h
atEOF <- hIsEOF h
rest <- case(atEOF) of
True -> return []
False …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jest 和 Avoriaz 验证基于 Vuetify 组件的 Vue 表单的行为。
我可以触发submit.prevent表单,导致预期的行为。
但是click在提交按钮上触发不起作用。
组件:
<template>
<v-form
ref="form"
data-cy="form"
@submit.prevent="login"
>
<v-text-field
id="email"
v-model="email"
label="Email"
name="email"
prepend-icon="mdi-at"
type="text"
required
autofocus
data-cy="email-text"
/>
<v-btn
color="primary"
type="submit"
data-cy="login-btn"
>
Login
</v-btn>
</v-form>
</template>
<script>
export default {
data () {
return {
email: 'test@test.com',
}
},
computed: {},
methods: {
login: function () {
console.log('Logging in')
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
测试设置:
import vuetify from '@/plugins/vuetify'
import { mount } from 'avoriaz' …Run Code Online (Sandbox Code Playgroud) haskell ×2
heap-memory ×1
iterate ×1
javascript ×1
jestjs ×1
mapreduce ×1
profiling ×1
vue.js ×1
vuetify.js ×1