ihp - 在没有许多的地方选择一些
问题描述
我正在尝试查找没有任何具有给定名称的用户名的域,即免费域。希望有一个不需要2个单独查询的解决方案,但我也遇到了类型系统的问题。
-- error Couldn't match type 'IO [Domain]' with '[[Domain]]', points to beginning of filter
Just term -> do
usernames <- query @Username
|> filterWhere (#name, term) |> fetch >>= collectionFetchRelated #domainId
domains <- search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
Just _ -> True
Nothing -> False
render IndexView {..}
解决方案
-- error Couldn't match type 'IO [Domain]' with '[[Domain]]', points to beginning of filter
由于此处的箭头符号而发生错误:
domains <- search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
Just _ -> True
Nothing -> False
由于这没有做任何 IO,它需要使用let
:
let domains = search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
Just _ -> True
Nothing -> False
现在的类型domains
是[[Domain]]
(域列表列表)。可能我们想要[Domain]
(只是一个普通的域列表)。
我们可以concat
为此目的使用:
let domains = search
|> filter ( \x-> case find (\y->get #domainId x == get #domainId y) usernames of
Just _ -> True
Nothing -> False
)
|> concat
现在类型错误应该消失了。
希望有一个不需要2个单独查询的解决方案
是的,这也适用于手写查询:
domains <- sqlQuery "SELECT domains.* FROM domains WHERE (SELECT COUNT(*) FROM usernames WHERE usernames.domain_id = domains.id AND usernames.name = ?) = 0" [term]
推荐阅读
- java - Android Studio 4 可以构建 API < 9 的项目吗?
- openapi - 在 C# .Net 5.0 中生成用于 OpenAPI 的强类型类
- ansible - ansible,仅当另一个特定主机也在剧本运行中时才执行任务
- typescript - Typescript“声明命名空间”、“声明模块”在全局/模块范围内的区别
- javascript - 如何在不发送验证电子邮件的情况下使用 NodeJS 验证电子邮件是否存在
- android - 是否可以在android中屏幕共享应用程序的特定屏幕?
- angular - 角定制垫芯片颜色
- c++ - 随机形状和运动生成不绘制形状
- flutter - 在谷歌地图上沿折线搜索标记颤动
- r - 删除任何其他相邻列中具有重复值的行