加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala列表映射vs mapConserve

发布时间:2020-12-16 08:49:25 所属栏目:安全 来源:网络整理
导读:我试图理解mapConserve,据说是“喜欢xs map f,但如果函数f将所有元素映射到自己,则返回xs不变”,从 List开始.但是,它给出了错误. def map [B] (f: (A) ? B): List[B]def mapConserve (f: (A) ? A): List[A]def mapConserve [B : A : AnyRef] (f: (A) ? B): L
我试图理解mapConserve,据说是“喜欢xs map f,但如果函数f将所有元素映射到自己,则返回xs不变”,从 List开始.但是,它给出了错误.

def map [B] (f: (A) ? B): List[B]
def mapConserve (f: (A) ? A): List[A]
def mapConserve [B >: A <: AnyRef] (f: (A) ? B): List[B]


scala> list map (x=>x)
res105: List[Int] = List(1,2,3,4,5,6,7,8,9,10)

scala> list mapConserve (x=>x)
<console>:12: error: inferred type arguments [Int] do not conform to method mapConserve's type parameter bounds [B >: Int <: AnyRef]
              list mapConserve (x=>x)
                   ^

mapConserve代码应该满足(A)=>一个功能.如果不是,它仍应满足(A)=> B函数,因为类型A可以是子类型和自身的超类型.请赐教mapConserve的目的和错误.

解决方法

实际上,mapConserve定义为

def mapConserve[A <: AnyRef](xs: List[A])(f: A => A): List[A]
def mapConserve[B >: A <: AnyRef](f: A => B): List[B]

所以A应该是AnyRef的子类型. Int is a subtype of AnyVal,这会带来错误.

scala> val l = List("foo","bar","baz")
l: List[java.lang.String] = List(foo,bar,baz)

scala> l.mapConserve(_.toUpperCase)
res4: List[java.lang.String] = List(FOO,BAR,BAZ)

scala> l.mapConserve(identity)
res5: List[java.lang.String] = List(foo,baz)

更新:

map和mapConserve之间的唯一区别,如scaladoc中所述:

Builds a new list by applying a function to all elements of this list.
Like xs map f,but returns xs unchanged if function f maps all elements to themselves (as determined by eq).

scala> val xs = List.fill(1000000)("foo")
xs: List[java.lang.String] = List(foo,foo,...)

scala> xs.map(identity) eq xs
res48: Boolean = false

scala> xs.mapConserve(identity) eq xs
res49: Boolean = true

在我的简单基准测试中,xs mapConserve标识的速度提高了约五倍.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读