我使用 Scala bCrypt包装器来加密用户密码,该包装器提供了一个隐式类。
package object bcrypt {
implicit class Password(val pswrd: String) extends AnyVal {
def bcrypt: String = B.hashpw(pswrd, BCrypt.gensalt())
def bcrypt(rounds: Int): String = B.hashpw(pswrd, BCrypt.gensalt(rounds))
def bcrypt(salt: String): String = B.hashpw(pswrd, salt)
def isBcrypted(hash: String): Boolean = B.checkpw(pswrd, hash)
}
def generateSalt: String = B.gensalt()
}
Run Code Online (Sandbox Code Playgroud)
但我面临一个奇怪的问题,每当我在类中使用这种隐式转换时,它都可以正常工作,但转换不适用于对象或案例类。
scala> import com.github.t3hnar.bcrypt._
import com.github.t3hnar.bcrypt._
scala> class Password(secret: String) {
| def validate(userSecret: String): Boolean = userSecret.isBcrypted(secret)
|
| override def toString …Run Code Online (Sandbox Code Playgroud) 我有以下用于解析的特性,它们为对象的开头和结尾提供文件位置:
case class FilePosn(lineNum :Int, tabs: Int, spaces: Int, fileName: String)
{/*code omitted*/}
trait PosnEnds
{
def startPosn: FilePosn
def endPosn: FilePosn
def multiLine: Boolean = startPosn.lineNum != endPosn.lineNum
def OneLine: Boolean = startPosn.lineNum == endPosn.lineNum
def indent: Int = startPosn.tabs
def startLine: Int = startPosn.lineNum
def endLine: Int = endPosn.lineNum
}
object FilePosnVoid extends FilePosn(0, 0, 0, "There is no File position")
{ override def posnString(indentSize: Int): String = "No File Posn: " }
Run Code Online (Sandbox Code Playgroud)
在伴侣对象中我创建了一个隐含的,因此PosnEnds序列本身就是隐含的PosnEnds:
object PosnEnds
{
implicit …Run Code Online (Sandbox Code Playgroud) 这里有一些看起来合理的代码:
val myMap: Map[Int, Int] = ((x: Int) => Map[Int, Int](1 -> x + 1, 2 -> x + 2))(4)
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我得到两个这样的错误:
Error:(20, 68) type mismatch;
found : Int(1)
required: String
val myMap: Map[Int, Int] = ((x: Int) => Map[Int, Int](1 -> x + 1, 2 -> x + 2))(4)
^
Run Code Online (Sandbox Code Playgroud)
据我所知,我的编译器正在尝试使用字符串添加实现+.但为什么这样做呢?如何让编译器在这里使用整数加法?
(Int改为Integer没有帮助.)
嗨,我有以下数据,并希望将其映射到第二个参数中的第一项.因此对于:
1 -> List((1,11))
1 -> List((1,1), (1,111))
Run Code Online (Sandbox Code Playgroud)
我想要:
(1,11)
(1,1)
Run Code Online (Sandbox Code Playgroud)
当此数据在RDD中时,我可以执行以下操作:
scala> val m = sc.parallelize(Seq(11 -> List((1,11)), 1 -> List((1,1),(1,111))))
m: org.apache.spark.rdd.RDD[(Int, List[(Int, Int)])] = ParallelCollectionRDD[198] at parallelize at <console>:47
scala> m.map(_._2.head).collect.foreach(println)
(1,11)
(1,1)
Run Code Online (Sandbox Code Playgroud)
但是,当它在Map对象(groupBy的结果)中时,我得到以下内容:
scala> val m = Map(11 -> List((1,11)), 1 -> List((1,1)))
m: scala.collection.immutable.Map[Int,List[(Int, Int)]] = Map(11 -> List((1,11)), 1 -> List((1,1), (1,111)))
scala> m.map(_._2.head)
res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1)
Run Code Online (Sandbox Code Playgroud)
当我映射到整个列表时,我得到了我期望的结果,但是当我打电话给它时
scala> m.map(_._2)
res2: scala.collection.immutable.Iterable[List[(Int, Int)]] = List(List((1,11)), List((1,1), (1,111)))
Run Code Online (Sandbox Code Playgroud)
如果我执行以下任一操作,我也可以得到我想要的结果:
scala> m.map(_._2).map(_.head)
res4: …Run Code Online (Sandbox Code Playgroud)