What is the difference between ::: and :::.() in scala -
in scala list class shown that:
list(1, 2) ::: list(3, 4) = list(3, 4).:::(list(1, 2)) = list(1, 2, 3, 4). shouldn't:
list(1,2) ::: list(3,4) = list(1,2).:::(list(3,4)) = list(3,4,1,2)
( method ::: prefixes list)
from docs:
def :::(prefix: list[a]): list[a] [use case] adds elements of given list in front of list.
example:
list(1, 2) ::: list(3, 4) = list(3, 4).:::(list(1, 2)) = list(1, 2, 3, 4) in scala, operators end in : right associative , invoking object appears on right side of operator. because right associative, prefix method called on object "to right", list(3, 4).
then name says, prefix list(3, 4) list(1, 2), , that's why list(1, 2, 3 ,4). not intuitive thing in world it's this:
a ::: b // ending in :, flip argument order, call method on b. b .:: // :: = prefix b result = a(concatenated) b now let's @ right hand side:
list(3, 4).:::(list(1, 2)) the . dot performing inversion of ::: prefixed invocation, invert 2 list objects before performing ::: prefix operation.
list(3, 4).:::(list(1,2)) = list(1, 2) ::: list(3, 4) // above. // , it's same story. the . dot way invert associative operators. a ::: b same b .::: a
Comments
Post a Comment