Get all elements after a certain index in Dart - dart

I want to retrieve all elements after a starting index from a list
and return an empty list if out of range.
List myList = [1,2,3,4,5];
print([2:]) // returns [3,4,5]
print([6:]) // returns []
How do I do that in dart?

This is what the skip method is for. If you look at the documentation for it, it has the behavior you desire.
List<int> myList = [1,2,3,4,5];
List<int> newList = myList.skip(2).toList();
print(newList); //[3, 4, 5]
List<int> myList = [1,2,3,4,5];
List<int> newList = myList.skip(5).toList();
print(newList); //[]

Related

strange result in dart list how to solve this

i have the following
List list1 = [];
List list2 = [];
list2 = list1 ;
list1.add(1);
print(list1); // outputs [1]
print(list2); // outputs [1] WHY?
i only change list1 .. why list2 is always be the same ..
sometimes in my app i need to make a list == another .. and this is great .. but once i make it they always be equals to each other even if i make a change to one of them
Assign list copy with List.of constructor method:
list2 = List.of(list1);
More explanation of pointers and how it works you can find at my answer here.

FSharpList assertions with NFluent?

I read on the nfluent documentation that you can assert lists like this:
var inteers = new int[] { 1, 2, 3, 4, 5, 666 };
Check.That(integers).Contains(3, 5, 666);
But when I try this in F# I cannot seem to get it work:
let myList : int list = [1;2;3]
Check.That(events).Contains(1, 2, 3)
The error message is seen here:
How do I get the list assertions to work i F#?
Update
In my actual code the F# function returns an FSharpList and I don't want to change this so I cannot simple change the type to e.g. a seq.
F# doesn't do implicit casting like C# does. Method signature expects to have IEnumerable, but you supply a list. That's what the error say.
The easiest fix would be to create a sequence, that is same as IEnumerable in C#:
let myList : int list = [1;2;3]
let mySeq = list |> Seq.ofList
Check.That(mySeq).Contains(1, 2, 3)

What does the use of brackets really mean in F#?

I need help understanding the following statement:
let dictionary = [ for i in 1..4 -> i, true ] |> Map.ofSeq
Specifically, I am confused about the rules regarding the use of brackets.
What does the use of brackets really mean in F#?
What type of collection/set do brackets enclose? (i.e. array, seq, list, etc.)
Can any expression that returns a collection be used within brackets?
NOTE
I know nothing about F#. So please forgive my ignorance.
These are the used brackets in F#
()
(1, 2) // Tuple (separator is a comma)
() // empty tuple, called unit
[]
[ 1; 2 ] // List
[] // List.empty
[| 1; 2 |] // Array
[||] // Array.empty
{}
seq { yield 1; yield 2 } // Sequence
Seq.empty // empty Sequence, not {}
async { return 1 } // Computation Expressions, e.g. async
type record = // Record type definition
{ Name : string
Age : int
}
<>
type A<'T> = A of 'T
Types can easily be composed
let composition =
async { return
[|
[ ("A",1); ("B",2) ]
[ ("C",3) ]
|]
}
// val composition : Async<(string * int) list []>
Just break it up.
let step1 = [ for i in 1..4 -> i, true ]
let step2 = step1 |> Map.ofSeq
Then you may be able to read the "signatures" of each step:
val step1 : (int * bool) list = [(1, true); (2, true); (3, true); (4, true)]
val step2 : Map<int,bool> = map [(1, true); (2, true); (3, true); (4, true)]
step1 is then a list so the brackets are for list (list expression).
Inside the list there is a tuple (algebraic data type, product type) of int and bool (int * bool) (note the * for 'product').
step2 is a Map, created from a sequence, and a list is also a sequence so that is the reason why that works (or lists supports the sequence interface). ofSeq should be the total giveaway I would guess.
Using Map.ofList would probably make it less confusing, but its equivalent
let step1 = [ for i in 1..4 -> i, true ]
let step2 = step1 |> Map.ofList
val step1 : (int * bool) list = [(1, true); (2, true); (3, true); (4, true)]
val step2 : Map<int,bool> = map [(1, true); (2, true); (3, true); (4, true)]
You may benefit from reading some documentation like:
https://msdn.microsoft.com/en-us/library/dd233230.aspx
Now send me my certificate from this course of yours... ;-)
[ sequence-expression ]
Creates a List
Other types of brackets create different types of collections eg. seq { ... } or \[| sequence-expression |\] for sequences (IEnumerable<T>) or arrays respectively.
Can any expression that returns a collection be used within brackets?
Sort of. There is a whole load of support for creating various collections (eg. yield value) including also merging sub-collections (yield! expression).
Really too much for a quick answer. The links about into the F# Reference will show you the scope of this.

Dart - Values in a Static typed List

Why is this not an error in Dart?
List<String> listA = ['aa', 1, 'cc']; //not compile time error, not run time error??
I am using Dart Editor.
Writing List<String> listA = ['aa', 1, 'cc']; is like writing List<String> listA = <dynamic>['aa', 1, 'cc'];. The runtimeType of ['aa', 1, 'cc'] is List which is compatible with the type annotation List<String> you used.
If you want to define a list of String to have a warning in editor and an error at runtime you should prefer :
List<String> listA = <String>['aa', 'cc'];
// or
final listA = <String>['aa', 'cc'];
To better understand here's some samples :
print((['aa', 1, 'cc']).runtimeType); // List
print((['aa', 'cc']).runtimeType); // List
print((<dynamic>['aa', 'cc']).runtimeType); // List
print((<String>['aa', 'cc']).runtimeType); // List<String>
List<int> l1 = ['aa', 1, 'cc']; // ok
List<int> l2 = ['aa', 'cc']; // ok
List<int> l3 = <dynamic>['aa', 'cc']; // ok
List<int> l4 = <String>['aa', 'cc']; // warning in editor + runtime error

F# Interactive - how to make it print properties of an object, when the latter is enumerable

It's easier to show on example. If I send the following to F# interactive
type Person = {Name : string; Age : int}
let joe = {Name = "Joe"; Age=30}
The output is:
type Person =
{Name: string;
Age: int;}
val joe : Person = {Name = "Joe";
Age = 30;}
Fsi is smart enough to print all the properties.
Sending the following, line-by-line
let l = new List<int>()
l
results in
val l : List<int>
val it : List<int> = seq []
Fsi sees that l implements IEnumerable, and (rightfully) thinks this is what I expect to see. But there are other properties in l, notably Count and Capacity. (That's not that important for lists, but it is for complex types in my case)
How do I make F# interactive print out the properties of an object and ignore it being IEnumerable? (just like for the type Person in the first example)
You can provide a custom printer using fsi.AddPrinter. It takes a function that specifies how to format a specific type - I don't think there is an easy way to do this generically, but you can use non-generic IEnumerable in this case. The following prints the sequence using the default printing and adds Count:
fsi.AddPrinter(fun (en:System.Collections.IEnumerable) ->
let count = Seq.cast<obj> en |> Seq.length
sprintf "%A { Count = %d }" en count
)
For example:
> l;;
val it : List<int> = seq [] { Count = 0 }
> seq { 1 .. 100000 };;
val it : seq<int> = seq [1; 2; 3; 4; ...] { Count = 100000 }
Funnily enough, this also changes printing for strings, but you could avoid that by adding the custom properties only when en value is not of type string.
> "hello";;
val it : string = "hello" { Count = 5 }
If you override the .ToString method, fsi will use that instead. You can also use the
[<StructuredFormatDisplay>] attribute if only want to affect FSI

Resources