- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我想添加像 map
这样的功能到 Scala List
,类似于list mapmap f
,它应用函数 f
到 list
的每个元素两次。 (一个更严重的例子可能是实现一个并行或分布式 map ,但我不想被那个方向的细节分散注意力。)
我的第一种方法是
object MapMap {
implicit def createFancyList[A](list: List[A]) = new Object {
def mapmap(f: A => A): List[A] = { list map { a: A => f(f(a)) } }
}
}
scala> import MapMap._
import MapMap._
scala> List(1,2,3) mapmap { _ + 1 }
res1: List[Int] = List(3, 4, 5)
List
s,我们没有理由不希望它适用于任何东西
Traverseable
, 带有
map
功能,例如
Set
s 或
Stream
s。所以第二次尝试看起来像
object MapMap2 {
implicit def createFancyTraversable[A](t: Traversable[A]) = new Object {
def mapmap(f: A => A): Traversable[A] = { t map { a: A => f(f(a)) } }
}
}
List[A]
:
scala> import MapMap2._
import MapMap2._
scala> val r: List[Int] = List(1,2,3) mapmap { _ + 1 }
<console>:9: error: type mismatch;
found : Traversable[Int]
required: List[Int]
CanBuildFrom
特征,甚至可能是
breakout
!)
最佳答案
您不能对所有 Traversable 执行此操作,因为它们不保证 map 返回比 Traversable 更具体的任何内容。请参阅下面的更新 2。
import collection.generic.CanBuildFrom
import collection.TraversableLike
class TraversableW[CC[X] <: TraversableLike[X, CC[X]], A](value: CC[A]) {
def mapmap(f: A => A)(implicit cbf: CanBuildFrom[CC[A], A, CC[A]]): CC[A]
= value.map(f andThen f)
def mapToString(implicit cbf: CanBuildFrom[CC[A], String, CC[String]]): CC[String]
= value.map(_.toString)
}
object TraversableW {
implicit def TraversableWTo[CC[X] <: TraversableLike[X, CC[X]], A](t: CC[A]): TraversableW[CC, A]
= new TraversableW[CC, A](t)
}
locally {
import TraversableW._
List(1).mapmap(1+)
List(1).mapToString
// The static type of Seq is preserved, *and* the dynamic type of List is also
// preserved.
assert((List(1): Seq[Int]).mapmap(1+) == List(3))
}
mapToString
, 来说明为什么
TraversableW
接受两个类型参数,而不是 Alexey 的解决方案中的一个参数。参数
CC
是更高种类的类型,它代表原始集合的容器类型。第二个参数
A
, 表示原始集合的元素类型。方法
mapToString
因此能够返回具有不同元素类型的原始容器类型:
CC[String
.
CC[X] <: Traversable[X]
,
TraversableLike
不是严格需要的。内联评论:
import collection.generic.CanBuildFrom
import collection.TraversableLike
class TraversableW[CC[X] <: Traversable[X], A](value: CC[A]) {
/**
* A CanBuildFromInstance based purely the target element type `Elem`
* and the target container type `CC`. This can be converted to a
* `CanBuildFrom[Source, Elem, CC[Elem]` for any type `Source` by
* `collection.breakOut`.
*/
type CanBuildTo[Elem, CC[X]] = CanBuildFrom[Nothing, Elem, CC[Elem]]
/**
* `value` is _only_ known to be a `Traversable[A]`. This in turn
* turn extends `TraversableLike[A, Traversable[A]]`. The signature
* of `TraversableLike#map` requires an implicit `CanBuildFrom[Traversable[A], B, That]`,
* specifically in the call below `CanBuildFrom[Traversable[A], A CC[A]`.
*
* Essentially, the specific type of the source collection is not known in the signature
* of `map`.
*
* This cannot be directly found instead we look up a `CanBuildTo[A, CC[A]]` and
* convert it with `collection.breakOut`
*
* In the first example that referenced `TraversableLike[A, CC[A]]`, `map` required a
* `CanBuildFrom[CC[A], A, CC[A]]` which could be found.
*/
def mapmap(f: A => A)(implicit cbf: CanBuildTo[A, CC]): CC[A]
= value.map[A, CC[A]](f andThen f)(collection.breakOut)
def mapToString(implicit cbf: CanBuildTo[String, CC]): CC[String]
= value.map[String, CC[String]](_.toString)(collection.breakOut)
}
object TraversableW {
implicit def TraversableWTo[CC[X] <: Traversable[X], A](t: CC[A]): TraversableW[CC, A]
= new TraversableW[CC, A](t)
}
locally {
import TraversableW._
assert((List(1)).mapmap(1+) == List(3))
// The static type of `Seq` has been preserved, but the dynamic type of `List` was lost.
// This is a penalty for using `collection.breakOut`.
assert((List(1): Seq[Int]).mapmap(1+) == Seq(3))
}
collection.breakOut
,因为我们无法从仅仅
Traversable[A]
中恢复特定的集合子类型.
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
val b = bf(repr)
b.sizeHint(this)
for (x <- this) b += f(x)
b.result
}
Builder
b
使用原始集合进行初始化,这是通过
map
保留动态类型的机制.但是,我们的
CanBuildFrom
通过类型参数
Nothing
拒绝所有关于 From 的知识.
Nothing
的所有功能是忽略它,这正是
breakOut
做:
def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
new CanBuildFrom[From, T, To] {
def apply(from: From) = b.apply();
def apply() = b.apply()
}
b.apply(from)
, 不超过你可以调用
def foo(a: Nothing) = 0
.
关于scala - 我可以使用具有很好变体类型的 TraversableLike.map 的类似物 "pimp my library"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3225675/
并发是编程中真正困难的部分。最近一段时间goroutine很火。我的问题是 - 其他语言中存在什么样的 goroutine 类似物? 最佳答案 goroutine 的灵感来自 CSP (Communi
对于 Scala 来说,是否存在任何与 LINQ (.NET) 类似的合理的东西? 最佳答案 这取决于“LINQ”的确切含义。 LINQ 涉及很多事情。 最明显的答案是:只需使用 Scala 的 .N
我正在尝试使用 maven 的 com.springsource.bundlor 插件为 OSGI 应用程序创建 list 文件,并且有很多问题,所以有人可以告诉我有类似于 spring bundlo
有人可以指出与 java JSplitPane 类似的好的 delphi 组件吗? 我知道 Delphi 中的标准 TSplitter,但我对使用它感到厌烦。 最佳答案 JVCL组件带有 4 个分离器
我希望能够从目录树中的任何位置运行grails。 最佳答案 如果没有这样的选项,则可以创建bugreport来引入这样的参数。 作为解决方法,您可以创建将grails命令包装在项目中的build.xm
我从 https://referencesource.microsoft.com/ 中获取了 Nullable 类的源代码并将其放入文件并重命名为 NullableZZ(以及 NonVersionab
基础:我有非常大的并行 Fortran90/MPI 程序,代表复杂的物理模型。我想为其添加新功能:例如,我需要组织消息队列,在某处引入合并排序并使用哈希表。 问题:我自己知道如何编写哈希表、创建队列和
刚从CMU HoTT的lectures中了解到,虽然Check Type在Coq中返回的是Type : Type,但是左边的Type和 right 由不同的数字隐式索引,因为如果它们相同,这将导致 B
我有我的 MVVM,模型由许多相关的类组成,但为了这个问题,我们只关注四个。 GrandParentModel、ParentModel、ChildModel 和联系人。 这些都继承自 ModelBas
我有一个 pandas DataFrame,其中包含“类别”和“总计”列。可以有 4 个不同的类别:A、B、C、D。我以字典的形式得到每个类别的切点值。我需要排除总计超过相应切点的所有条目。这工作正常
Micronaut 中是否有类似@RequestScope 的注解? 如果没有等效注解,如何在Micronaut 中实现相同的效果? 最佳答案 此功能从 1.2.0.RC1 版本开始添加 您可以找到文
在 Java 社区中有没有什么东西可以称为 .NET 的“应用程序服务器”?类似于 Tomcat、Resin 和 Jetty。我对 JSP 等效物不感兴趣,我正在寻找用于 XML/HTTP 事务处理的
Android 上是否有 javax.smartcardio 类似物?但不使用 Open Mobile API。 谢谢! 最佳答案 我在名为 SCUBA 的项目中使用 javax.smartcardi
前几天我在用 C# 编写一些代码来获取一个对象列表,每个对象都包含一个数组字段,并将其转换为一个映射(即字典),其中对象成为值,对象数组的每个元素领域成为关键。这可能没有多大意义,所以让我们展示一些代
Pascal 中有一个方便的东西,那就是类型化的二进制文件。 来自 Free Pascal wiki 的描述 The type file of , where is any simple type
这是在 unix 中使用 pipe fork exec trio 的简单演示。 #include #include #include #include int main() { int
有人可以在 C++ 中编写以下代码: #include template bool TestArgumentTypesOnEquality(Type1 argument1, Type2 argum
在 MySQL 的旧版本中可以选择分析现有表格: SELECT * FROM `table_name` PROCEDURE ANALYSE() 这似乎很有用,开发人员可以分析现有表,查看建议(例如使
开源,免费 XF.Network类似物(creating High Performance TCP/IP Server using C#.NET 的产品) 最佳答案 尝试 spserver . 关于c
我是一名优秀的程序员,十分优秀!