Sometimes it is really difficult in Scala to pass a function as an argument.
Can someone help me find a more natural way to pass the 5th and 6th arguments
to this function. The Magma.isField
method is has type parameter T
, and its 5th and 6th
arguments have type T=>Option[T]
. At the call site, I’m demanding that T
be (Int, Int)
.
I.e., I want to create a closure which closes over the variable m
.
I tried simply a => Some(m.subtract(m.zero, a))
, in which case I get an error that a
needs a type parameter. Then I change it to
a:(Int,Int) => Some(m.subtract(m.zero, a)), I see that
Some` needs a type parameter, and that the parser can no longer parse the code because of problems with parens and curly braces.
'}' expected but ',' found.
a:(Int,Int) => Some(m.subtract(m.zero, a)),
Here is the call:
object testGaussianInt {
def main(argv: Array[String]): Unit = {
for {p <- 2 to 4
m = new GaussianIntModP(p)
f = Magma.isField(m.gen, m.member,
m.add, m.mult,
locally {
def maybe_add_invert(a: (Int, Int)): Option[(Int, Int)] =
Some(m.subtract(m.zero, a))
maybe_add_invert _
},
locally {
def maybe_mult_invert(a: (Int, Int)): Option[(Int, Int)] =
m.gen().find(b => m.mult(a, b) == m.one)
maybe_mult_invert _
},
m.one, m.zero
)
} println(s"$p $f")
}
}