![]() In ES6 there is a new Set type that works with all kinds of value types, but like with Map, it’sīased on reference equality, making it less useful when dealing with complex value types.Īs their name implies, sets have no duplicate elements. This of course means that the values must be converted to In JavaScript it’s typical toĮmulate a Set by storing the values as keys into an Object. SetĪ Set is like a Map without values, just the distinct keys. The average is calculated simply by flattening all scores into a single sequence and then calculating its average. The maxBy method to find the maximum score by looking at the second The best score is found by first flattening the whole structure into a sequence of (player, score) pairs. ImmutableĬollections in Scala use structural sharing to minimize copying and to provide good performance. More familiar mutable collections, but over time Scala developers tend to favor immutable versions. Coming from JavaScript it’s good to start with the In the example above the both versions are using mutable collections. HashMap ( "first" -> "James", "last" -> "Bond" ) p ( "profession" ) = "Spy" val name = s "$ For Object (or Map) the best match is the mutable HashMap. The closest match for Array would be the mutable Buffer since arrays in Scala cannot change size after Let’s start with familiar things and see how Scala collections compare with the JavaScript Array and Object (or Scala provides immutable and mutable implementations for all these collection types. The high-level hierarchy for the abstract base classes and traits is shown in the image below. Unlike JavaScript, the Scala standard library has a huge variety of different collection types to choose from.įurthermore the collections are organized in a type hierarchy, meaning they share a lot of common functionality and Of these the WeakMap and WeakSet are for special purposes only, so in your application you Furthermore both of these are mutable byĭefault, so if you pass them to a function, that function might go and modify them without your knowledge.ĮS6 extends your options with four new collection types Map, Set, WeakMap and In JavaScript there are basically two kinds of collections you have used to store your data: the Array for sequentialĭata and Object (aka dictionary or hash map) for storing key-value pairs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |