Player is loading...

Embed

Copy embed code

Transcriptions

Note: this content has been automatically generated.
00:00:01
i welcome to the last talk of today i'll be got enough sleep last night
00:00:07
this is about implementing the us got a lot to thirteen collections my
00:00:11
name is difference i go i'm a member of this quality might lie plant
00:00:16
and we just released to scholar to thirteen yesterday
00:00:26
i had the major feature is the redesigned collection library the main
00:00:31
goal was to make it easier for users with better discover ability
00:00:35
better error messages so normally use cases in this colour docks number
00:00:40
can build from leaking out an error messages when things go wrong
00:00:43
but it's also easier for collection improve matters because
00:00:46
it's more regular but there's still plenty of unnecessary complexity
00:00:51
so i'm uh going to show you how to implement different collections
00:00:57
before we get to the actual collections the white like to start
00:00:59
with the type called it durable ones which is not quite a collection
00:01:04
but it uh it can be consumed by many collection methods
00:01:09
terrible once basically means it has an iterative method so that's similar to it
00:01:14
a ripple in java except that in a scalable it rubble once we also allow
00:01:20
uh types that only allows single traversal for most important iterate are
00:01:24
still integrator extends it rubber ones and it's iterative methods just returns itself
00:01:31
and this is a lightweight interface that doesn't have any of the
00:01:34
additional methods that you would previously find on types liked reversible once
00:01:39
so it can be implemented easily but non collection types in particular
00:01:43
we now have option implementing curable once this was not true previously
00:01:50
so let's see what you have to do to implement a rubble once yourself
00:01:56
well this is the simplest thing you can do you just extend it double once in
00:01:59
this case of and i did a fine iterate iterative method that's all you need to do
00:02:07
there are two more things you might want to add though one is override known size
00:02:12
if you do not the size because it can be used to optimise certain collection operations
00:02:17
if you look at the bottom there's this example when creating an array pa for and then adding an instance often a rubble once
00:02:24
for three elements it doesn't really make a difference but if you have a lot of data in there the rape offer may have to re
00:02:30
crow several times in order to fit all the data but if you
00:02:34
although right now on size that it will free are located that correct size
00:02:40
the second thing you may want to implement all the ride is the stepper
00:02:45
stepper is a type that was previously part of of this
00:02:48
colour java eight compact library and is now in the standard library
00:02:54
so uh a stepper is basically like jobless
00:02:57
splitter rater it allows both high or low
00:03:01
conned a consuming of up a greater like things and
00:03:06
it also allows primitive specialise station four inches long and double
00:03:13
so and uh here we do this just but delegating to data dot stepper which gets a stepper from ray
00:03:21
if you look at an actual usage this is something we can do
00:03:23
this with a stepper so first we get the stepper but calling dot stepper
00:03:29
and this is actually of type int stepper it's not just
00:03:32
the stepper off and this explains why we need this a strange
00:03:37
functional dependency with the stepper shape the stepper shape
00:03:41
computes the correct stepper type for whatever element type
00:03:44
we have that's because we need to support jobless primitive specialise asian
00:03:49
so now we haven't it's stepper and if we call java iterate or it will give us a primitive iterate or of it
00:03:55
and then when you call next this is actually a
00:03:59
primitive into method which doesn't involve any boxing or on boxing
00:04:04
you could use the same code at the call side without overwriting
00:04:08
stepper method but that the false stepper implementation just goes through inter rater
00:04:13
so that that means it will box primitives and it will not support parallel uh processing
00:04:20
i
00:04:22
okay let's move on to the real collections though
00:04:27
i will start or something like this we're going to implement a sequence of elements of arbitrary types
00:04:34
it's supposed to be immutable and have array like performance characteristics
00:04:37
so it's basically immutable daughter macy which exists in the standard library
00:04:43
will we will do the real thing which has four hundred fifty lines of code so it might
00:04:47
get a bit that to read at the back of the room for put it on the slide
00:04:52
but the sizes may it because it supports
00:04:55
all the manually specialised versions for primitive types
00:04:59
we can do simple version which just keeps its data always boxed in an array of any
00:05:04
so we have our class my seek which extends some type and
00:05:08
contains the data so let's see which tried we should actually extend here
00:05:17
we have a a few different apps to collection types to choose from
00:05:22
at the top level there's collection it durable that's the basic type of a collection
00:05:27
and it is extended but immutable into rubble and immutable into rubble
00:05:32
in general you do not want to extend the type like collection it
00:05:35
rubble always pick the immutable mutable version depending on which you uh implement
00:05:42
at the level below we have types like seek and again there are three versions of seek there's a
00:05:48
collection see there's immutable seek and there's a mutable seek either works the same way for all of these types
00:05:54
the immutable and mutable versions always extend both they
00:05:58
uh immutable mutable parent type and the generic version
00:06:02
of the collection package so to keep this diagram a bit simpler we're going to collapse them like this
00:06:08
and we can take a look at the full hierarchy you start with a durable and we have
00:06:15
sick below it on the left and it is for the divide it into linear seek at index seek
00:06:22
engineers sick is one that uh that supports a in an efficient tail operation
00:06:28
and an index six apart efficient apply and length operation so depending on how you want to consume them
00:06:35
you may want and when you're sick or index the kind of course depending on what you have in your implementation
00:06:41
you usually want to extend one of the two but it's perfectly fine to extend the generic seek to
00:06:48
actually in the mutable universe we also have mutable part
00:06:51
for which is a sikh that allows growing and shrinking
00:06:56
the next to seek we have both set and map they are based on the
00:07:01
standard equals and has coat contract and their extended by sorted set and sort of map
00:07:08
these take an additional ordering constraint and uh oh
00:07:13
they keep their keys or elements in case of set
00:07:16
in this ordering map is further extended by c. come up
00:07:21
which is a map that has a sikh like ordering for its key so they are by insertion or
00:07:27
and finally we have the views there's a new tied extended by seek view and map to you but you don't
00:07:35
generally extent and if you want to implement collections at
00:07:39
if anything you may use them in to in your implementations
00:07:43
sick view and mad you just add some specific operations for views that come from seeks or maps
00:07:49
you may be wondering why are they not actually seek so maps well that's because of equality
00:07:55
the quality is defined at this level so every seek can only be equal to
00:08:01
another seek never to reset map of you even if they contain the same elements
00:08:07
and since uh equality has to be transit if you cannot implement more
00:08:11
than one of these uh top level types that and still get equality correct
00:08:18
okay so the one that we want is the red one here that's in immutable index sick so let's do that
00:08:28
so we at extends index sick of a and there are
00:08:32
two methods we have to implement they are length and apply
00:08:37
the implementations are pretty straight forward again we just forward to the
00:08:40
re methods because we want to do the simplest possible thing here
00:08:44
i had one other feature which is to override class name
00:08:48
just so we get the nice output here in the console
00:08:51
this was done listen if reflection hack by default in previous
00:08:55
versions but we remove that because uh it just leads to confusion
00:09:01
and that was this in place you can create a new my sick and you can call
00:09:06
for each or index where or any number of
00:09:09
other collection operations and this already works quite well
00:09:15
but it's it's dedicated to created by by calling the constructor
00:09:19
and passing this internal data structure that you're not supposed to see
00:09:23
so the next thing i'd like to do is uh to at a
00:09:26
factory implementation every collection usually has it a factory and its companion object
00:09:33
so we have object my seek and we extend sick factory of my seek
00:09:38
there is no specific index the factory so we take take the
00:09:42
next best thing by going up in the hierarchy and that's sick factory
00:09:46
and a factory has to implement three methods they are empty new builder
00:09:51
and from you would see the same pattern in many different instances further on
00:09:56
empty just returns an empty instance and because we haven't immutable collection it makes sense
00:10:02
to always return the same on so we have a cast instance that we return
00:10:07
then there's new builder that may be familiar to you from those colour to twelve collections
00:10:12
we always created a builder in order to build a collection
00:10:16
but it's trick building only doesn't support lazy collections like stream
00:10:20
or the new lazy list that's why we have two versions
00:10:23
no the basic one is from which also allows lazy building
00:10:28
imagine passing and iterate or two from it taken it rubble once we can pass any rater and then just
00:10:34
returning some wrapper which never consumes the rater so this is a lazy collection this is the way you build it
00:10:41
no you can then later consume the iterative one by one as needed
00:10:46
so i'm an array already has a from the thought so we can reuse it and we just wrap it in my seek
00:10:54
and we're done new builder works the same way we have to return a builder the reason why we have new builder
00:11:00
in addition to from is because for strict collections it's usually
00:11:04
more efficient to use the builder so you always have both
00:11:08
you could implement one in terms of the other as it before but i looked through the collection implementations actually
00:11:14
and so um there is not a single case at a store where we used is
00:11:18
the for the always override them separately for performance reasons so that's what we're doing here too
00:11:25
or a has a new builder method so we reuse it i can we map the result in you seek
00:11:33
so let's use the factory
00:11:37
we have the user factory methods available out of the box
00:11:40
just by implementing these methods we can call things like tabulate
00:11:44
or we can use it here but when converting an existing collection to my seek so this works
00:11:52
but not everything works we still have these two cases to deal with so one is uh
00:11:59
an example of a one case and the other one's a bit different will see the difference on the next slide
00:12:05
recalling filter and map here and the values you get in the results are correct
00:12:11
that's exactly what you expect but the types are wrong so both return and indexed seek
00:12:18
but not in my seek that that's what we want and the index sick is implemented by
00:12:23
a vector that's because vectors its default implementation of the immutable indexed eek so why is this
00:12:31
well the extended index seek off any and
00:12:34
this in turn extended trade caught in dixie ops
00:12:39
and indexing cops as these type parameters yeah they are they're called a. e.
00:12:45
c. c. and c. c. is the collection constructor c. is the collection type
00:12:52
in our case the collection constructors index seek and map returns that index the of the
00:12:59
and the collection teddy c. which is indexed sick of a so filter returns an index the cafe
00:13:06
we always use this e. type when we don't change the element types all the means
00:13:11
when we reuse elements from the current collection to build a new collection as in filter
00:13:17
if we have to change the element type and built some uh instance
00:13:21
of the collection with new elements we used to see see type here
00:13:25
so what can we do to improve that well as you can see here both ah cool variant that's plus
00:13:31
c. c. and plus c. so we can all right um and specifies up types so that's exactly what we do
00:13:38
we extend not just index sick of it but also in dixie of ops of
00:13:44
a comma my seek come on my sick of re so the overriding the inherited versions
00:13:51
and become part i would complain at this point because the
00:13:54
types no longer match so we have to implement it rubber factory
00:13:58
it's just the factory that we previously implemented and this gives us
00:14:03
a my seek a constructor basically that's the way to build
00:14:07
in my seek instance so this corresponds to the c. c. type
00:14:11
and in order to also get the c. type right
00:14:15
we extend it durable factory defaults but we will later see how to do this manually
00:14:22
you have to implement a few more methods for this but often the pattern is that uh
00:14:27
c. is just c. c. off a and in this case we can use it defaults version
00:14:35
i don't have a fully functional collection of implementation everything that you want to do with an
00:14:40
index seek works with this thing there's just a few more optimisation so you may want to make
00:14:47
first our everything we extended so far are traits
00:14:52
and if you extend a lot of traits you generate a lot of byte code you two were due to the waiting coding works
00:14:58
so we have a few abstract classes that you can you reuse to reduce the byte code size there is no
00:15:03
abstract index c. but there is an abstract eek so what
00:15:07
we can do is to extend abstract seek within next week
00:15:11
and then as i mentioned the default of building collections
00:15:15
is lazy which is not always the fastest for strict collections
00:15:19
since we're building a strict collection we have is a
00:15:22
special strict optimised traded you we can use instead so
00:15:27
there is again no strict optimised index the op so
00:15:31
instead we extend in dixie cops with strict optimistic ops
00:15:35
we just use the best available one which contains the optimisation is for or type
00:15:42
and finally it would be nice to make it zero lies a ball and we have
00:15:45
a default zero sizable trait that works for any standard a collection kind so you get
00:15:52
the sincere illustration that most of the standard library collections use there's there's nothing else you have to
00:15:56
do about it because it just uses your factory and iterate or to perform the serious i see realisation
00:16:04
and uh the only thing left to do now is to
00:16:07
optimise based on the actual collection implementation so usually you want to
00:16:13
overwrites the methods to make the more efficient for the data structure you implement so
00:16:18
for example map previously into twelve there would have been some can't bill from shenanigans and you need to
00:16:24
get a builder and have some special cases in there that's all gone so the map method that you override
00:16:30
looks exactly as you would navy right it
00:16:33
and the implementation is he is totally straight forward
00:16:38
yeah we this is just a simple example when we use the re to
00:16:43
our advantage and we return to my c. candy and there's nothing special about this
00:16:51
okay let's take a quick teacher at this point and talk about
00:16:55
collection kinds so well first uh start with kinds of from type theory
00:17:01
so in in type theory kinds are used to classify types like
00:17:06
types classified values so basically a kind is the type of a type
00:17:12
so we have the standard or put it in the standard type here we
00:17:16
we have proper types which are of kind type just written as a star
00:17:21
and that is that the kind of type that a proper
00:17:25
value has every value your program has a type of kinds type
00:17:31
and uh uh examples are string int but also list
00:17:35
of and a value kind of the type list of and
00:17:39
and next we have uh you re type constructors of kind type to type these
00:17:45
use some task uh like notation there's nothing specific for school i didn't want invented here
00:17:51
this would be for example the store set or my seek that we
00:17:55
just implement that that's a unit type constructor takes a single type argument
00:18:00
and that we have things like binary type constructors of
00:18:04
type type to type to type they originally carried version because
00:18:08
again it comes from mass call like a map it takes
00:18:12
to type parameters and produces a proper type in the end
00:18:18
that's not quite enough has got also we would have to extend it to support upper
00:18:23
and lower bounds and variance uh that's something adrian didn't this a paper on linking here
00:18:29
so basically we have to add an upper and lower bound
00:18:32
to every every a proper type at so i'm like this
00:18:40
and uh we have to i had a kinda kind annotation to the arrow
00:18:46
this is this is really not important for what we're trying to do and just giving you some background here
00:18:52
so the kind of the immutable not would have this plus arrow here because there's a plus
00:18:59
on this type parameter and for any ref map which has a constraint the key
00:19:04
type must extend any ref we would see the same thing here in the kind
00:19:10
but even these uh skylark extended kinds are not enough for the collection kinds that we want
00:19:18
so uh what we need to do mostly is track context
00:19:23
bounce context pond is similar to type on but not quite
00:19:28
the uh the main difference where we deviate from the tight zero definition
00:19:33
of a kind is that it type with the missing context aunt implicit
00:19:39
cannot be instantiated but it is still a valid type
00:19:44
basically when you have any in violation of a a subtype
00:19:48
pound on a on a kind the tight itself is invalid
00:19:53
but we care not only about data types but also
00:19:56
about being able to instantiate these types and the values
00:20:02
so that's why we we use this extended not quite correct definition of kinds
00:20:08
we have a few different standard kinds that we are support
00:20:12
in a standard library out of the box was different abstractions
00:20:16
we've already seen it durable it on my sick implementation that's just type to type
00:20:21
and there's both an abstract collection type for this and there is a factory type that you can use
00:20:27
for this kind then we have map which is a binary type constructor that's keep the scary one for now
00:20:36
let's look at sorted it rubble that is really just an in rubble wasn't additional
00:20:41
ordering a constraint on the element type so that's the context on on the element type
00:20:48
i know if we look again at the scary what the evidence it durable
00:20:52
this is really a started it herbal but we abstract over the ordering
00:20:58
so it's not only ordering you first have to get this type an
00:21:03
which is also off kinds type to type and that we can
00:21:07
use it as a context beyond so the for the generalisation off
00:21:11
of uh ordering related it ripples of saw that it robots
00:21:16
but we do not have an abstract type for that because what does it mean if ray
00:21:21
if a collection take some arbitrary um context pound for a
00:21:27
it's element type it doesn't have any meeting so they cannot be
00:21:31
napster collection type but we do have a factory because we know
00:21:34
how to construct these by giving it a dis um context bound
00:21:40
and the next to sort it durable we have sort of map which is the
00:21:43
same for map types with an ordering constrained and we have specific it or a pulse
00:21:50
for example pits that these are already proper types and they also all or supported directly with the factory type
00:22:00
so if we go back to our uh type iraqi
00:22:04
let's look at where these uh these kinds these collection kinds shop you see them here
00:22:12
but there is a an inheritance hierarchy here so what does that mean well it means that every map
00:22:18
is not just off kind map it is also of kind a durable because it extends into rubble
00:22:24
and every saw that map is not just of kind saw that not it
00:22:28
is of kind saw that map it is of kind not added of candidate rubble
00:22:33
we have to implement all the different kinds for for these collection types
00:22:38
so i'll probably do this well it goes back to
00:22:43
the c. c. types the collection constructors that we use
00:22:47
'cause these are different for the different kinds and the way we do this is by having separate ones for each kind so
00:22:53
if you look at the definition of map ops here slightly
00:22:56
simplified it's it takes a c. c. type parameter which has a
00:23:03
to type parameters itself the binary type constructor here that's a map c. c. but
00:23:10
they uh see see it passes on to a terrible ops is not this c. c.
00:23:15
that's just a durable so these are separate c. c. types for each kind
00:23:20
the same for sort of laptops it takes a c. c.
00:23:24
but the one it passes on to map ops is just map
00:23:30
i generally you don't care about these inherited once you only ever want to refine the most
00:23:36
concrete one but you could refine all of them if if that's what you need in your collection
00:23:41
so um maybe it becomes clear if you if we look at an
00:23:45
actual implementation so the simplest one we can do here is a map
00:23:51
let's start with the factory this time there is a special map factory type
00:23:56
and implementation looks very much the same as the other factory that we've implemented there's really only one
00:24:02
difference here the free methods we have to implement
00:24:05
empty from i knew better now take to type parameters
00:24:09
that's the only difference what they do is exactly the same and we implement them in the same way basically
00:24:16
there's also a a a a custom builder implementation that i wrote you just because the
00:24:21
there wasn't anything really fast enough in the standard library to reuse so these are all
00:24:27
also pretty simple to ride in a few lines of code but the main
00:24:30
take away here is that these factories all look the same except for the parameters
00:24:35
if we were to implement a saw that map factory it would
00:24:38
again be the same just with uh and ordering constrain only scary types
00:24:47
so if we look at the actual a collection class my map
00:24:53
what i'm doing here is uh is wrapping immutable job i hash map
00:24:58
it it immutable scholar collection but it's not something you want to
00:25:02
do impact is usually for performance reasons but it's easy to implement
00:25:08
so be extent some abstract map type we
00:25:11
um makes in strict optimised laptops which has
00:25:16
key and value type parameters it has a. c. c. type the one for map
00:25:21
and it has a c. type mind that off the key and value type
00:25:27
the same basically as my sick just with the two time type parameters instead of one
00:25:32
we also extend map factory defaults because again we have this relationship we
00:25:36
have my map of k. comma be for my map and k. and he
00:25:43
so all they have to do is implement overwrite it mapped factory method
00:25:48
notice that this is no map factory it's not it rubble factory this was a was a
00:25:54
bit tricky in the old collections because they didn't
00:25:56
have any factory abstractions in this way for uh
00:26:01
for a collection that are not available kind now we have them so we do not overwrite it rubber factory
00:26:07
in fact we inherit this one the one i commented out is the one we inherit it or will factory
00:26:13
is just immutable irritable because we do not change the it durable c. c.
00:26:17
type your your to change the map c. c. type so we overwrite map factory
00:26:27
for the actual implementation thereof for methods
00:26:30
we need for the map we need to get it or greater than the get method which returns a value
00:26:36
there's a remove my thought that create a new map implementation without that
00:26:41
value added updated method that create a new map with the new binding
00:26:46
uh the implementation here is a fairly straightforward on an interesting
00:26:54
instead let's uh look at how you would override methods
00:26:58
for performance so i showed you earlier that we overrode the map method in my seek
00:27:04
with a a custom implementations so we can do the same for my map
00:27:09
it said there are now two map methods and that's because we implement two
00:27:13
different collection kind so we have to map math map methods the first one
00:27:18
is the one from a map ops it takes both the key and value type around around and it applies whenever
00:27:24
you're mapping returns another key value pair 'cause then you want
00:27:28
to get another map back in this case a mind map
00:27:32
the second one applies when you return something else that is not a key value peer
00:27:37
and then you just tilted robot that's the map method we'd had or heard from the rubble ops
00:27:42
if you all the right one you generally should override the others and just call super that's because of the way
00:27:49
implicit up the overload resolution barks so the compiler might otherwise get
00:27:54
confused which one you want to call when they come from different
00:27:57
points in the hierarchy so it's always best to override all of
00:28:00
them and only in one of these you would put your actual implementation
00:28:07
and this applies to all generally to all the methods that return e. c. c.
00:28:11
because we have different c. c. types at the different levels any method that returns is
00:28:16
c. c. will be overloaded at that level the most important ones that you see in the
00:28:20
in the standards collection ops traits a map flat map collect in contact
00:28:26
so these are the ones you usually want to over ride in this way
00:28:33
so what about non standard collection kinds of showing a few
00:28:36
of the standard ones but maybe you want something completely different
00:28:41
and you won't be the first one to want that because we even have them in the standard library
00:28:46
they're just not worth adding any abstractions for them but these
00:28:50
or just individual implementations of collections that support even other other kinds
00:28:56
the first one is any ref map which is a map whose key type must be a subtype of any ref
00:29:03
so if you'll recall the slide about the extent it's colour types we have to take the
00:29:10
uh this uh this uh super type and subtype constraints into account so it's a different kind so
00:29:16
we need something more specific than just map for it there's also in map and long not which are maps
00:29:23
that have in or long respectively as the key types and you only can can only parameter as the values
00:29:30
then we have bit set which is always a set of int sounds simple
00:29:35
enough as a as an implementation but we'll get back to that in a minute
00:29:40
and finally there's collusion prove hash map that's
00:29:42
a new map implementation in us got two thirteen
00:29:47
which uh uses and ordering to uh to combat uh
00:29:54
uh excessively bad performance in in case of a of hash collision attacks
00:30:02
that's something that can happen in the two twelve i'm not
00:30:05
implementations if someone produces deliberate hash collisions you get a bad
00:30:10
performance it has some up so this can be used as an attack vector vector that's got the job a hash map
00:30:17
uh has a clever hack for that it it checks if
00:30:21
the type implements comparable and then further checks with some uh
00:30:26
reflection magic if it's comparable to itself and that
00:30:30
it performs this optimisation just hoping for the best basically
00:30:34
but it's got all we have all during other type class and generally
00:30:38
you don't implement comparable so this wouldn't work for your custom type song
00:30:43
instead what we do is are you getting map that has an ordering of
00:30:49
context down for the key type but it is not a sorted map that that's the main take away
00:30:55
here meteorite constrain but is not a started nap so it cannot use the standard abstractions for started maps
00:31:02
i don't want to to show want to show you any um the full implementations for these types
00:31:08
uh but just going to look at individual features the deal mostly the same problems you
00:31:14
will encounter and all of these but it's easier to see in some than in others
00:31:19
let's start with immutable bit set so you
00:31:22
can see we extend straight optimised sorted sat ops
00:31:28
and the c. c. tell it is started set and the c. type is it set is not started set of int
00:31:34
because if you map from a bit set and return into values it would be nice to produce
00:31:39
another bit set as a result that's what all this can build from ninety to ten to twelve
00:31:46
so um that means there is no factory d. for straight the factory defaults traits
00:31:53
are only available if you have this correspondence that c. equals c. c. of a so
00:31:59
what we do instead is to implement a way to produce a bit set the c. type directly
00:32:06
again there are three methods and they should be rather familiar to you they were empty
00:32:12
from specific and you specific builder it's basically a factory for the c. type that we have to implement
00:32:18
and uh we're doing this similarly to the other collections by having a bit set factory but
00:32:24
that's all over right and we're not implementing anything that's that's just
00:32:28
so convenient way of writing it and then we're delegating to this factory
00:32:35
but if you forget about those or what the exact signatures or the compiler will tell you
00:32:41
we we have a previously interstates in like two twelve miles
00:32:45
still on four or five where we had some extract abstractions there
00:32:49
could actually forget this and you just some hon sound isn't
00:32:52
the compiler it wouldn't tell you that you forgot to implement them
00:32:55
so we change that there's a bit more boilerplate no but it is safe the compiler
00:33:00
we'll tell you when you forget this just like it will tell you when you forget over at the factory
00:33:08
so let's move on to the collision prove hash map as i mentioned it
00:33:12
requires an ordering for the key type but it is not a sort of not
00:33:19
but we still at a sort of map factory here and that's perfectly fine
00:33:26
the only extend not ops with map so it's there's no sort of methods to be seen anywhere
00:33:32
but we can use the factory because the factory just cares about how to build this
00:33:37
and in order to build this you need to give it an ordering for the key type the same as for started about this
00:33:43
there's nothing really starting specific in the factory type so we reuse it
00:33:51
i hear it into map
00:33:56
we can see it extends strict optimised map ops
00:34:01
of hints commodity come a map i my into map of t.
00:34:05
so of course we have the same same problem again with the ah
00:34:09
it was eighty four straight we have to implement or or three factory methods are
00:34:13
not showing them here and said what i want to show is yet another map methods
00:34:19
because if you call map on internet on your return a
00:34:23
pair whose first element is another int so this thing here
00:34:29
it would be nice to return another into map and not just a nap
00:34:34
so again we override the overloaded here that's another all the loading of the map method
00:34:39
you know already inherit the other two ones from now and uh i did rubble and we can overloaded again this
00:34:46
is something you would have avoided in in early us collaborations
00:34:49
mainly because type inference just didn't work for these overloaded methods
00:34:54
you you would have to annotate every call side with the precise type so you never did this kind of code but
00:35:00
we quickly improve overload resolution it's got to thirteen so you can easily
00:35:05
do things like that and have the compiler figure out all the types
00:35:11
and the implementation is something you will commonly see if you if you write these abstractions
00:35:17
this is basically the baseline implementation of most methods in the library at least the
00:35:23
ones that are not specific to strip collections you create a view for the operation
00:35:29
which works because we already have these operation we already
00:35:32
have map implementation so there's a map view for that
00:35:36
so we just wrap this interview let's give you figure out how to do the map and then
00:35:42
use this in to map from method that we provide here to build orange not from the view
00:35:47
of course you can optimise it further but this is a reasonable t. for just
00:35:52
just to get the basics signature in there to make it work i and you stay in that factory
00:36:03
the most interesting part about it so far is actually that the it's not really interesting
00:36:09
it doesn't extend anything it's just a simple object there is no abstraction for building an intern up
00:36:16
the the type to read it kind of it is the same as
00:36:19
it was for it rubble but of course building it works differently you need
00:36:23
matt pairs of end and that other type to build an internet and we don't have a
00:36:28
factory type for that so you can just implement whatever you want to build your own factory here
00:36:35
we have to use on efforts methods to to make it look like the other
00:36:38
factories so that's what you generally do but you're free to do whatever you want
00:36:43
um there's one piece that's missing though because um
00:36:50
well if you want to the keynote yesterday martin told you there
00:36:54
is no more can build from it's got to thirteen he's technically correct
00:37:02
there are actually two of them now
00:37:06
and they are caught built from and factory but having to sell it sounds worse but it's
00:37:11
actually better because you you have one axes here you have one axes here for these two
00:37:18
and the candle from previously gave you all these area in
00:37:21
between all that has gone away all the complexity in there
00:37:25
now we only have factory to built to enable building driven by a static type
00:37:31
and don't from for collection building driven by a dynamic type we already have
00:37:36
an instance of that collection and you want something of the same uh dynamic type
00:37:43
for concrete collection implementation so not for defining new abstractions
00:37:49
this doesn't really make a difference because if you if you have a concrete collection type then static and dynamic
00:37:55
type or the same so it it's just the same boilerplate you have to write twice for both of them
00:38:01
and uh or not having can build from is also correct in the sense that uh
00:38:08
all of the methods we've seen so far have not used candle from you will will only find
00:38:15
one or two methods maybe in the whole collections library that that use these types they are very rare
00:38:21
and only use the corner cases both of them have
00:38:26
two different ways of using them one is by an implicit conversion off the companion object
00:38:32
which sounds strange at first but uh it's quite natural and you've
00:38:36
already seen it used so the typical one for factory is this
00:38:42
you have an existing collection and you call the two method on it and you
00:38:46
just pass some other accompanied object at this works phone internet even though into map
00:38:52
doesn't extend any of the standard collection factory types and it works
00:38:56
because it's my provides an implicit conversion to factory from this object
00:39:02
and the other way of using it is what an implicit instance for the type i've written implicitly
00:39:08
here just to show you how it works over conjuring up a factory from into to list of and
00:39:14
i'm writing implicit implicitly because there is not a single abstraction the standard library that actually uses this
00:39:21
but you can use it in your own code if you want to have this can be useful
00:39:26
for stuff like type driven zero legislation o. d. c. realisation that's that's the use cases we had in mind
00:39:33
and then future uh build from is is actually used typically in this way
00:39:38
the main use cases for future dot sequence so we haven't it rubble of future of int
00:39:45
and does it really could be anything it could be a vector all this maybe
00:39:50
and if you call future dot sequence you will get a
00:39:53
future of arab off into just swap the two type constructors
00:39:57
it would be nice to have the actual implementation of those it ripples
00:40:01
be the same as the one you put in a even though statically you
00:40:04
only know that it's an interval and that's what built from dallas so
00:40:08
if you put in a list of future of and you will get out
00:40:12
a future of this stuff and not statically but at run time
00:40:18
what you can also do is rely on the implicit conversion off the companion object
00:40:23
just a tad this so this is what usually the implicit parameter here
00:40:28
i don't set with passing list so that means the list that's the
00:40:32
replacement for collection dot break out that you may have used intuitive off
00:40:37
but it only works in these very specific cases like future dot sequence word is actually used
00:40:45
oh so his are in that factory
00:40:49
that this looks like a lot of codes and and it is
00:40:52
but it's boilerplate so there's there's not much to it what we have
00:40:58
is a factory we can just use the singleton object here that we cast to whatever type we ate
00:41:05
and i get it has the same methods that we've always see there's a from specific
00:41:09
and there's a new build a method that we implement and both are straightforward and that we have
00:41:17
it rubber factory method that returns a an implicit factory for whatever
00:41:23
type and we have eight to factory method that takes as it
00:41:27
dammit parameter this optic itself and converted to that type
00:41:32
so this wires up factory and then we have exactly the same thing again for bill from
00:41:39
the only difference here is really that
00:41:42
this these methods additionally take if from parameter but we
00:41:46
don't use it anywhere because we don't care about the object
00:41:49
because the static and dynamic type are always the same
00:41:52
so the implementations are the same as they are for factory
00:41:58
okay but getting to the end so that sum up what we hopefully
00:42:04
remember what i try to show you at least the first thing is that
00:42:09
the integration often not collection types can be done by interval once it will once
00:42:15
connects this the sequential collections from the standard library at the parallel
00:42:20
collections at all to java it streams for both sequential and parallel iteration
00:42:26
and it also supports on box iteration of collections
00:42:32
that we used collection factories and we saw that there
00:42:36
is no special treatment of it rubber kind of connections anymore
00:42:40
all the different kinds are on equal footing and they're all supported and you can
00:42:45
add your own abstractions just move a bit of boilerplate for both from and factory
00:42:51
and these last two types replace scandal from in is simpler more directed way
00:42:59
if you'd like to know more about the architecture of the
00:43:01
two thirteen collections there is an overview documents they are called the
00:43:06
architecture of colour to thirteen says collections which was a re written
00:43:10
first got to thirteen specifically it's on doc starts cullen don't work
00:43:15
and uh uh the main collection documentation on that
00:43:18
website has also been updated for two thirteen so
00:43:22
if you go there you'll find two different versions would to twelve an earlier and for two thirteen now
00:43:27
and you can find the demo project with these types of uh
00:43:31
was implementations i showed you and some sample code to run them
00:43:34
on my get up together with the slides and if
00:43:38
you want you can follow me on tutor thank you how
00:43:47
hi i think we're out of times that right
00:43:52
or if people have questions or you have time
00:43:59
there's a question
00:44:05
yes is there any movement for a collections that are kind of exceeding the size of
00:44:10
the into his indexing so that you have like lists that are indexed by long something to
00:44:17
i remember seeing a ticket about that but we didn't do any work on in this area
00:44:30
oh that's right accumulate years these days import this that that's
00:44:35
another type for integrating with parallel connections and job light streams
00:44:42
uh thanks for the talk um are expecting the same architecture for doughty or is gonna change again
00:44:48
um i expect some minor changes it to fourteen
00:44:52
bucks that's basically it because to fourteen is a
00:44:55
a compiler focused really is not aligned refocused one so it will just be standard maintenance and then
00:45:01
dotty or scholar three point will be used to to
00:45:03
fourteen collections verbatim because it uses the exact same standard library
00:45:14
thanks for talk and talk a bit about connection brigham young because of the future no but how is it
00:45:21
can work for you want to map collection two different when we got off normal commercial use iterate are generally
00:45:29
call it rater and then almost all of the collection methods are also need
00:45:33
rater in some cases like for map methods that may not be an iterative
00:45:39
that probably available on view so you can call documents sort of that integrator
00:45:43
and in the end you just call to and but whatever you want from it
00:45:54
is another one up
00:46:00
hi um are you just one or if her
00:46:04
we can use non standard or implementation to um um
00:46:11
tired or prove him better machine large or receiver in america
00:46:18
sorry i didn't understand that oh can
00:46:22
you use some kind of your features trim
00:46:26
to be sorry prove ah what's pattern matching you if ari
00:46:32
a march or cherries list of ends a
00:46:37
parent list extreme it's fun for it's both lists
00:46:43
yes so that's why she you'd have to get a a class tax somewhere for the element type
00:46:49
from you context so that that's just you two rages over there's really not much we can do
00:46:55
as a d. for than the standard library there is class tag and that's
00:46:59
what you need to use all you have to wire it up yourself basically uh_huh
00:47:12
so there is a a scholar fixed rule to migrate to
00:47:17
two there isn't collections
00:47:20
yes and uh and that's gonna collections compact repository
00:47:23
there's a set of rules to perform the migration so
00:47:27
um one of my coworkers shane tell more always ask you
00:47:32
ah he tried it and the code broke because there's like a mismatch between
00:47:37
immutable immutable collections um yeah that that's why are already replied to monitor about us
00:47:44
well the so if you wanted to say basically you should be more funnier but i have no idea what's happening so
00:47:51
well we are anyway that's what is what doesn't exist so that the the thing here is um you can
00:47:58
get some break it just because seek into thirteen is
00:48:01
now immutable seek by default the same goes for index seek
00:48:05
so the easy thing to do would be to provide a
00:48:08
rule that changes every existing sickening exceed to be of the
00:48:12
generic collection dot whatever kind but that will not be idiomatic
00:48:17
uh two thirteen called because in most cases you really want
00:48:21
an immutable seek or an immutable index egon it was
00:48:24
only not immutable by default just accidentally in into twelve
00:48:30
and it it's good if your if you go through your code and how they
00:48:34
there was to be really immutable than only use a generic ones where you need to
00:48:38
but uh it would make sense to have it as an optional rule just to do the the
00:48:43
easy thing so does your role take the generic one and then convert it into the mid immutable and
00:48:48
um as far as i know there is no rule at the moment to do
00:48:52
any of that but it it would be nice addition to have one that just
00:48:55
i don't say it's the types basically so when you just use coloured dots seek
00:49:00
it would change it interest got of collection dot seeks to get the same thing
00:49:04
okay cool and how would that that be added to stella collection combat
00:49:10
its multiple request call i have never actually implemented the rule myself so
00:49:16
you will have to ask julia or someone else who actually worked on these i only know that they exist
00:49:23
uh one last question
00:49:26
uh_huh
00:49:28
oh
00:49:34
so what is your trade for a stricter minimise the uh operations but does this mean very very
00:49:42
collection we implement restraint has all the operations implemented as
00:49:47
a strict so optimism to be straight or we are still
00:49:51
variability depending on the middle class so um
00:49:56
whether it collection is strict all lazy first of all depends on how you build it
00:50:02
if you're from method built in the strict way by consuming the it herbal once that it gets then
00:50:08
your collection is fully strict because building it always consumes
00:50:12
the source so that this is just the performance optimisation
00:50:16
we all wrote the methods where it makes sense to overwrite them to get better performance
00:50:22
but at that point old operations are overridden attributed to be you're working right to restrict
00:50:29
or of the well you said well just go improvement or oh all by would make sense where it
00:50:33
makes sense or possibly not all of them because we just didn't manage to benchmark every single operations but
00:50:40
the critical on sorry no there's it's better to implement the ministry way or they were previously
00:50:44
implemented and strict way those older and there is a documentation about what is implemented and overwritten
00:50:52
there's no documentation have to look at the source and and see
00:50:55
what it actually overrides thank you but again it's just a performance optimisation
00:50:59
it's not about strict versus lazy everything is strict if you're builders if you're building a collection of strict way
00:51:07
things are
00:51:08
much

Share this talk: 


Conference Program

Welcome!
June 11, 2019 · 5:03 p.m.
1575 views
A Tour of Scala 3
Martin Odersky, Professor EPFL, Co-founder Lightbend
June 11, 2019 · 5:15 p.m.
8340 views
A story of unification: from Apache Spark to MLflow
Reynold Xin, Databricks
June 12, 2019 · 9:15 a.m.
1271 views
In Types We Trust
Bill Venners, Artima, Inc
June 12, 2019 · 10:15 a.m.
1569 views
Creating Native iOS and Android Apps in Scala without tears
Zahari Dichev, Bullet.io
June 12, 2019 · 10:16 a.m.
2233 views
Techniques for Teaching Scala
Noel Welsh, Inner Product and Underscore
June 12, 2019 · 10:17 a.m.
1296 views
Future-proofing Scala: the TASTY intermediate representation
Guillaume Martres, student at EPFL
June 12, 2019 · 10:18 a.m.
1159 views
Metals: rich code editing for Scala in VS Code, Vim, Emacs and beyond
Ólafur Páll Geirsson, Scala Center
June 12, 2019 · 11:15 a.m.
4697 views
Akka Streams to the Extreme
Heiko Seeberger, independent consultant
June 12, 2019 · 11:16 a.m.
1552 views
Scala First: Lessons from 3 student generations
Bjorn Regnell, Lund Univ., Sweden.
June 12, 2019 · 11:17 a.m.
577 views
Cellular Automata: How to become an artist with a few lines
Maciej Gorywoda, Wire, Berlin
June 12, 2019 · 11:18 a.m.
386 views
Why Netflix ❤'s Scala for Machine Learning
Jeremy Smith & Aish, Netflix
June 12, 2019 · 12:15 p.m.
5030 views
Massively Parallel Distributed Scala Compilation... And You!
Stu Hood, Twitter
June 12, 2019 · 12:16 p.m.
958 views
Polymorphism in Scala
Petra Bierleutgeb
June 12, 2019 · 12:17 p.m.
1113 views
sbt core concepts
Eugene Yokota, Scala Team at Lightbend
June 12, 2019 · 12:18 p.m.
1656 views
Double your performance: Scala's missing optimizing compiler
Li Haoyi, author Ammonite, Mill, FastParse, uPickle, and many more.
June 12, 2019 · 2:30 p.m.
839 views
Making Our Future Better
Viktor Klang, Lightbend
June 12, 2019 · 2:31 p.m.
1682 views
Testing in the postapocalyptic future
Daniel Westheide, INNOQ
June 12, 2019 · 2:32 p.m.
498 views
Context Buddy: the tool that knows your code better than you
Krzysztof Romanowski, sphere.it conference
June 12, 2019 · 2:33 p.m.
394 views
The Shape(less) of Type Class Derivation in Scala 3
Miles Sabin, Underscore Consulting
June 12, 2019 · 3:30 p.m.
2321 views
Refactor all the things!
Daniela Sfregola, organizer of the London Scala User Group meetup
June 12, 2019 · 3:31 p.m.
514 views
Integrating Developer Experiences - Build Server Protocol
Justin Kaeser, IntelliJ Scala
June 12, 2019 · 3:32 p.m.
551 views
Managing an Akka Cluster on Kubernetes
Markus Jura, MOIA
June 12, 2019 · 3:33 p.m.
736 views
Serverless Scala - Functions as SuperDuperMicroServices
Josh Suereth, Donna Malayeri & James Ward, Author of Scala In Depth; Google ; Google
June 12, 2019 · 4:45 p.m.
936 views
How are we going to migrate to Scala 3.0, aka Dotty?
Lukas Rytz, Lightbend
June 12, 2019 · 4:46 p.m.
710 views
Concurrent programming in 2019: Akka, Monix or ZIO?
Adam Warski, co-founders of SoftwareMill
June 12, 2019 · 4:47 p.m.
1975 views
ScalaJS and Typescript: an unlikely romance
Jeremy Hughes, Lightbend
June 12, 2019 · 4:48 p.m.
1377 views
Pure Functional Database Programming‚ without JDBC
Rob Norris
June 12, 2019 · 5:45 p.m.
6389 views
Why you need to be reviewing open source code
Gris Cuevas Zambrano & Holden Karau, Google Cloud;
June 12, 2019 · 5:46 p.m.
484 views
Develop seamless web services with Mu
Oli Makhasoeva, 47 Degrees
June 12, 2019 · 5:47 p.m.
785 views
Implementing the Scala 2.13 collections
Stefan Zeiger, Lightbend
June 12, 2019 · 5:48 p.m.
811 views
Introduction to day 2
June 13, 2019 · 9:10 a.m.
250 views
Sustaining open source digital infrastructure
Bogdan Vasilescu, Assistant Professor at Carnegie Mellon University's School of Computer Science, USA
June 13, 2019 · 9:16 a.m.
376 views
Building a Better Scala Community
Kelley Robinson, Developer Evangelist at Twilio
June 13, 2019 · 10:15 a.m.
245 views
Run Scala Faster with GraalVM on any Platform
Vojin Jovanovic, Oracle
June 13, 2019 · 10:16 a.m.
1342 views
ScalaClean - full program static analysis at scale
Rory Graves
June 13, 2019 · 10:17 a.m.
463 views
Flare & Lantern: Accelerators for Spark and Deep Learning
Tiark Rompf, Assistant Professor at Purdue University
June 13, 2019 · 10:18 a.m.
380 views
Metaprogramming in Dotty
Nicolas Stucki, Ph.D. student at LAMP
June 13, 2019 · 11:15 a.m.
1250 views
Fast, Simple Concurrency with Scala Native
Richard Whaling, data engineer based in Chicago
June 13, 2019 · 11:16 a.m.
624 views
Pick your number type with Spire
Denis Rosset, postdoctoral researcher at Perimeter Institute
June 13, 2019 · 11:17 a.m.
245 views
Scala.js and WebAssembly, a tale of the dangers of the sea
Sébastien Doeraene, Executive director of the Scala Center
June 13, 2019 · 11:18 a.m.
662 views
Performance tuning Twitter services with Graal and ML
Chris Thalinger, Twitter
June 13, 2019 · 12:15 p.m.
2003 views
Supporting the Scala Ecosystem: Stories from the Line
Justin Pihony, Lightbend
June 13, 2019 · 12:16 p.m.
163 views
Compiling to preserve our privacy
Manohar Jonnalagedda and Jakob Odersky, Inpher
June 13, 2019 · 12:17 p.m.
302 views
Building Scala with Bazel
Natan Silnitsky, wix.com
June 13, 2019 · 12:18 p.m.
565 views
245 views
Asynchronous streams in direct style with and without macros
Philipp Haller, KTH Royal Institute of Technology in Stockholm
June 13, 2019 · 3:45 p.m.
304 views
Interactive Computing with Jupyter and Almond
Sören Brunk, USU Software AG
June 13, 2019 · 3:46 p.m.
682 views
Scala best practices I wish someone'd told me about
Nicolas Rinaudo, CTO of Besedo
June 13, 2019 · 3:47 p.m.
2713 views
High performance Privacy By Design using Matryoshka & Spark
Wiem Zine El Abidine and Olivier Girardot, Scala Backend Developer at MOIA / co-founder of Lateral Thoughts
June 13, 2019 · 3:48 p.m.
754 views
Immutable Sequential Maps – Keeping order while hashed
Odd Möller
June 13, 2019 · 4:45 p.m.
278 views
All the fancy things flexible dependency management can do
Alexandre Archambault, engineer at the Scala Center
June 13, 2019 · 4:46 p.m.
390 views
ScalaWebTest - integration testing made easy
Dani Rey, Unic AG
June 13, 2019 · 4:47 p.m.
468 views
Mellite: An Integrated Development Environment for Sound
Hanns Holger Rutz, Institute of Electronic Music and Acoustics (IEM), Graz
June 13, 2019 · 4:48 p.m.
213 views
Closing panel
Panel
June 13, 2019 · 5:54 p.m.
400 views