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

zero for OneElement #332

Merged
merged 7 commits into from
Jul 9, 2024
Merged

zero for OneElement #332

merged 7 commits into from
Jul 9, 2024

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Dec 22, 2023

With this,

julia> v = OneElement(10, 3, 4)
4-element OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}:
  
  
 10
  

julia> zero(v)
4-element Zeros{Int64}

julia> v + zero(v) isa typeof(v)
true

Copy link

codecov bot commented Dec 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.90%. Comparing base (4bfbeec) to head (af63448).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #332   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files           8        8           
  Lines        1086     1093    +7     
=======================================
+ Hits         1085     1092    +7     
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@longemen3000
Copy link

Seems good to add. But before adding, what is the contract for zero when using with AbstractArray?. before merging, is there any advantage on returning Zeros{T}/Ones{T} instead of OneElement{T} ?

@jishnub
Copy link
Member Author

jishnub commented Mar 4, 2024

The main advantage is that typeof(x + zero(x)) will be the same as typeof(x), in this case a OneElement. The sum of two OneElements isn't a OneElement.

@jishnub jishnub requested a review from dlfivefifty June 7, 2024 06:54
@jishnub
Copy link
Member Author

jishnub commented Jun 7, 2024

I've updated this to return a zero based on the value and not the type. zero(v::OneElement) now tries to preserve the type of the array, which is often assumed elsewhere.

julia> using FillArrays

julia> v = OneElement(10, 3, 4)
4-element OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}:
  
  
 10
  

julia> zero(v)
4-element OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}:
 
 
 0
 

@jishnub
Copy link
Member Author

jishnub commented Jul 9, 2024

Gentle bump

@dlfivefifty dlfivefifty merged commit 1896e30 into master Jul 9, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants