Temporary collections for Scala?

Clojure has a very nice concept of transitional collections . Is there a library providing libraries for Scala (or F #)?

+4
source share
3 answers

It sounds like a really great concept for a language like F #, thanks to an interesting link!

When programming using arrays, F # programmers use the exact same pattern. For example, create a mutable array, initialize it imperatively, return it, and then work with it using functions that regard it as immutable, such as Array.map (even if the array can be mutated, since there is no transition array).

Using seq <'a> type: One way to do something like this is to convert the data structure to a common sequence ( seq<'a> ), which is an immutable data type, and therefore you cannot (directly) change the original data structure via seq<'a> . For instance:

 let test () = let arr = Array.create 10 0 for i in 0 .. (arr.Length - 1) do arr.[i] <- // some calculation Array.toSeq arr 

It’s good that the conversion is usually O (1) (arrays / lists / .. implement seq<'a> as an interface, so this just does). However, seq<'a> does not preserve the properties of the original collection (for example, efficiency, etc.), and you can only process it using general functions for working with sequences (from the Seq module). However, I think this is relatively close to the transitional collections pattern.

A similar .NET type is also ReadOnlyCollection<'a> , which transfers the type of the collection (somewhat more powerful than seq<'a> ) into an immutable wrapper whose operations change the collection throw exception.

Related types: For more complex collection types, F # /. NET usually has both mutable and immutable implementations (immutable from F # libraries). Types are usually very different, but sometimes have a common interface. This allows you to use one type when you use a mutation and convert it to another type when you know what you no longer need. However, here you need to copy data between different structures, so the conversion is definitely not O (1). It could be a cross between O (n) and O (n * log n).

Examples of such collections are mutable Dictionary<'Key, 'Value> with immutable Map<'Key, 'Value> and mutable HashSet<'T> or SortedSet<'T> with immutable set<'T> (from F # libraries).

+4
source

I don’t know of any library for this in F # (nothing in the standard library, and I don’t remember anyone from the blog anything like that, although there are several libraries for simple constant / immutable structures). It would be great for third-party developers to create such a library. Rich Hickey is a man these days, when it comes to these amazing practical (mostly) functional data structures, I like to read about it.

+3
source

Please take a look at the following post: Daniel Spivak:
http://www.codecommit.com/blog/scala/implementing-persistent-vectors-in-scala

He also ported the Rich Hickey algorithm to Scala. IntMap is also mentioned in the article, which is almost as quickly implemented by Clojure.

0
source

Source: https://habr.com/ru/post/1305957/


All Articles