Skip to content

Instantly share code, notes, and snippets.

@ohlawdie
Last active February 11, 2020 22:40
Show Gist options
  • Save ohlawdie/dd425dbb87acfb1ef204fe02940fb208 to your computer and use it in GitHub Desktop.
Save ohlawdie/dd425dbb87acfb1ef204fe02940fb208 to your computer and use it in GitHub Desktop.
C8 Indices and Ranges #cs2fs
namespace global
open System
open System.Linq
namespace ExploreCsharpEight
type IndicesAndRanges() =
member val private words = [|"The"; "quick"; "brown"; "fox"; "jumped"; "over"; "the"; "lazy"; "dog"|] with get, set
member this.Syntax_LastIndex() =
Console.WriteLine (sprintf "The last word is %O" this.words.[(int (* ERROR UnknownPrefixOperator "^" *))])
0
member this.Syntax_Range() =
let mutable quickBrownFox = this.words.[(int (* ERROR UnknownNode *))]
for word in quickBrownFox do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
0
member this.Syntax_LastRange() =
let mutable lazyDog = this.words.[(int (* ERROR UnknownNode *))]
for word in lazyDog do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
0
member this.Syntax_PartialRange() =
let mutable allWords = this.words.[(int (* ERROR UnknownNode *))]
let mutable firstPhrase = this.words.[(int (* ERROR UnknownNode *))]
let mutable lastPhrase = this.words.[(int (* ERROR UnknownNode *))]
for word in allWords do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
for word in firstPhrase do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
for word in lastPhrase do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
0
member this.Syntax_IndexRangeType() =
let mutable (the : Index) = (* ERROR UnknownPrefixOperator "^" *)
Console.WriteLine (this.words.[(int the)])
let mutable (phrase : Range) = (* ERROR UnknownNode *)
let mutable text = this.words.[(int phrase)]
for word in text do
Console.Write (sprintf "< %O >" word)
Console.WriteLine ()
0
member this.Syntax_WhyChosenSemantics() =
let mutable numbers = (Enumerable.Range (0, 100)).ToArray ()
let mutable (x : int) = 12
let mutable (y : int) = 25
let mutable (z : int) = 36
Console.WriteLine ("===================== ^0 is the same as Length. =>")
Console.WriteLine ((sprintf "%O is the same as %O" numbers.[(* ERROR UnknownPrefixOperator "^" *)]) numbers.[(numbers.Length - x)])
Console.WriteLine ((sprintf "%O is the same as %O" numbers.[(* ERROR UnknownNode *)].Length) (y - x))
Console.WriteLine ()
Console.WriteLine ("===================== Consecutive disjoint sequences. =>")
Console.WriteLine ("numbers[x..y] and numbers[y..z] are consecutive and disjoint:")
let mutable (x_y : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
let mutable (y_z : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
Console.WriteLine ((((sprintf "\tnumbers[x..y] is %O through %O, numbers[y..z] is %O through %O" x_y.[0]) x_y.[(* ERROR UnknownPrefixOperator "^" *)]) y_z.[0]) y_z.[(* ERROR UnknownPrefixOperator "^" *)])
Console.WriteLine ()
Console.WriteLine ("===================== Remove elements from both ends. =>")
Console.WriteLine ("numbers[x..^x] removes x elements at each end:")
let mutable (x_x : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
Console.WriteLine ((sprintf "\tnumbers[x..^x] starts with %O and ends with %O" x_x.[0]) x_x.[(* ERROR UnknownPrefixOperator "^" *)])
Console.WriteLine ()
Console.WriteLine ("===================== Incomplete sequences imply 0, ^0 =>")
Console.WriteLine ("numbers[..x] means numbers[0..x] and numbers[x..] means numbers[x..^0]")
let mutable (start_x : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
let mutable (zero_x : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
Console.WriteLine ((((sprintf "\t%O..%O is the same as %O..%O" start_x.[0]) start_x.[(* ERROR UnknownPrefixOperator "^" *)]) zero_x.[0]) zero_x.[(* ERROR UnknownPrefixOperator "^" *)])
let mutable (z_end : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
let mutable (z_zero : Span<int>) = numbers.[(* ERROR UnknownNode *)] :> Span<Int32>
Console.WriteLine ((((sprintf "\t%O..%O is the same as %O..%O" z_end.[0]) z_end.[(* ERROR UnknownPrefixOperator "^" *)]) z_zero.[0]) z_zero.[(* ERROR UnknownPrefixOperator "^" *)])
Console.WriteLine ()
0
member this.ComputeMovingAverages() =
let mutable (sequence : int[]) = this.Sequence (1000)
do
let mutable (start : int) = 0
while (start < sequence.Length) do
let mutable (r : Range) = (* ERROR UnknownNode *)
(* ERROR UnknownNode *) <- MovingAverage (sequence, r)
Console.WriteLine (((((sprintf "From %O to %O: \tMin: %O,\tMax: %O,\tAverage: %O" r.Start) r.End) min) max) average)
start <- start + 100
do
let mutable (start : int) = 0
while (start < sequence.Length) do
let mutable (r : Range) = (* ERROR UnknownNode *)
(* ERROR UnknownNode *) <- MovingAverage (sequence, r)
Console.WriteLine (((((sprintf "From %O to %O: \tMin: %O,\tMax: %O,\tAverage: %O" r.Start) r.End) min) max) average)
start <- start + 100
(* ERROR UnknownNode *)
0
member private this.Sequence(count : int) =
((Enumerable.Range (0, count)).Select (fun x -> int (Math.Sqrt (x) * 100))).ToArray ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment