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 each-function #79

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Add each-function #79

wants to merge 7 commits into from

Conversation

gustaphe
Copy link
Contributor

Add function each(ElementType, collection) that returns an iterable with eltype<:ElementType, even if collection is an element, itself iterable, with another eltype. This makes it easier to write functions with arguments ::Union{ String, Array{String} } and similar.

@codecov-commenter
Copy link

codecov-commenter commented Jul 27, 2020

Codecov Report

Merging #79 into master will increase coverage by 3.26%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #79      +/-   ##
==========================================
+ Coverage   73.85%   77.11%   +3.26%     
==========================================
  Files           1        1              
  Lines         283      284       +1     
==========================================
+ Hits          209      219      +10     
+ Misses         74       65       -9     
Impacted Files Coverage Δ
src/IterTools.jl 77.11% <100.00%> (+3.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 21fe124...32efa26. Read the comment docs.

@omus
Copy link
Contributor

omus commented Jul 27, 2020

This reminds me of:

help?> collect(::Type, ::Any)
  collect(element_type, collection)

  Return an Array with the given element type of all items in a collection or iterable. The result has the same shape and number of dimensions as collection.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> collect(Float64, 1:2:5)
  3-element Array{Float64,1}:
   1.0
   3.0
   5.0

Maybe this PR should do a convert?

@gustaphe
Copy link
Contributor Author

It could be done, pretty easilly, but I'm not sure I agree that it should. I imagine it would add a bit of overhead, and it won't (without bigger changes) do what you expect in some cases:

each(Tuple{Vararg{Float32,Float32}},(1.0,1.0))

will simply not recognize (1.0,1.0) as a Tuple{Float32,Float32}, and so will try to convert each Float64 to a Tuple and fail. Since these collections with iterable elements are the typical use case for these functions, I'm not sure it's a good idea to make this do convert.() except when it should be used.

@gustaphe
Copy link
Contributor Author

Okay, this is actually pretty good. I don't know about the performance impact, but the code ended up being pretty clean. Is it idiomatic to use try-catch like this in Julia?

@gustaphe
Copy link
Contributor Author

gustaphe commented Aug 3, 2020

The failed codecov lines are ones where my editor accidentally corrected a pair of tabs to space indentation in code that isn't mine. I didn't know it would make a difference, and since it was consistent with the rest of the file I let it be. Sorry for my bad git discipline.

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

Successfully merging this pull request may close these issues.

None yet

3 participants