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

Add view kwarg to first and last #2951

Merged
merged 7 commits into from
Dec 1, 2021
Merged

Add view kwarg to first and last #2951

merged 7 commits into from
Dec 1, 2021

Conversation

Chandu-4444
Copy link
Contributor

@Chandu-4444 Chandu-4444 commented Nov 30, 2021

Please look at this, I've added view (defaults to false) kwarg to first and last. Also added tests to check for checking types of the results that were returned by the above functions. #2845

@bkamins bkamins added this to the 1.x milestone Nov 30, 2021
@nalimilan
Copy link
Member

It would be good to have an @inferred test if that's doable.

@bkamins
Copy link
Member

bkamins commented Nov 30, 2021

It would be good to have an @inferred test if that's doable.

My experience is that it will be problematic, but we can check :).

@Chandu-4444
Copy link
Contributor Author

I didn't clearly understand the working of @inferred. Is it possible to point a few resources, I tried this https://docs.julialang.org/en/v1/stdlib/Test/#Test.@inferred, but still I wasn't very obvious about this.

@bkamins
Copy link
Member

bkamins commented Nov 30, 2021

@inferred checks if Julia compiler is able to determine the return value of the function.

In our case here the question is if compiler is able to determine that when you do not pass view kwarg the returned value will have DataFrame type.

So just check if @inferred first(df, 6) errors or not. If it errors it is not the end of the world. For example with filter we have:

julia> df = DataFrame(x=1:3)
3×1 DataFrame
 Row │ x
     │ Int64
─────┼───────
   1 │     1
   2 │     2
   3 │     3

julia> @inferred filter(x -> true, df)
3×1 DataFrame
 Row │ x
     │ Int64
─────┼───────
   1 │     1
   2 │     2
   3 │     3

julia> @inferred filter(x -> true, df, view=false)
ERROR: return type DataFrame does not match inferred return type Union{DataFrame, SubDataFrame{DataFrame, DataFrames.Index, T} where T<:AbstractVector{Int64}}

and both results are expected and optimal. By default compiler knows that the returned value will have DataFrame type, however, when you pass view kwarg the return type is a union as at compile time the value of view kwarg cannot be determined.

@Chandu-4444
Copy link
Contributor Author

So just check if @inferred first(df, 6) errors or not.

Yes, that throws an error.

@bkamins
Copy link
Member

bkamins commented Nov 30, 2021

I have pushed changes that should make it @inferred hopefully.

@bkamins
Copy link
Member

bkamins commented Nov 30, 2021

Yes - my fix works.

@bkamins bkamins requested a review from nalimilan November 30, 2021 19:13
Copy link
Member

@bkamins bkamins left a comment

Choose a reason for hiding this comment

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

Looks good (subject to wording of docstrings, which @nalimilan is better at checking than me)

@bkamins bkamins removed this from the 1.x milestone Nov 30, 2021
@bkamins bkamins added this to the 1.3 milestone Nov 30, 2021
Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

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

Thanks!

@bkamins bkamins merged commit b07a8bf into JuliaData:main Dec 1, 2021
@bkamins
Copy link
Member

bkamins commented Dec 1, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants