diff --git a/SliceMap.Tests/Program.fs b/SliceMap.Tests/Program.fs index 0695f84..7954422 100644 --- a/SliceMap.Tests/Program.fs +++ b/SliceMap.Tests/Program.fs @@ -1 +1,18 @@ -module Program = let [] main _ = 0 +open SliceMap +open Xunit + +module Program = + let [] main _ = + #if FABLE_COMPILER + // putting a sample here + let x = SMap.ofList [for i in 1..5 -> i, i] + let isDivisibleBy2 x = x % 2 = 0 + let ok = x.[Where isDivisibleBy2] = SMap [(2, 2); (4, 4)] + if not ok then + failwith $"%A{x.[Where isDivisibleBy2]}\nexpected\n%A{SMap [(2, 2); (4, 4)]}" + else + printfn "ok!" + // python: fails on TryFind.toMap due to Comparer + printfn $"{x.[Where isDivisibleBy2]} looks ok :)" + #endif + 0 diff --git a/SliceMap/SliceSet.fs b/SliceMap/SliceSet.fs index 32ecdcb..8a7a528 100644 --- a/SliceMap/SliceSet.fs +++ b/SliceMap/SliceSet.fs @@ -23,6 +23,31 @@ type RankResult = | Closest of int | Empty +#if FABLE_COMPILER +type Memory<'T>(data: 'T array, offset: int, length: int) = + let length = length + member x.Span = + if offset = 0 && length = data.Length then + data + else + // very very bad, this copies... + data[offset .. length - 1] + + member x.Length = length + //#if FABLE_COMPILER_PYTHON + + //#else + + //#endif + member x.Slice(startIndex) = Memory(data, offset + startIndex, length - startIndex) + member x.Slice(startIndex, newLength) = Memory(data, offset + startIndex, newLength) +[] +module internal Extensions = + type 'T ``[]`` with + member x.AsMemory() = Memory(x, 0, x.Length) + member x.AsMemory(startIndex, length) = Memory(x, startIndex, length) +#endif + [] type SliceSet<[]'T when 'T : comparison>(comparer:IComparer<'T>, values:Memory<'T>) = let comparer = comparer @@ -74,7 +99,7 @@ type SliceSet<[]'T when 'T : comparison>(comparer:ICompar new(values:seq<'T>) = let comparer = LanguagePrimitives.FastGenericComparer<'T> let v = values |> Seq.distinct |> Seq.toArray |> Array.sort - SliceSet(comparer, v.AsMemory<'T>()) + SliceSet(comparer, v.AsMemory()) member _.Item with get (idx) =