![]() ![]() That allows to do exactly that, creating all the necessary lenses under the hood: scala> import ._ In Scala there is a great library called quicklens However most of the time our goal is just to update data. So far we have replaced the copy boilerplate with a number of lens declarations. (diagram(startup) + diagram(raisedFounder)).render( "startup") RenderingOptions = RenderingOptions(density = 75),ĭirectory = Paths.get( ImagePath, "immutability", "lenses") extra declarations for this section import ntrib. ![]() Startup = Startup( Acme, Employee( Michael, 4010), List( Employee( Adam, 2100), Employee( Bella, 2100), Employee( Chad, 1980), Employee( Delia, 1850))) Startup = Startup( Acme, Employee( Michael, 4000), List( Employee( Adam, 2100), Employee( Bella, 2100), Employee( Chad, 1980), Employee( Delia, 1850))) Would require a lot of copy calls: case class Employee( However once composition comes into play, the resulting nested immutable data structures Scala> val raisedEmployee = py(salary = employee.salary + 10) Updating this sort of data can be tricky if it’s immutable.įor case classes Scala gives us the copy method: case class Employee( So far we were looking into “standard” data structures,īut in our code we often have to deal with custom data structures comprising our domain model. (diagram(vector1) + diagram(vector2)).render( "big-vectors", _.withVerticalSpacing( 2)) However as more layers leap into action, a huge chunk of the data can be shared: scala> val vector1 = ( 1 to 100).toVector (diagram(vector1) + diagram(vector2)).render( "vectors", _.withVerticalSpacing( 2)) The internal 32-element arrays form the basic structural sharing blocks.įor small vectors they will be recreated on most operations: scala> val vector1 = ( 1 to 20).toVector Which can be deemed constant time (yes, the trick is that the number of elements that can Therefore getting any element by its index requires at most 6 pointer dereferences, Internally vectors utilize up to 6 layers of arrays, where 32 elements sit on the first layer,ġ024 - on the second, 32^3 - on the third, etc. A Vector is a powerful data structureĪddressing this shortcoming and available in Scala (among other languages, like Clojure). We need to potentially traverse the whole structure. One downside common to both lists and queues we saw before is that to get an element by index, It consists of a number of cells pointing to each other: scala> val list = List( 1, 2, 3) We’ll start with one of the simplest structures: a list. RenderingOptions = RenderingOptions(density = 100),ĭirectory = Paths.get( ImagePath, "immutability", "data") Immutable data structures // extra declarations for this section val renderer = Renderer( The interactive sessionĪlready has all the necessary imports in scope. Reloads images automatically on file change). To start an interactive session, just run $ sbt render(List(1, 2, 3))Īnd open diagram.png in your favorite image viewer (hopefully one that Throughout this page we will assume the followingĭeclarations (each section might add its own): import re._ as an interactive playground where you can try the same commands I presented.as a reference/refresher on the concepts covered in the talk. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |