# ANN: TypeSortedCollections.jl

**URL:** <https://discourse.julialang.org/t/ann-typesortedcollections-jl/5414>\
**Category:** Community\
**Created:** [August 16, 2017, 3:57pm UTC](https://discourse.julialang.org/t/ann-typesortedcollections-jl/5414 "2017-08-16T15:57:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 16, 2017, 3:57pm UTC](https://discourse.julialang.org/t/ann-typesortedcollections-jl/5414/1 "2017-08-16T15:57:22Z")

</div>

I recently wrote and registered [TypeSortedCollections.jl](https://github.com/tkoolen/TypeSortedCollections.jl), a small package that allows you to perform operations on a type-heterogeneous collection in type-stable and statically dispatched fashion. I originally implemented this for use in [RigidBodyDynamics.jl](https://github.com/tkoolen/RigidBodyDynamics.jl), but decided to pull it out into its own package as I hope it will be useful for others as well.

TypeSortedCollections provides the `TypeSortedCollection` type, which sorts a type-heterogeneous input collection by type upon construction, and stores the elements in a `Tuple` of concretely typed `Vectors`, one for each type. TypeSortedCollections provides type stable methods for `map!`, `foreach`, and `mapreduce` that take at least one TypeSortedCollection.

An example (from the readme):

```julia
julia> using TypeSortedCollections

julia> f(x::Int64, y::Float64) = x * y
f (generic function with 2 methods)

julia> f(x::Float64, y::Float64) = round(Int64, -x * y)
f (generic function with 2 methods)

julia> xs = Number[1.; 2; 3]; # note the abstract element type; contains both Float64 and Int64

julia> sortedxs = TypeSortedCollection(xs);

julia> ys = [1.; 2.; 3.];

julia> results = Vector{Int64}(length(xs));

julia> map!(f, results, sortedxs, ys)
3-element Array{Int64,1}:
 -1
  4
  9

julia> @allocated map!(f, results, sortedxs, ys)
0

```

Please check out the readme for additional features and notes (especially on [when `TypeSortedCollection`s are appropriate to use](https://github.com/tkoolen/TypeSortedCollections.jl#use-cases)), and let me know if you have any questions!
