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

Avoid iterating seq multiple times in Frame.ofRecords #406

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/Deedle/FrameUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ module internal Reflection =
[| for _, (input, body) in convertors ->
let cast = Expression.Convert(body, typeof<IVector>)
Expression.Lambda<Func<seq<'T>, IVector>>(cast, input).Compile() |]
let data = data |> Array.ofSeq
let frameData =
[| for convFunc in convertors -> convFunc.Invoke(data) |]
|> vectorBuilder.Create
Frame<int, string>(Index.ofKeys [0 .. (Seq.length data) - 1], colIndex, frameData, IndexBuilder.Instance, VectorBuilder.Instance)
Frame<int, string>(Index.ofKeys [0 .. data.Length - 1], colIndex, frameData, IndexBuilder.Instance, VectorBuilder.Instance)

/// Helper that makes it possible to call convertRecordSequence on untyped enumerable
type ConvertRecordHelper =
Expand Down
12 changes: 12 additions & 0 deletions tests/Deedle.Tests/Frame.fs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ let ``Can read simple sequence of records using a specified column as index`` ()
set df.ColumnKeys |> shouldEqual (set ["Volume"; "Price"])
df.RowKeys |> Seq.head |> shouldEqual rows.[0].Date

[<Test>]
let ``Can read simple sequence of records deterministically `` () =
let mutable n = 0
let values =
seq {
yield n, n
n <- n + 1
yield n, n
n <- n + 1 }
let expected = [|0, 0; 1, 1|] |> Frame.ofRecords
values |> Frame.ofRecords |> shouldEqual expected

[<Test>]
let ``Can expand properties of a simple record sequence`` () =
let df = frame [ "MSFT" => Series.ofValues (typedRows ()) ]
Expand Down