I don't know if it's digestable
Sep. 9th, 2021 07:41 pmMy slides for Scala BTB: https://tinyurl.com/adtincat
chaource
sassa_nf xacid getting a special invitation
What do you think?
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
What do you think?
override def equals(that: Any): Boolean = that match { case that: GenSet[_] => (this eq that) || (that canEqual this) && (this.size == that.size) && (try this subsetOf that.asInstanceOf[GenSet[A]] catch { case ex: ClassCastException => false }) case _ => false }
instance Eq1 Set where liftEq eq m n = size m == size n && liftEq eq (toList m) (toList n)
T(x)=1+x*T2(x)
, where x is the type of the value that the tree contains. dT/dx = T2 + 2*x*T*dT/dx
, whereby we havedT/dx = T2/(1-2*x*T)
List(x)
is defined by a formula List(x)=1+x*List(x)
, right? Either empty (=1) or a pair (ok, a product) (x, List(x))
, right?List(x)=1+x*List(x)
has a solution, List(x)=1/(1-x)
. Remember this.1/(1-2*x*T)
is a list, List(2*x*T)
dT/dx = T2 * List(2*x*T)
(bool, x, T)
, where T
is a tree.trait Monoid { def neutral: Monoid def binOp(another: Monoid): Monoid }
trait Monoid[Actual] { def neutral: Actual def binOp(another: Actual): Actual }
class StringConcatMonoid(val s: String) extends Monoid[StringConcatMonoid] { def neutral = new StringConcatMonoid("") def binOp(another: StringConcatMonoid) = new StringConcatMonoid(s + another.s) }
String
class, right? That's the problem, we cannot throw in the new functionality into String
. StringConcatMonoid
a model of Monoid
theory. We were lucky, we had just one type; imagine we had more than one. Then there's no way to inherit anything. And we are still not happy that we cannot define binOp
on String
s themselves; we look at Haskell, and seems like they do it easily.class Monoid m where binOp :: m -> m -> m neutral :: m
instance Monoid [a] where binOp = (++) neutral = []
object A { implicit object addMonoid extends Monoid [Int] { def binOp (x :Int,y :Int) = x+y def neutral = 0 } } object B { implicit object multMonoid extends Monoid [Int] { def binOp (x :Int,y :Int) = x ∗ y def neutral = 1 } } val test :(Int,Int,Int) = { { import A._ println(binOp(2, 3)) } { import B._ println(binOp(2, 3)) } }
binOp
, in another we used another. We can define fold that implicitly accepts an instance of Monoid, and provides the required operation, but that's beyond the topic of this talk.List[T]
, is not it a type class? We have abstract operations, independent of the type T
, and so it is also not a specific type, but a class of types, right?T
, we produce another type, List[T]
, uniquely determined by the type T
, and whose operations (almost) do not depend on what is T
.class Eq a ...
; в скале это можно задать приблизительно.