在Scala中使用地图上使用地图时输入不匹配

发布于 2025-02-01 02:11:08 字数 917 浏览 1 评论 0 原文

考虑此代码:

  /** Takes a list and turns it into an infinite looping stream. */
  def loop(l: List[Char]): LazyList[Char] = {
      l.to(LazyList)  #:::loop(l)
  }

  /** Encodes a sequence of characters with a looped key. */
  def codec(message: Seq[Char], key: Seq[Char], cipher: (Char, Char) => Char): Seq[Char] = {
    val loopedKey = loop(key.toList)
    val mergedMessage = message.toList zip loopedKey
    val xorResult = mergedMessage.map(cipher)
    xorResult.toSeq
  }

循环函数正确,但是汇编后的函数 cocdec 会产生以下错误:

[error]  type mismatch;
[error]  found   : (Char, Char) => Char
[error]  required: ((Char, Char)) => ?
[error]     val xorResult = mergedMessage.map(cipher)

我不明白为什么必需 part ext eap taid:> ((char,char))=> ?

Consider this code :

  /** Takes a list and turns it into an infinite looping stream. */
  def loop(l: List[Char]): LazyList[Char] = {
      l.to(LazyList)  #:::loop(l)
  }

  /** Encodes a sequence of characters with a looped key. */
  def codec(message: Seq[Char], key: Seq[Char], cipher: (Char, Char) => Char): Seq[Char] = {
    val loopedKey = loop(key.toList)
    val mergedMessage = message.toList zip loopedKey
    val xorResult = mergedMessage.map(cipher)
    xorResult.toSeq
  }

The loop function is correct, but the function named codec yield the following error when compiled :

[error]  type mismatch;
[error]  found   : (Char, Char) => Char
[error]  required: ((Char, Char)) => ?
[error]     val xorResult = mergedMessage.map(cipher)

I don't understand why the required part says : ((Char, Char)) => ?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2025-02-08 02:11:08

(char,char)=> char 是一个函数,接受2 chars 和返回 char zip 将导致元组 (char,char)。一个选项是更改 cipher 键入接受元组 - (((char,char))=> char

def codec(message: Seq[Char], key: Seq[Char], cipher: ((Char, Char)) => Char): Seq[Char] = ...

没有更改签名 - 直接访问元组元素:

val xorResult = mergedMessage.map(t => cipher(t._1, t._2))

或者只使用 tupled

val xorResult = mergedMessage.map(cipher.tupled)

(Char, Char) => Char is a function accepting 2 Chars and returning Char while zip will result in a collection of tuples (Char, Char). One option is to change cipher type to accept a tuple - ((Char, Char)) => Char:

def codec(message: Seq[Char], key: Seq[Char], cipher: ((Char, Char)) => Char): Seq[Char] = ...

Without changing the signature - access tuple elements directly:

val xorResult = mergedMessage.map(t => cipher(t._1, t._2))

Or just use tupled:

val xorResult = mergedMessage.map(cipher.tupled)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文