Skip to content

Commit 4db5cd7

Browse files
authored
InvalidOperationException F# snippets (#7773)
1 parent 8606ed4 commit 4db5cd7

File tree

17 files changed

+383
-2
lines changed

17 files changed

+383
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Enumerable1
2+
3+
// <Snippet6>
4+
open System
5+
open System.Linq
6+
7+
let data = [| 1; 2; 3; 4 |]
8+
let average =
9+
data.Where(fun num -> num > 4).Average();
10+
printfn $"The average of numbers greater than 4 is {average}"
11+
// The example displays the following output:
12+
// Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
13+
// at System.Linq.Enumerable.Average(IEnumerable`1 source)
14+
// at <StartupCode$fs>.main()
15+
// </Snippet6>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Enumerable2
2+
3+
// <Snippet7>
4+
open System
5+
open System.Linq
6+
7+
let dbQueryResults = [| 1; 2; 3; 4 |]
8+
let moreThan4 =
9+
dbQueryResults.Where(fun num -> num > 4)
10+
11+
if moreThan4.Any() then
12+
printfn $"Average value of numbers greater than 4: {moreThan4.Average()}:"
13+
else
14+
// handle empty collection
15+
printfn "The dataset has no values greater than 4."
16+
17+
// The example displays the following output:
18+
// The dataset has no values greater than 4.
19+
// </Snippet7>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Enumerable3
2+
3+
// <Snippet8>
4+
open System
5+
open System.Linq
6+
7+
let dbQueryResults = [| 1; 2; 3; 4 |]
8+
9+
let firstNum = dbQueryResults.First(fun n -> n > 4)
10+
11+
printfn $"The first value greater than 4 is {firstNum}"
12+
13+
// The example displays the following output:
14+
// Unhandled Exception: System.InvalidOperationException:
15+
// Sequence contains no matching element
16+
// at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
17+
// at <StartupCode$fs>.main()
18+
// </Snippet8>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Enumerable4
2+
3+
// <Snippet9>
4+
open System
5+
open System.Linq
6+
7+
let dbQueryResults = [| 1; 2; 3; 4 |]
8+
9+
let firstNum = dbQueryResults.FirstOrDefault(fun n -> n > 4)
10+
11+
if firstNum = 0 then
12+
printfn "No value is greater than 4."
13+
else
14+
printfn $"The first value greater than 4 is {firstNum}"
15+
16+
// The example displays the following output:
17+
// No value is greater than 4.
18+
// </Snippet9>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Enumerable5
2+
3+
// <Snippet10>
4+
open System
5+
open System.Linq
6+
7+
let dbQueryResults = [| 1; 2; 3; 4 |]
8+
9+
let singleObject = dbQueryResults.Single(fun value -> value > 4)
10+
11+
// Display results.
12+
printfn $"{singleObject} is the only value greater than 4"
13+
14+
// The example displays the following output:
15+
// Unhandled Exception: System.InvalidOperationException:
16+
// Sequence contains no matching element
17+
// at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
18+
// at <StartupCode$fs>.main()
19+
// </Snippet10>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Enumerable6
2+
3+
// <Snippet11>
4+
open System
5+
open System.Linq
6+
7+
let dbQueryResults = [| 1; 2; 3; 4 |]
8+
9+
let singleObject = dbQueryResults.SingleOrDefault(fun value -> value > 2)
10+
11+
if singleObject <> 0 then
12+
printfn $"{singleObject} is the only value greater than 2"
13+
else
14+
// Handle an empty collection.
15+
printfn "No value is greater than 2"
16+
17+
// The example displays the following output:
18+
// Unhandled Exception: System.InvalidOperationException:
19+
// Sequence contains more than one matching element
20+
// at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
21+
// at <StartupCode$fs>.main()
22+
// </Snippet11>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Iterating1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
7+
for number in numbers do
8+
let square = Math.Pow(number, 2) |> int
9+
printfn $"{number}^{square}"
10+
printfn $"Adding {square} to the collection...\n"
11+
numbers.Add square
12+
13+
// The example displays the following output:
14+
// 1^1
15+
// Adding 1 to the collection...
16+
//
17+
//
18+
// Unhandled Exception: System.InvalidOperationException: Collection was modified
19+
// enumeration operation may not execute.
20+
// at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
21+
// at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
22+
// at <StartupCode$fs>.main()
23+
// </Snippet1>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Iterating2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Collections.Generic
6+
7+
let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
8+
9+
let upperBound = numbers.Count - 1
10+
for i = 0 to upperBound do
11+
let square = Math.Pow(numbers[i], 2) |> int
12+
printfn $"{numbers[i]}^{square}"
13+
printfn $"Adding {square} to the collection...\n"
14+
numbers.Add square
15+
16+
printfn "Elements now in the collection: "
17+
for number in numbers do
18+
printf $"{number} "
19+
// The example displays the following output:
20+
// 1^1
21+
// Adding 1 to the collection...
22+
//
23+
// 2^4
24+
// Adding 4 to the collection...
25+
//
26+
// 3^9
27+
// Adding 9 to the collection...
28+
//
29+
// 4^16
30+
// Adding 16 to the collection...
31+
//
32+
// 5^25
33+
// Adding 25 to the collection...
34+
//
35+
// Elements now in the collection:
36+
// 1 2 3 4 5 1 4 9 16 25
37+
// </Snippet2>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Iterating3
2+
3+
// <Snippet3>
4+
open System
5+
open System.Collections.Generic
6+
7+
let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
8+
let temp = ResizeArray()
9+
10+
// Square each number and store it in a temporary collection.
11+
for number in numbers do
12+
let square = Math.Pow(number, 2) |> int
13+
temp.Add square
14+
15+
// Combine the numbers into a single array.
16+
let combined = Array.zeroCreate<int> (numbers.Count + temp.Count)
17+
Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count)
18+
Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count)
19+
20+
// Iterate the array.
21+
for value in combined do
22+
printf $"{value} "
23+
// The example displays the following output:
24+
// 1 2 3 4 5 1 4 9 16 25
25+
// </Snippet3>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module List_Sort1
2+
3+
// <Snippet12>
4+
type Person(firstName: string, lastName: string) =
5+
member val FirstName = firstName with get, set
6+
member val LastName = lastName with get, set
7+
8+
let people = ResizeArray()
9+
10+
people.Add(Person("John", "Doe"))
11+
people.Add(Person("Jane", "Doe"))
12+
people.Sort()
13+
for person in people do
14+
printfn $"{person.FirstName} {person.LastName}"
15+
// The example displays the following output:
16+
// Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. --->
17+
// System.ArgumentException: At least one object must implement IComparable.
18+
// at System.Collections.Comparer.Compare(Object a, Object b)
19+
// at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
20+
// at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
21+
// at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
22+
// --- End of inner exception stack trace ---
23+
// at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
24+
// at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
25+
// at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
26+
// at <StartupCode$fs>.main()
27+
// </Snippet12>

0 commit comments

Comments
 (0)