我编写了一个扩展Iterator [A]的自定义特性,我希望能够使用我在另一个方法返回的迭代器[A]上编写的方法.这可能吗?
trait Increment[+A] extends Iterator[A]{
def foo() = "something"
}
class Bar( source:BufferedSource){
//this ain't working
def getContents():Increment[+A] = source getLines
}
Run Code Online (Sandbox Code Playgroud)
我仍然试图绕过整个隐含的东西而不是在Bar对象定义中编写方法.我将如何以上述方式包装这样的物品?
如何foreachWithIndex在Scala集合上添加方法?
这是我到目前为止所能提出的:
implicit def iforeach[A, CC <: TraversableLike[A, CC]](coll: CC) = new {
def foreachWithIndex[B](f: (A, Int) => B): Unit = {
var i = 0
for (c <- coll) {
f(c, i)
i += 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
Vector(9, 11, 34).foreachWithIndex { (el, i) =>
println(el, i)
}
Run Code Online (Sandbox Code Playgroud)
引发以下错误:
error: value foreachWithIndex is not a member of scala.collection.immutable.Vector[Int]
Vector(9, 11, 34).foreachWithIndex { (el, i) =>
Run Code Online (Sandbox Code Playgroud)
但是,当我明确应用转换方法时,代码可以正常工作:
iforeach[Int, Vector[Int]](Vector(9, 11, 34)).foreachWithIndex { (el, i) =>
println(el, i) …Run Code Online (Sandbox Code Playgroud) scala implicit-conversion scala-collections enrich-my-library
我有以下内容:
case class Bar()
trait Foo {
def bars : Seq[Bar]
}
case class MyFoo(bars : Seq[Bar]) extends Foo
trait Foos extends Seq[Foo] {
def bars : Seq[Bar] = this.map(_.bars).flatten
}
Run Code Online (Sandbox Code Playgroud)
我想将对象转换Seq[MyFoo]为MyFoos,最好隐式.怎么能实现这个目标?
例如.
val foos : Foos = Seq(new MyFoo(Seq(new Bar)))
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
class Register
attr_accessor :val
def initialize
@val = 0
end
end
Run Code Online (Sandbox Code Playgroud)
我希望能够,给定ax=Register.new,输入3 + ax或ax + 3得到相当于的结果3 + ax.val.我试过搜索,但无法找到如何在Ruby中完成.
我用nodeJs创建了一个程序,生成代码,如
eval("XXXXXX") == "XXXXXX"
Run Code Online (Sandbox Code Playgroud)
它运作得很好,但是在某个时刻他给了我这个:
eval("475957E-8905") == "475957E-8905"
Run Code Online (Sandbox Code Playgroud)
我用Firebug测试了它,结果是true
.但我真的不明白为什么.
当然,eval("475957E-8905")回归0
但为什么0 == "475957E-8905"呢?
容器需要提供一种iterator可隐式转换为a 的类型const_iterator.鉴于此,我尝试使用auto初始化对象vector::begin(),并std::distance在RHS所在的位置使用该结果对象const_iterator.这不起作用.这是一个完整的例子:
#include <cstdlib>
#include <vector>
#include <iterator>
#include <iostream>
typedef std::vector <char> Packet;
typedef std::vector <Packet> Packets;
template <typename Iter>
Iter next_upto (Iter begin, Iter end, size_t n)
{
Iter ret = begin;
for (; n > 0 && ret != end; ++ret, --n)
;
return ret;
}
Packets::const_iterator Process (Packets::const_iterator begin, Packets::const_iterator end)
{
Packets::const_iterator ret = begin;
while (ret != end)
++ret; // do something …Run Code Online (Sandbox Code Playgroud) 您可以隐式地将int转换为double: double x = 5;
您可以显式地将int转换为double: double x = (double) 5;
您可以显式地将double转换为int: int x = (int) 5.0;
为什么不能隐式地将double转换为int?: int x = 5.0;
我理解C语言在整数和浮点类型之间的隐式转换,但我对签名/无符号隐式类型转换有疑问.
例如,如果添加a unsigned char 和a signed int,结果类型是什么?它会是a unsigned int,a signed int还是别的什么?
我没有看到C99 ANSI标准中有任何具体内容,所以任何帮助都表示赞赏.
我怎样才能找到所有的实例Monoid.例如,如何知道scalaz中是否有Monoid实例Map?如果是的话,它在源代码中的位置.我试过以下没有成功
@ implicitly[Monoid[Map[_, _]]]
Main.scala:1146: could not find implicit value for parameter e: scalaz.Monoid[Map[_, _]]
implicitly[Monoid[Map[_, _]]]
^
Compilation Failed
Run Code Online (Sandbox Code Playgroud)当我从REPL执行代码时,我怎么能看到会发生什么(隐式转换......)
Map("a", 1) |+| Map("a", 1)
Run Code Online (Sandbox Code Playgroud)我有以下代码,它应该采用一个函数A => Boolean(输入类型的通用)并Y[A]通过链式隐式转换将其转换为通用特征:
val f: Int => Boolean = ???
trait X[A] {
def m1: Unit
}
implicit def toX[A](f: A => Boolean): X[A] = ???
f.m1 // Works
trait Y[A] {
def m2: Unit
}
implicit def toY[T, A](x: T)(implicit toX: T => X[A]): Y[A] = ???
f.m2 // Won't compile
Run Code Online (Sandbox Code Playgroud)
不幸的是,最后一行不会编译.
执行以下任何一项更改就足以使代码编译:
X非泛型Y非泛型Int => A)上使用泛型函数替换源类型(在输入类型上是通用函数)Option[A],Seq[A]或Array[A]基于此,我的结论是隐式转换链不起作用,因为源类型(输入类型的泛型函数)是通用的和逆变的 …