Writing a function of some generic scala collection - scala

I have defined a case class...
case class QueryRef[A](id: UUID, descriptor: (A) => Boolean, selector: immutable.Iterable[A] => A])
...that will be passed as a message between Akka Actors. The receiver will filter some collection of type A using the descriptor and then select a single element from the resulting filtered collection using the selector.
As written it will only work if the receiving actor's collection has type immutable.Seq[A]. I would like to generalize the above so that it would work with a generic collection of elements of type A. Is this possible?

Scala collections have a hierarchy, illustrated below. You just need to choose which level of the hierarchy is appropriate for your use-case. Iterable could be a good candidate for you if you want Maps and Sets to be allowed.
Of course, you can then only use those functions which are available at that level of the hierarchy, you wouldn't be able to use any Seq specific functionality.

Related

In Scala 2.13+, how can one efficiently implement a conversion to a particular standard collection type?

The current collection framework favors the use of to method in order to convert to the target collection type, and with an implicit conversion available from various collection companion objects to the Factory argument, this creates a neat, uniform interface. It unfortunately makes very hard optimisations which were easy in the old framework with CanBuildFrom. Lets say I have a custom collection type Unique[T], which is a combination of Set and Seq in that the order in which elements follows the order of insertion, but any element can occur only once. Because it offers fast indexOf, apply(i :Int) and contains, conversions to both Set and Seq can be O(1) with a simple wrapper. I can override toSet and toSeq, but I see no way of determining inside to (other than extremely questionable reflection) that the target factory builds Seq or Set, because the Factory implementation used by the implicit conversion is generic and not a prototype instance like old ReusableCBF.

Why a list in Scala is LinkedList internally?

I am new to Scala and got to know that a list in Scala is a singly Linked List under the hood.
Here is the documentation for the same:
A class for immutable linked lists representing ordered collections of elements of type A.
This class comes with two implementing case classes scala.Nil and scala.:: that implement the abstract members isEmpty, head and tail.
This class is optimal for last-in-first-out (LIFO), stack-like access patterns. If you need another access pattern, for example, random access or FIFO, consider using a collection more suited to this than List.
Why is it like that the list is a linked list internally?
Isn't it less effective in case a random access is required?

Subsets and shapeless extensible records

How to define polymorphic function which:
accept any record containing specified set of fields (superset of fields)
return any subset of specified set of fields
with shapeless-2.3?
I've found solution for single field for 1. but I need to work with a set of fields. I've found suggestion to define class containing implicits for each of the field, but I think it should be less boilerplated way to define it in such advanced language like Scala. I've found an assumption than SelectAll trait can be used for that but not concrete example how exactly it can be used.
Looks like your question is the duplicate of this one:
Checking for subtype relationship between extensible records in shapeless
The functionality you look for is implemented as Extractor typeclass and will be present in shapeless 2.3.3 (github.com/milessabin/shapeless/pull/714)

How do I choose the collection type to return in Scala?

In case of Set or List, the choice seems to be easier, but what do I do for Java's Collection, Iterable equivalent? Do I go for Seq? Traversable? GenTraversableOnce?
You need to decide based on your need. For example: According to Scala Documentation the definition of Seq is
Sequences are special cases of iterable collections of class Iterable. Unlike iterables, sequences always have a defined order of elements. Sequences provide a method apply for indexing.
So if you want to benefit ordering or you want to retrieve element by index you can use Seq
Again according to Scala Documentation if you are mainly interested in iteration over your Collection Traversable is sufficient
Just notice that there is a general good practice that for your function signature like function return type, use more general (abstract) data type to prevent unnecessary performance penalty for the function callers.
As often, it will depend on the needs of your caller.
Traversable is pretty high level (you only get foreach) but it might be sufficient. Seq would be used if you need a defined order of elements. GenTraversableOnce would be a bit abstract for me and possibly for your fellow coders.

Why does 'toSet' method mix up ordering of elements in ListBuffer?

In scala, why does toSet() method mix up the order of elements in a collection (ListBuffer)?
Which collection can I use to both secure uniqueness of each element and keep their original order?
Because the set abstraction, being a subclass of the traversable, has no guarantees about the order of elements held within:
A traversable class might or might not have two properties: strictness and orderedness. Neither is represented as a type.
...
If the class is not ordered, foreach can visit elements in different orders for different runs (but it will keep the same order in the same run).'
More precisely about why the elements get 'mangled': the toSet method constructs a new set collection out of some existing collection. It uses the default set implementation for this new set collection. The default set implementation is based on a hash table. In a hash table, the order of elements is undefined.