data structures - Why is the return type of a method, a class name in the below code of scala? -


abstract class intset {    def contains(x: int): boolean    def incl(x: int): intset } 

i'm learning scala. why return type of method incl class name intset? mean?

usually logic , functional programming languages declarative (not per se enforced, in cases, declarative style advised). means not alter datastructure: construct new 1 modified original one. in declarative language when have intset , add element, not add intset, construct new intset has elements of original intset , 1 add x.

so reason why incl returns intset because original intset not modified, constructs new one.

usually when new intset constructed, of course not construct complete new object. since orignal intset not supposed change, can use sub-structures of original intset. therefore declarative languages have own set of dedicated data-structures, instance finger tree.

take instance this implementation of intset. see is:

object empty extends intset {   override def tostring = "."   def contains(x: int): boolean = false   def include(x: int): intset = new nonempty(x, empty, empty)   def union(other: intset): intset = other }

so when use empty intset object, , include element x, instead of altering object, construct new intset: nonempty(x,empty,empty). intset contains x.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -