假设我们有以下类和一些值(在Scala中):
class A[T](val x: T)
class B[T](x: T, val y: T) extends A[T](x)
val x1 = new A("test")
val x2 = new B(1,2)
val x3 = new B("foo","bar")
val x4 = new A(1)
Run Code Online (Sandbox Code Playgroud)
此外,我们定义以下多态函数值(使用无形):
object f extends (A ~> Option) {
def apply[T](s: A[T]) = Some(s.x)
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以致电:
f(x1); f(x2); f(x3); f(x4)
Run Code Online (Sandbox Code Playgroud)
哪一切都成功了(应该恕我直言).然而:
val list = x1 :: x2 :: x3 :: x4 :: HNil
list.map(f)
// could not find implicit value for parameter mapper:
// shapeless.Mapper[f.type,shapeless.::[A[String],shapeless.::[
// B[Int],shapeless.::[B[String],shapeless.::[A[Int],shapeless.HNil]]]]]
Run Code Online (Sandbox Code Playgroud)
我期待的地方: …
我试图测试两个"容器"是否使用相同的高级类型.看下面的代码:
import scala.reflect.runtime.universe._
class Funct[A[_],B]
class Foo[A : TypeTag](x: A) {
def test[B[_]](implicit wt: WeakTypeTag[B[_]]) =
println(typeOf[A] <:< weakTypeOf[Funct[B,_]])
def print[B[_]](implicit wt: WeakTypeTag[B[_]]) = {
println(typeOf[A])
println(weakTypeOf[B[_]])
}
}
val x = new Foo(new Funct[Option,Int])
x.test[Option]
x.print[Option]
Run Code Online (Sandbox Code Playgroud)
输出是:
false
Test.Funct[Option,Int]
scala.Option[_]
Run Code Online (Sandbox Code Playgroud)
但是,我希望一致性测试能够成功.我究竟做错了什么?我如何测试更高级的类型?
澄清
在我的例子中,我正在测试的值(x: A在示例中)来自List[c.Expr[Any]]宏.所以任何依赖于静态分辨率的解决方案(就像我给出的那样)都无法解决我的问题.
我希望Scala工作表(和Scala解释器)的工作目录是Eclipse项目路径而不是Eclipse安装目录.我怎样(非编程)实现这一点?
我知道我可以使用System.setProperty("user.dir", "..."),但恕我直言,不属于代码.此外,它似乎不起作用:
object ScratchWS {
System.setProperty("user.dir", "C:\\") //> res0: String = C:\adt-bundle-windows-x86_64-20130219\eclipse
new File("putty.exe").exists() //> res1: Boolean = false
new File("C:\\putty.exe").exists() //> res2: Boolean = true
}
Run Code Online (Sandbox Code Playgroud) 我想在Scala工作表中抑制辅助变量的输出:
val sqs = scen.rssis.toSeq.filter { case (ap,s) =>
s.exists(e => e.epoch > 1) }.sortBy { -_._2.length }.take(10)
//> sqs :
// *snip* a lot of stuff I'd rather not have
//| Output exceeds cutoff limit.
sqs foreach { case (api,s) =>
println(f"${scen.aps(api).ssid}%-10s ${s.length}% 5d")
} //> 2WIRE230 74
//| 2WIRE736 74
//| Jamie56 73
//| VVHOA 69
//| 2WIRE059 68
//| Rainsnet 68
//| 2WIRE519 67
//| 2WIRE604 65
//| neo_vex_24 63
//| ALEMANIA7 63
Run Code Online (Sandbox Code Playgroud)
有没有办法在Scala工作表中抑制赋值的输出?
如何在 github 中查找使用某个 gem 的项目。(例如我想找到使用 activeresource 的项目)。
我是Scala的新手,我想知道为什么main方法没有在这个脚本中运行?
class Word {
}
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我删除Word课程时,它工作正常.为什么是这样?我该如何解决?
在清理(即大部分压缩)某些更大功能的历史时,我经常遇到以下情况:
Commit A:
- change A.1
- change A.2
- change A.3
// EDIT: maybe some more commits
Commit B:
- change B.1
- change B.2
- change B.3
Run Code Online (Sandbox Code Playgroud)
假设现在我想转移change B.1使用交互式rebase提交A. 到目前为止我发现的唯一方法是:
rebase -ireset HEAD^然后add -p)rebase -i这需要两个rebase并且非常麻烦.另外,我必须在拆分时重写提交B的提交消息.
有没有更好/更有效的方法来实现这一目标?
我正在阅读Scala中的Functional Programming,在第04章中,作者自己实现了Option.现在,在定义函数时,getOrElse他们使用上限将类型限制A为超类型(如果正确理解)
所以,定义如下:
sealed trait Option[+A] {
def getOrElse[B >: A](default: => B): B = this match {
case None => default
case Some(a) => a
}
}
Run Code Online (Sandbox Code Playgroud)
所以,当我们有类似的东西时
val a = Some(4)
println(a.getOrElse(None)) => println prints a integer value
val b = None
println(b.getOrElse(Some(3)) => println prints a Option[Integer] value
Run Code Online (Sandbox Code Playgroud)
a有类型Option[Int],所以A是类型Int.B将是类型Nothing.Nothing是每种其他类型的子类型.这意味着它Option[Nothing]是Option[Int](由于协方差)的子类型,对吧?
但是B >: A我们说过B必须要超类型?!那我们怎么能 …
假设我有一个像这样的网址:
https://example.com/myproject/index-dev.html?_ijt=hsdlgh8h5g8hh489sajoej&a=102&b=a%20m&c=45&d=all&e=all
Run Code Online (Sandbox Code Playgroud)
或者它可能是本地主机上的网页,例如:
localhost:63342/my project/index-dev.html?_ijt=hsdlgh8h5g8hh489sajoej&a=102&b=a%20m&c=45&d=all&e=all
Run Code Online (Sandbox Code Playgroud)
我必须从二维数组中的这些 url 中提取查询字段(出现在“?”之后),如下所示:
_ijt | hsdlgh8h5g8hh489sajoej
a | 102
b | a m
c | 45
d | all
e | all
Run Code Online (Sandbox Code Playgroud)
请注意,在“b”字段中,我已将“%20”替换为空格。_ijt、a、b、c、d、e 等字段的数量可能会有所不同,它们的名称例如“a”可以是“城市”。到目前为止,我已经使用正则表达式提取了 '?' 之后的部分。然后使用 split("&") 方法将字符串拆分为多个字符串。代码 -
val url=localhost:63342/my project/index-dev.html?_ijt=hsdlgh8h5g8hh489sajoej&a=102&b=a%20m&c=45&d=all&e=all
val pattern="""(http|htpps)([A-Za-z0-9\:\/\%\-\.]*)\?""".r
val temp_url=pattern.replaceFirstIn(url,"")
val fields=temp_url.split("&")
println(fields.foreach(println))
Run Code Online (Sandbox Code Playgroud)
输出是:
_ijt=hsdlgh8h5g8hh489sajoej
a=102
b=a%20m
c=45
d=all
e=all
Run Code Online (Sandbox Code Playgroud)
但这似乎不是执行此操作的正确方法。有什么帮助吗?
给定containers.Map包含给定事件(键)的计数(值)的对象的单元数组.我想获得一个包含每个键的计数总和(作为值)的映射(或者允许我计算计数的东西).
例如:
maps = { containers.Map({'a','b'},{1,2}),
containers.Map({'b','c'},{4,1}) };
mergeMaps(maps)
ans = Map(a -> 1, b -> 6, c -> 1)
Run Code Online (Sandbox Code Playgroud)
到目前为止我唯一能想到的是:
maps = { ... };
res = containers.Map();
for cMapC = maps
cMap = cMapC{1};
for cKeyC = keys(cMap{1})
cKey = cKeyC{1};
if isKey(res, cKey)
res(cKey) = res(cKey) + cMap(cKey);
else
res(cKey) = cMap(cKey);
end
end
end
Run Code Online (Sandbox Code Playgroud)
哪个应该工作(未经测试),但其"效率"和可读性至少可以说是有问题的.什么是更好的解决方案?
请注意,目前没有归因于事件的索引,因此使用稀疏矩阵而不是映射是很麻烦的.
我正在Laravel 4中创建我的网站,我在表格中有created_at&updated_at字段.我想创建一个新闻系统,告诉我自帖子发布以来已经过了多少时间.
| name | text | created_at | updated_at |
| __________ | __________ | ________________________ | ___________________ |
| news name | news_text | 2013-06-12 11:53:25 | 2013-06-12 11:53:25 |
Run Code Online (Sandbox Code Playgroud)
我想表现出类似的东西:
- 5分钟前创建
- 5个月前创建
如果帖子超过1个月
- 创建于2012年11月5日