The Vala Tutorial mentions the following methods and properties (and operators) for built-in arrays:
arr.length
arr += element
arr.resize()
arr.move()
(By "built-in arrays" I mean ones like int[] arr = new int[5], in contrast to the fancy data structures provided by GLib or Gee.)
My question: do such arrays support any more methods and properties? Where is this documented?
According to the compiler source code there should also be arr.copy():
https://gitlab.gnome.org/GNOME/vala/blob/master/vala/valaarraytype.vala
Also if you look at the unit tests you can see that slices (e.g. arr[1:5]) are a feature of arrays:
https://gitlab.gnome.org/GNOME/vala/blob/master/tests/basic-types/arrays.vala
But I think thats it. The built-in array is pretty minimalistic.
Related
Most of what I know is Javascript. I believe "Lists" are the closest thing Dart has to Arrays, but are they technically the same thing? Is it wrong to think of them as Arrays? Similarly, would Maps be considered Objects? I realize there are likely differences, but would it be "wrong" to make that comparison?
JavaScript Array objects are similar to Dart growable List objects. Both can be added to and accessed by index. Dart lists can only be accessed by index, where JavaScript lists are full JavaScript objects (see below), it just has a magic length property that is only updated when using integer-named properties. In practice, use a Dart List where you would have used a JavaScript Array.
JavaScript Objects are a combination of multiple things. They are mappings from string names to values - that part is best simulated by a Dart Map<String, Object>. They also allow prototype based inheritance, and if you are using that, you probably want to define a Dart class instead. As a data structure, use a Dart Map where you would have used a JavaScript {} object.
Everything is an object in Dart.
EVERYTHING
Everything in Dart is an Object - a List, a Map, even an int:
https://www.dartlang.org/guides/language/language-tour#important-concepts
Dart has Lists which are ordered sequences of objects. There is no class or interface called Array, though Lists act extremely similar to Arrays you might have encountered in other programming languages.
Everything in Dart is an Object.
List is pretty close to JS Array.
Map is somewhat related to Object in JS, but not quite.
A map is just to store keys and values together, but in Dart you can have non-string keys. There are also no prototypes or similar for maps in Dart.
Which way is more idiomatic to use Nullable<'a> or to use Option<'a> for representing a nullable int?
Option is far more idiomatic in F# code.
It has far nicer syntax when used in match and has large amounts of support from the standard library.
However, if you plan to access the code from C# or some other language you should probably expose the interface with Nullable which is easier to use in C#.
As John said, Option<T> is definitely more idiomatic type in F#. I would certainly use options as my default choice - the Option module provides many useful functions, pattern matching works nicely on options and F# libraries are generally designed to work with options.
That said, there are two cases when you might want to use nullable:
When creating arrays of optional values - Nullable<T> is a value type (sort of) and if you create an array Nullable<T>[] then it is allocated as continuous memory block. On the other hand options are reference types and option<T>[] will be an array of references to heap-allocated objects.
When you need to write some calculations and propagate missing values - in F# 3.0, there is a module Microsoft.FSharp.Linq.NullableOperators which implements various operators for dealing with nullable values (see MSDN documentation) which lets you write e.g.:
let one = Nullable(1)
let two = Nullable(2)
// Add constant to nullable, then compare value of two nullables
(one ?+ 2) ?>=? two
I have implemented IEnumerable for a collection I built, and (although I have not tested them all) the Seq values appear to work correctly. Is it possible to override some Seq values, for instance "last", when the native performance of a value of my collection is better than using Seq's IEnumerable based function? I have not found any information on overriding Seq.
No -- the functions in the Seq module can't be overridden. However, some of them do try to optimize performance by checking their input value (the seq<'T> instance you pass them) to see if it's an instance of IList<'T> or 'T[]; if it is, the functions will take some optimized code path. For example, if you pass an array ('T[]) to Seq.length, it'll be able to quickly determine the length by using the .Length property of arrays.
If you're stuck on using the Seq module, the only performance optimization I can think of would be to have your collection also implement ICollection<'T> and/or IList<'T>. That may optimize some cases, but it won't be all cases.
As already said in the other answer, there is no way you can override the functions in the Seq module. If you're implementing a custom collection, then the best thing to do is to follow the standard pattern used by the core F# libraries.
The Seq module contains the most often used functions and functions that can be reasonably provided for any sequence.
Modules like Array or List provide more efficient implementations for a specific collection type and they add more functions (not available in Seq) that are specific to the collection (for example, functions List.tail and Array.get).
The best way when adding your own collection is to follow this pattern:
Implement IEnumerable<'T> so that the functions from Seq module work for your type
Create MyCollection module that contains efficient implementations of standard functions (at least those that matter to you) and adds more functionality that is specific to your collection.
I use F# a lot. All the basic collections in F# implement IEumberable interface, thus it is quite natural to access them using the single Seq module in F#. Is this possible in OCaml?
The other question is that 'a seq in F# is lazy, e.g. I can create a sequence from 1 to 100 using {1..100} or more verbosely:
seq { for i=1 to 100 do yield i }
In OCaml, I find myself using the following two methods to work around with this feature:
generate a list:
let rec range a b =
if a > b then []
else a :: range (a+1) b;;
or resort to explicit recursive functions.
The first generates extra lists. The second breaks the abstraction as I need to operate on the sequence level using higher order functions such as map and fold.
I know that the OCaml library has Stream module. But the functionality of it seems to be quite limited, not as general as 'a seq in F#.
BTW, I am playing Project Euler problems using OCaml recently. So there are quite a few sequences operations, that in an imperative language would be loops with a complex body.
This Ocaml library seems to offer what you are asking. I've not used it though.
http://batteries.forge.ocamlcore.org/
Checkout this module, Enum
http://batteries.forge.ocamlcore.org/doc.preview:batteries-beta1/html/api/Enum.html
I somehow feel Enum is a much better name than Seq. It eliminates the lowercase/uppercase confusion on Seqs.
An enumerator, seen from a functional programming angle, is exactly a fold function. Where a class would implement an Enumerable interface in an object-oriented data structures library, a type comes with a fold function in a functional data structure library.
Stream is a slightly quirky imperative lazy list (imperative in that reading an element is destructive). CamlP5 comes with a functional lazy list library, Fstream. This already cited thread offers some alternatives.
It seems you are looking for something like Lazy lists.
Check out this SO question
The Batteries library mentioned also provides the (--) operator:
# 1 -- 100;;
- : int BatEnum.t = <abstr>
The enum is not evaluated until you traverse the items, so it provides a feature similar to your second request. Just beware that Batteries' enums are mutable. Ideally, there would also be a lazy list implementation that all data structures could be converted to/from, but that work has not been done yet.
Is there similar component for Delphi like dictionary of Python?
I'm now using TStringList to map string/object pairs, but I'd like more general approach and maybe more powerful (TStringList has binary search when it is sorted).
Solutions for pre-D2009 are also welcome.
There is in Delphi 2009. A whole new containers unit has been added, and a Dictionary class is one of the classes available.
Couple this with Generics, and you have a very powerful set of classes.
There is a THashedStringList class (subclassed from TStringList) "hidden" in IniFiles.pas that can significantly speed up searching in a string list based dictionary.
I haven't tried it myself, but how about this Delphi Collections package? (Thank you Google).
If you are looking for some Hashtable implementations take a look at Hashtable implementations
I have allways used DeCAL. It includes both sequences, maps and hashtables.