-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: master
Are you sure you want to change the base?
Conversation
src/main/scala/fpspeedrun/Eq.scala
Outdated
|
||
object Eq { | ||
|
||
implicit class EqOps[T](val x: T) extends AnyVal{ |
There was a problem hiding this comment.
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._
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Принято)
src/main/scala/fpspeedrun/Ord.scala
Outdated
def compare(y: T)(implicit ord: Ord[T]): Compare = ord.compare(x, y) | ||
} | ||
|
||
implicit val ratioOrd: Ord[Ratio] = (x: Ratio, y: Ratio) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В данном случае, если бы это лежало в компаньоне типа, не пришлось бы реализовывать и Eq и Ord, хватило бы этого инстанса
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это можно пояснить немного? В компаньон типа Ratio этот инстанс лучше положить?
src/main/scala/fpspeedrun/Ord.scala
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нехвостовая рекурсия = StackOverflow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А разве не хвостовая, рекурсивный вызов as compare bs
же последнее действие в цепочке?Или из-за того, что операция compare
ещё через имплисит обрабатывается?
src/main/scala/fpspeedrun/Ord.scala
Outdated
object OrdInstances { | ||
import Ord.OrdOps | ||
|
||
implicit def sizeFirstListOrd[T : Ord]: Ord[List[T]] = (x: List[T], y: List[T]) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не очень понял смысл этого модуля, но этот метод по крайней мере стекобезопасен, хотя и нарушает общепринятый лексикографический подход, его бы объединить с решением сверху
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это просто искусственно придуманные варианты)) Чтобы поиграться с несколькими инстансами
No description provided.