Bifunctors的参数化类型别名

时间:2016-06-14 12:52:22

标签: scala functor scalaz type-alias bifunctor

我有一个Console.WriteLine(My.Inner.Namespace.Contains<SomeTypeA>()); // False Console.WriteLine(My.Inner.Namespace.Contains<SomeTypeB>()); // True 我希望将其分成Seq[R],而我正在对此进行编码时,我想到了我可以使用自定义Tuple2[Seq[E], Seq[S]]作为seqs元组的事实并且当练习试图编码时:

Bifunctor

这样可以正常工作,但由于某种原因,我不得不声明一个参数化类型别名来进行所有这些编译,即import scalaz.Bifunctor type MyType[E, S] = (Seq[E], Seq[S]) case class MyVali[E, S](tp: (Seq[E], Seq[S]))(implicit bifunctor: Bifunctor[MyType]) { def bimap[C, D](f: (E) => C, g: (S) => D): (Seq[C], Seq[D]) = bifunctor.bimap(tp)(f, g) def leftMap[C](f: (E) => C): (Seq[C], Seq[S]) = bifunctor.leftMap(tp)(f) def rightMap[D](g: (S) => D): (Seq[E], Seq[D]) = bifunctor.rightMap(tp)(g) } val myValBifunctorInstance = new Bifunctor[MyType] { override def bimap[A, B, C, D](fab: (Seq[A], Seq[B]))(f: (A) => C, g: (B) => D): (Seq[C], Seq[D]) = (fab._1.map(f), fab._2.map(g)) } MyVali((Seq.empty[String], Seq.empty[Int]))(myValBifunctorInstance).bimap(a => a, b => b) 我很难理解为什么这样做有效,而不是:

type MyType[E, S] = (Seq[E], Seq[S])
  

[error] ...(Seq [E],Seq [S])没有类型参数,预期:两个

     

[error] def myValBimap [E,S] = new Bifunctor [Tuple2 [Seq [E],Seq [S]]] {

当定义了这样的类型别名时,编译器是否会创建一个2类型的挂起(如嵌套的lambda类型?)?

1 个答案:

答案 0 :(得分:1)

Sub PlayAllVideo()
    Dim IE As Object

    For Each cl In Range("A2:A22")
        Set IE = CreateObject("InternetExplorer.Application")
        'It is generally advisable to avoid using Select and ActiveCell
        'cl.Select
        IE.Visible = True
        IE.Navigate cl.Hyperlinks.item(1).Address
    Next cl
    Set IE = Nothing
End Sub

上面def myValBimap[E, S] = new Bifunctor[Tuple2[Seq[E], Seq[S]]] { ... } 中的Tuple2[...]不再有两个类型参数,因为BifunctorE已填写。

例如,S尝试创建myValBimap[Int, String],类型Bifunctor[(Seq[Int], Seq[String])]显然没有两个类型参数。

你可以写

  • (Seq[Int], Seq[String])
  • Bifunctor[({ type λ[α, β] = (Seq[α], Seq[β])})#λ]使用种类的投影机插件。

如果您需要其他类型,则需要带有类型参数的隐式函数,可能类似于:

Bifunctor[λ[(α, β) => (Seq[α], Seq[β])]]

一个更简单的例子是析取/ implicit def myBimap[F[_]: Functor]: Bifunctor[λ[(α, β) => (F[α], F[β])]] = ??? 的{​​{1}},它使用左侧的类型参数:

Functor

关于您的原始问题:您可以将\/映射到implicit def functorDisj[L]: Functor[L \/ ?] = ??? implicit def functorDisj[L]: Functor[({ type λ[α] = L \/ α })#λ] = ??? 并使用R

E \/ S