Scala Tour 隐式转换示例

编程入门 行业动态 更新时间:2024-10-14 18:20:31
本文介绍了Scala Tour 隐式转换示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难理解这段代码到底做了什么:

I am having a hard time understanding what this piece of code does exactly:

import scala.language.implicitConversions implicit def list2ordered[A](x: List[A]) (implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] = new Ordered[List[A]] { //replace with a more useful implementation def compare(that: List[A]): Int = 1 }

它来自 Scala Tour,位于隐式转换"部分.我知道 list2ordered 需要一个 List[A] 来自 List(1, 2, 3) <= List(4,5) compare 函数中的 和 that 是右侧.

It comes from the Scala Tour and it is in the section "Implicit Conversions". I understand that list2ordered takes a List[A] that comes from the left hand side of List(1, 2, 3) <= List(4, 5) and that in compare function is the right hand side.

然而,为什么 A =>Ordered[A] 而不是 List[A] =>有序[列表[A]] ?我对这段代码的实际作用有点困惑.

However, why is A => Ordered[A] that and not List[A] => Ordered[List[A]] ? I am a bit confused as to what this piece of code actually does.

推荐答案

你的困惑是可以理解的.示例代码并不是非常有启发性,主要是因为代码,如所呈现的,不需要 A-to-Ordered[A] 转换.我们可以将其注释掉,一切仍然有效"(如其所是).

Your confusion is understandable. The example code isn't terribly illuminating largely because the code, as presented, doesn't need the A-to-Ordered[A] conversion. We can comment it out and everything still "works" (such as it is).

import scala.language.implicitConversions implicit def list2ordered[A](xs: List[A] //)(implicit elem2ordered: A => Ordered[A] ): Ordered[List[A]] = new Ordered[List[A]] { def compare(ys: List[A]): Int = 1 //this is always greater than that }

我们甚至可以实现一个有意义的(如果头脑比较简单的话)List 排序,并且仍然不需要 A-to-Ordered[A] 转化.

We can even implement a meaningful (if rather simple minded) List ordering and still not need the A-to-Ordered[A] conversion.

import scala.language.implicitConversions implicit def list2ordered[A](xs: List[A] //)(implicit elem2ordered: A => Ordered[A] ): Ordered[List[A]] = new Ordered[List[A]] { def compare(ys: List[A]): Int = xs.length - ys.length //shorter List before longer List }

但是如果 List 顺序取决于元素顺序,那么我们需要这种转换.

But if List order depends on element order, then we need that conversion.

import scala.language.implicitConversions implicit def list2ordered[A](xs: List[A] )(implicit elem2ordered: A => Ordered[A] ): Ordered[List[A]] = new Ordered[List[A]] { //3rd element determines order def compare(ys: List[A]): Int = (xs.lift(2),ys.lift(2)) match { case (None,None) => 0 case (None, _) => -1 case (_, None) => 1 case (Some(x), Some(y)) => x compare y //implicit conversion needed } }

为了说明重点,让我们通过修改所需的转换来简化这种按 3rd-element 排序的排列.

Just to drive home the point, let's simplify this order-by-3rd-element arrangement by modifying the required conversion.

import scala.language.implicitConversions implicit def list2ordered[A](xs: List[A] )(implicit elem2ordered: Option[A] => Ordered[Option[A]] ): Ordered[List[A]] = new Ordered[List[A]] { def compare(ys: List[A]): Int = xs.lift(2) compare ys.lift(2) //3rd element determines order }

更多推荐

Scala Tour 隐式转换示例

本文发布于:2023-07-14 10:12:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   隐式   Scala   Tour

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!