Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote the LaTeX parser to be more flexible #10

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type AliasMap<'Key, 'Value when 'Key : comparison and 'Value : comparison> =
interface System.Collections.IEnumerable with
charlesroddie marked this conversation as resolved.
Show resolved Hide resolved
member this.GetEnumerator() = (AliasMap.toSeq this :> System.Collections.IEnumerable).GetEnumerator()
interface System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'Key, 'Value>> with
member this.GetEnumerator() = match this with | AliasMap (k2v, _) -> (k2v :> System.Collections.Generic.IEnumerable<_>).GetEnumerator()
member this.GetEnumerator() = match this with AliasMap (k2v, _) -> (k2v :> System.Collections.Generic.IEnumerable<_>).GetEnumerator()
interface System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<'Key, 'Value>> with
member this.Count = AliasMap.count this
interface System.Collections.Generic.IReadOnlyDictionary<'Key, 'Value> with
Expand Down Expand Up @@ -92,13 +92,4 @@ module AliasMap =
/// Takes a list of (Primary key you get by indexing with the value, Other keys, Value)
let ofListWithValueMap valueMap list = ofListWtihKeyKeysValueMap id id valueMap list
/// Takes a list of (Primary key you get by indexing with the value, Other keys, Value)
let ofListWithKeyValueMap keyMap valueMap list = ofListWtihKeyKeysValueMap keyMap (List.map keyMap) valueMap list

type INT = INT of int
type L() =
class
member __.Item with get(f : INT) = f
member __.Item with get(f : int) = f
static member IUEWOKJU(this : L) = this.[1]
end

let ofListWithKeyValueMap keyMap valueMap list = ofListWtihKeyKeysValueMap keyMap (List.map keyMap) valueMap list
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<Compile Include="MapBenchmark.fs" />
<Compile Include="AliasMap.fs" />
<Compile Include="BiDictionary.fs" />
<Compile Include="AliasMapBenchmark.fs" />
<Compile Include="Program.fs" />
Expand Down
5 changes: 2 additions & 3 deletions MathDisplay.Tests/UnitTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ type TestClass () =

[<Test>]
member __.``Gather output`` () =
let fff = MathDisplay.DataTypes.List.partitionWhile ((=) 'a') ['a'; 'a'; 'a'; 'a'; 'b'; 'c']
let x =
@"123"
|> LaTeX.toAtom LaTeX.Options.Default
@"\left.\frac\sqrt{23}4\right)"
|> LaTeX.ToAtom LaTeX.Options.Default
(match x with Ok r -> r | Error e -> InfoException e |> raise).ToString() |>
Assert.Pass
48 changes: 48 additions & 0 deletions MathDisplay/DataTypes/AliasDictionary.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace MathDisplay.DataTypes

open System.Collections.Generic

/// Holds a d:Dictionary<'X,'Y>, and an e:Dictionary<'Y,'X> which are quasi-inverses in the sense that
/// for any key x in d, x.[d] is key in 'Y, and d.[e.[y]] = y.
/// d-keys are "Aliases" of e-keys, and a e-key may have many aliases mapping to it (so d may not be injective),
/// but there is a primary alias corresponding to each e-key (so e is injective).
type AliasDictionary<'X, 'Y when 'X : equality and 'Y : equality> private(d:Dictionary<'X,'Y>, e:Dictionary<'Y,'X>) =
new() = AliasDictionary(Dictionary<'X, 'Y>(), Dictionary<'Y, 'X>())
/// Add primary aliases
member __.AddPrimary(primaryAlias:'X, value:'Y) =
d.Add(primaryAlias, value)
e.Add(value, primaryAlias)
/// Add secondary aliases
member __.AddMore(secondaryAliases:seq<'X>, value:'Y) =
for x in secondaryAliases do d.Add(x, value)
member this.Add(primaryAlias:'X, secondaryAliases : seq<'X>, value:'Y) =
d.Add(primaryAlias, value)
e.Add(value, primaryAlias)
this.AddMore(secondaryAliases, value)
/// The first item of aliases is primary.
member this.Add(aliases : 'X list, value:'Y) =
match aliases with
| primaryAlias::secondaryAliases ->
d.Add(primaryAlias, value)
e.Add(value, primaryAlias)
this.AddMore(secondaryAliases, value)
| [] -> ()
member __.TryGetValue key =
match d.TryGetValue key with
| (true, v) -> Some v
| (false, _) -> None
member __.TryGetKey value =
match d.TryGetValue value with
| (true, v) -> Some v
| (false, _) -> None
member private t.D = d
member private d.E = e
/// The first of any 'X list is primary.
new(pairs:('X list * 'Y) list) =
let dict = AliasDictionary<'X, 'Y>()
pairs |> List.iter (fun (keys, value) ->
match keys with
| primaryKey::secondaryKeys ->
dict.Add(primaryKey, secondaryKeys, value)
| [] -> ())
AliasDictionary(dict.D, dict.E)
13 changes: 11 additions & 2 deletions MathDisplay/DataTypes/ListExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ module MathDisplay.DataTypes.List
/// Partition elements of a list using the specified predicate.
/// The result is a tuple containing elements (from the beginning
/// of the list that satisfy the predicate) and the rest of the list.
let inline partitionWhile f =
let partitionWhile (f: 'T -> bool) =
let rec loop acc = function
| x::xs when f x -> loop (x::acc) xs
| xs -> List.rev acc, xs
loop []
loop []

let mapFoldResult f (state:'State) (list: 'T list) : Result<'Result list * 'State, 'Error> =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs XML comment.

let rec loop acc state = function
| [] -> (List.rev acc, state) |> Ok
| x::xs ->
match f state x with
| Ok (value, state) -> loop (value::acc) state xs
| Error e -> Error e
loop [] state list
Loading