Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 1 (11.04.2018): task 1 #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Week 1 (11.04.2018): task 1 #5

wants to merge 2 commits into from

Conversation

odbc
Copy link

@odbc odbc commented Jun 14, 2018

No description provided.


object Eq {

implicit class EqOps[T](val x: T) extends AnyVal{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

как привести это в скоуп?

import Eq._ 
//или
import Eq.EqOps

оба варианта мне кажутся не очень, поэтому обычно такие вещи складываются в какой-то специальный объект вроде

import Eq.syntax._
//
import syntax.eq._

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято)

def compare(y: T)(implicit ord: Ord[T]): Compare = ord.compare(x, y)
}

implicit val ratioOrd: Ord[Ratio] = (x: Ratio, y: Ratio) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В данном случае, если бы это лежало в компаньоне типа, не пришлось бы реализовывать и Eq и Ord, хватило бы этого инстанса

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это можно пояснить немного? В компаньон типа Ratio этот инстанс лучше положить?

case (a, b) if a.nonEmpty && b.isEmpty => GT
case (a :: as, b :: bs) =>
val cmp = a compare b
if (cmp == EQ) as compare bs else cmp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нехвостовая рекурсия = StackOverflow

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А разве не хвостовая, рекурсивный вызов as compare bs же последнее действие в цепочке?Или из-за того, что операция compare ещё через имплисит обрабатывается?

object OrdInstances {
import Ord.OrdOps

implicit def sizeFirstListOrd[T : Ord]: Ord[List[T]] = (x: List[T], y: List[T]) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не очень понял смысл этого модуля, но этот метод по крайней мере стекобезопасен, хотя и нарушает общепринятый лексикографический подход, его бы объединить с решением сверху

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это просто искусственно придуманные варианты)) Чтобы поиграться с несколькими инстансами

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants