3
spec = describe "Router" $ do

  let sampleRoutes = [( Tuple "/"  "views/index.yaml" ), 
                      ( Tuple "/foo" "views/foo.yaml" ), 
                      ( Tuple "/bar" "views/bar.yaml" )]

  it "should default to the first of the list" $ do
    r <- fst <$> head sampleRoutes
    fprint r

上記は次のエラーをスローします。

Error in declaration spec
Cannot unify Data.Maybe.Maybe with Control.Monad.Eff.Eff u4505.

type の2番目の引数が期待されているためだと思いますが、2番目の引数によって導入されたEffの使用により、代わりに typeになります。MaybeheadMaybe

it :: forall e a. String -> Eff e a -> Eff (it :: It | e) Unit

問題は、これを解決する方法がわかりません。Maybe代わりに効果的なコード ブロックを使用できないでしょうか?

4

2 に答える 2

5

Maybeはブロックで使用できますが、doブロック内のすべてのアクションは、Maybe a一部の のタイプである必要がありaます。

同じことが - にも当てはまります。 withEff effを使用できますが、すべてのアクションは一部のタイプである必要があります。Eff effdoEff eff aa

doブロック内で 2 種類の効果を組み合わせることはできません。

モナドが でMaybe aあるブロック内で type の値を使用したいようです。いくつかのオプションがあります。doEff eff

  • Data.Array.Unsafe.headwhich を使用すると、ラップされていない が得られ、直接Tuple呼び出すことができますfst
  • モナドMaybeでの一連のアクションを決定するための値のパターン マッチ:Eff

    it "should default to the first of the list" $ do
      case head sampleRoutes of
        Nothing -> ... -- Handle empty array
        Just tuple -> fprint (fst tuple) -- Print the first component
      .. rest of do block ..
    
于 2014-07-28T03:12:39.440 に答える
2

traverse_この例では、 fromを利用することも可能Data.Foldableです。

を使用しているためMaybe (Tuple String String)MaybeにはFoldableインスタンスがあり、適用可能なインスタンスがあるため、ではなくEff eを使用できます。traverse_(<$>)

Tuple String String -> Eff e aいくつかの関数を提供するだけですa。と を構成するfstfprint、まさにそれが得られます。

あなたの例は

spec = describe "Router" $ do

  let sampleRoutes = [( Tuple "/"  "views/index.yaml" ), 
                      ( Tuple "/foo" "views/foo.yaml" ), 
                      ( Tuple "/bar" "views/bar.yaml" )]

  it "should default to the first of the list" $ 
    traverse_ (fst >>> fprint) $ head sampleRoutes
于 2014-09-10T06:10:09.867 に答える