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 10 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
109 changes: 109 additions & 0 deletions MathDisplay/DataTypes/AliasDictionary.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
namespace MathDisplay.DataTypes

open System.Collections.Generic

type AliasDictionary<'Key, 'Value when 'Key : equality and 'Value : equality> private(k2v, v2k) =
new() = AliasDictionary(Dictionary<'Key, 'Value>(), Dictionary<'Value, 'Key>())
new(valueComparer) = AliasDictionary(Dictionary<_, _>(), Dictionary<_, _>(comparer = valueComparer))

member __.Add(primaryKey, value) =
k2v.Add(primaryKey, value)
v2k.Add(value, primaryKey)
member this.Add(primaryKey, key1, value) =
this.Add(primaryKey, value)
k2v.Add(key1, value)
member this.Add(primaryKey, key1 : 'Key, key2, value) =
this.Add(primaryKey, key1, value)
k2v.Add(key2, value)
member this.Add(primaryKey, key1, key2, key3, value) =
charlesroddie marked this conversation as resolved.
Show resolved Hide resolved
this.Add(primaryKey, key1, key2, value)
k2v.Add(key3, value)
member this.Add(primaryKey, keys : seq<_>, value) =
k2v.Add(primaryKey, value)
v2k.Add(value, primaryKey)
this.AddMoreKeys keys value
member __.AddMoreKeys keys value = for key in keys do k2v.Add(key, value)
member __.Contains key value =
match k2v.TryGetValue key with
| true, value' -> value = value'
| false, _ -> false
member __.ContainsKey key = k2v.ContainsKey key
member __.ContainsValue value = v2k.ContainsKey value
member __.CopyTo array arrayIndex = (k2v :> ICollection<_>).CopyTo(array, arrayIndex)
member __.Count = k2v.Count
member __.Clear() = k2v.Clear(); v2k.Clear()
member __.Keys = k2v.Keys
member __.Values = k2v.Values
member __.TryGetValue(key, value : byref<_>) = k2v.TryGetValue(key, &value)
member __.TryGetKey(value, key : byref<_>) = v2k.TryGetValue(value, &key)
member __.TryGetValueFSharp key = k2v.TryGetValue key
charlesroddie marked this conversation as resolved.
Show resolved Hide resolved
member __.TryGetKeyFSharp value = v2k.TryGetValue value
member __.Item with get key = k2v.[key] and set key value = k2v.[key] <- value; v2k.[value] <- key
member __.Item with get value = v2k.[value] and set value key = k2v.[key] <- value; v2k.[value] <- key
charlesroddie marked this conversation as resolved.
Show resolved Hide resolved
member __.Remove key value =
match [ for pair in k2v do if value = pair.Value then yield pair ] with
| [] -> false
| [KeyValue (key', _)] ->
if key = key' then
k2v.Remove(key) |> ignore
v2k.Remove(value) |> ignore
true
else false
| pairs ->
if List.forall (fun (KeyValue (key', _)) -> key <> key') pairs then false
else
k2v.Remove(key) |> ignore
if v2k.[value] = key then
v2k.[value] <- (List.find (fun (KeyValue (key', _)) -> key <> key') pairs).Key
true
member this.RemoveKey key = this.Remove key k2v.[key]
member __.RemoveValue value =
if v2k.Remove value then
for KeyValue (key, value') in k2v do
if value = value' then k2v.Remove(key) |> ignore
true
else false
member __.GetEnumerator() = k2v.GetEnumerator()
interface System.Collections.IEnumerable with
member this.GetEnumerator() = this.GetEnumerator() :> System.Collections.IEnumerator
interface IEnumerable<KeyValuePair<'Key, 'Value>> with
member this.GetEnumerator() = this.GetEnumerator() :> IEnumerator<_>
interface IReadOnlyCollection<KeyValuePair<'Key, 'Value>> with
member this.Count = this.Count
interface IReadOnlyDictionary<'Key, 'Value> with
member this.ContainsKey key = this.ContainsKey key
member this.Item with get(key) = this.[key]
member this.Keys = this.Keys :> seq<_>
member this.Values = this.Values :> seq<_>
member this.TryGetValue(key, value) = this.TryGetValue(key, &value)
interface ICollection<KeyValuePair<'Key, 'Value>> with
member this.Add (KeyValue (key, value)) = this.Add(key, value)
member this.Clear() = this.Clear()
member this.Contains (KeyValue (key, value)) = this.Contains key value
member this.CopyTo(array, arrayIndex) = this.CopyTo array arrayIndex
member this.Count = this.Count
member __.IsReadOnly = false
member this.Remove (KeyValue (key, value)) = this.Remove key value
interface IDictionary<'Key, 'Value> with
member this.Add(key, value) = this.Add(key, value)
member this.ContainsKey key = this.ContainsKey key
member this.Item with get key = this.[key] and set key value = this.[key] <- value
member this.Keys = this.Keys :> ICollection<_>
member this.Remove key = this.RemoveKey key
member this.TryGetValue(key, value : byref<_>) = this.TryGetValue(key, &value)
member this.Values = this.Values :> ICollection<_>

[<AutoOpen>]
module internal AliasDictionary =
let aliasDict pairs =
let dict = AliasDictionary<_, _>()
for primaryKey, keys, value in pairs do dict.Add(primaryKey, keys = keys, value = value)
dict
let aliasDictValueMap map pairs =
let dict = AliasDictionary<_, _>()
for primaryKey, keys, value in pairs do dict.Add(primaryKey, keys = keys, value = map value)
dict
let aliasDictValueComparer valueComparer pairs =
let dict = AliasDictionary<_, _>(valueComparer)
for primaryKey, keys, value in pairs do dict.Add(primaryKey, keys = keys, value = value)
dict
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