我是haskell的新手,我在postgreSQL数据库上摆弄了Groundhog-ORM库。如果我将所有数据库内容放在一个函数中,那么Everythings工作正常。但是,现在我想在不同的函数中分离不同的主题。
{-# LANGUAGE GADTs, TypeFamilies, TemplateHaskell, QuasiQuotes, FlexibleInstances, StandaloneDeriving #-}
module Main where
import System.IO
import Database.Groundhog.TH
import Database.Groundhog.Postgresql
import Data.DateTime
import Data.List.Split (splitOn)
import Control.Monad (mapM_, when, unless)
import Control.Monad.IO.Class (liftIO)
postgres_connection_string = "host=localhost port=5432 user=itsme password=unknown dbname=stocks"
data Symbol = Symbol {
name :: String
} deriving (Show, Eq)
mkPersist defaultCodegenConfig [groundhog|
definitions:
- entity: Symbol
dbName: symbols
schema: stocks
keys:
- name: unique_symbol
constructors:
- name: Symbol
uniques:
- name: unique_symbol
fields: [name]
|]
ensureSymbol :: (PersistBackend m) => String -> m (Symbol)
ensureSymbol sym = do
loadedSymbols <- select (NameField ==. sym)
return (loadedSymbols !! 0)
main = do
contents <- readFile "src\\inputTable.csv"
putStrLn ("Read " ++ show (length contents) ++ " Bytes input data.")
withPostgresqlConn postgres_connection_string . runDbConn $ do
liftIO $ putStrLn "About to ensure table structures."
runMigration $ do
migrate (undefined :: Symbol)
liftIO $ putStrLn "Checking for the symbol."
symbol <- ensureSymbol "EURUSD"
... [ Some more stuff ] ...
正如您所看到的,我已经分离了一个从数据库中读取符号实体的小函数。这个例子编译好了。
但是,该函数名为ensureSymbol
而不是readSymbol
,因此该函数应为
ensureSymbol :: (PersistBackend m) => String -> m (Symbol)
ensureSymbol sym = do
loadedSymbols <- select (NameField ==. sym)
when (null loadedSymbols) (return (Symbol sym))
unless (null loadedSymbols) (return (loadedSymbols !! 0))
但现在我收到一堆错误消息,即使在行
中也是如此loadedSymbols <- select (NameField ==. sym)
以前编译正确。我假设我需要一个额外的类型说明符来使when
和unless
对函数可见,但我不确定。
src\Main.hs:117:40:
Couldn't match type `Symbol' with `()'
In the first argument of `select', namely `(NameField ==. sym)'
In a stmt of a 'do' block:
loadedSymbols <- select (NameField ==. sym)
In the expression:
do { loadedSymbols <- select (NameField ==. sym);
when (null loadedSymbols) (return (Symbol sym));
unless (null loadedSymbols) (return (loadedSymbols !! 0)) }
src\Main.hs:118:40:
Couldn't match expected type `()' with actual type `Symbol'
In the first argument of `return', namely `(Symbol sym)'
In the second argument of `when', namely `(return (Symbol sym))'
src\Main.hs:119:5:
Couldn't match type `()' with `Symbol'
Expected type: m Symbol
Actual type: m ()
In a stmt of a 'do' block:
unless (null loadedSymbols) (return (loadedSymbols !! 0))
In the expression:
do { loadedSymbols <- select (NameField ==. sym);
when (null loadedSymbols) (return (Symbol sym));
unless (null loadedSymbols) (return (loadedSymbols !! 0)) }
也许有一种更优雅的方式来做到这一点?
答案 0 :(得分:0)
when
和unless
都具有相同的类型:
when, unless :: Monad m => Bool -> m () -> m ()
因此,作为第二个参数传递的值必须为m ()
类型。在这两种情况下,您都要通过m Symbol
。看起来您只想使用if
:
ensureSymbol sym = do
loadedSymbols <- select (NameField ==. sym)
if (null loadedSymbols) then (return (Symbol sym)) else (return (loadedSymbols !! 0))
或case
:
ensureSymbol sym = do
loadedSymbols <- select (NameField ==. sym)
case loadedSymbols of
[] -> return (Symbol sym)
s:ss -> return s