-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C# doesn't support Tail Recursion Optimization. So time to bring out F#.
- Loading branch information
1 parent
6c177ec
commit 3bfae1c
Showing
7 changed files
with
79 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace TelnetNegotiationCore.Functional | ||
|
||
open System.Text | ||
|
||
type Trigger = | ||
| NULL = 0uy | ||
| MSDP_VAR = 1uy | ||
| MSDP_VAL = 2uy | ||
| MSDP_TABLE_OPEN = 3uy | ||
| MSDP_TABLE_CLOSE = 4uy | ||
| MSDP_ARRAY_OPEN = 5uy | ||
| MSDP_ARRAY_CLOSE = 6uy | ||
|
||
module MSDPLibrary = | ||
let rec MSDPScan (root: obj, array: seq<byte>, type_: Trigger, encoding : Encoding) = | ||
if Seq.length(array) = 0 then root | ||
else | ||
match type_ with | ||
| Trigger.MSDP_VAR -> | ||
(root :?> Map<string, obj>).Add(encoding.GetString(array |> Seq.takeWhile(fun x -> x <> byte Trigger.MSDP_VAL) |> Array.ofSeq), MSDPScan(root, array |> Seq.skipWhile(fun x -> x <> byte Trigger.MSDP_VAL) |> Seq.skip(1), Trigger.MSDP_VAL,encoding)) | ||
| Trigger.MSDP_VAL -> | ||
let nextType = | ||
try | ||
array |> Seq.find(fun x -> | ||
x = byte Trigger.MSDP_ARRAY_OPEN || | ||
x = byte Trigger.MSDP_TABLE_OPEN || | ||
x = byte Trigger.MSDP_ARRAY_CLOSE || | ||
x = byte Trigger.MSDP_TABLE_CLOSE || | ||
x = byte Trigger.MSDP_VAL) | ||
with | ||
| :? System.Collections.Generic.KeyNotFoundException -> 0uy | ||
|
||
match LanguagePrimitives.EnumOfValue nextType : Trigger with | ||
| Trigger.NULL -> | ||
encoding.GetString(array |> Array.ofSeq) | ||
| Trigger.MSDP_VAL -> | ||
MSDPScan((root :?> List<obj>) @ [encoding.GetString(array |> Seq.takeWhile(fun x -> x <> nextType) |> Array.ofSeq)], array |> Seq.skipWhile(fun x -> x <> byte Trigger.MSDP_VAL) |> Seq.skip(1), Trigger.MSDP_VAL, encoding) | ||
| Trigger.MSDP_TABLE_CLOSE -> | ||
(root :?> List<obj>) @ [encoding.GetString(array |> Seq.takeWhile(fun x -> x <> nextType) |> Array.ofSeq)] | ||
| Trigger.MSDP_ARRAY_OPEN -> | ||
MSDPScan(root, array |> Seq.skipWhile(fun x -> x <> byte Trigger.MSDP_ARRAY_OPEN) |> Seq.skip(1), Trigger.MSDP_ARRAY_OPEN,encoding) | ||
| Trigger.MSDP_TABLE_OPEN -> | ||
MSDPScan(root, array |> Seq.skipWhile(fun x -> x <> byte Trigger.MSDP_TABLE_OPEN) |> Seq.skip(1), Trigger.MSDP_TABLE_OPEN,encoding) | ||
| _ -> root | ||
| Trigger.MSDP_ARRAY_OPEN -> | ||
MSDPScan(List.Empty, array |> Seq.skip(1), Trigger.MSDP_VAL, encoding) | ||
| Trigger.MSDP_TABLE_OPEN -> | ||
MSDPScan(Map<string, obj> [], array |> Seq.skip(1), Trigger.MSDP_VAR, encoding) | ||
| _ -> failwith "Failure in MSDPScan." |
12 changes: 12 additions & 0 deletions
12
TelnetNegotiationCore.Functional/TelnetNegotiationCore.Functional.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="MSDPLibrary.fs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters