-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Support comprehensions with unknown-length iterables #1457
Comments
I think this makes sense for any "dimensionality" of this problem: just precompute the result vector for any task that does not have a
Would first compute result vectors for |
I agree. Just wasn't sure about how much automagic conversion to vectors that would be desirable. |
Perhaps we should just do it for any object for which
|
Stefan had suggested |
And it seems that |
Here's a I struggled with a little. I was trying to put the zlib version into a tuple. It's available as a string, such as "1.2.7" or "1.2.3.4". I wanted this parsed into a tuple so I could easily compare versions, even when the tuples were of different length. Here are a few things I tried or wished existed: julia> # Tuple comprehension (Python-like generator syntax, doesn't exist in julia)
julia> (int(c) for c in each_match(r"\d+", Zlib.zlib_version))
syntax error: missing separator in tuple
julia> tuple(int(c) for c in each_match(r"\d+", Zlib.zlib_version))
syntax error: missing comma or ) in argument list
julia> # List comprehension (requires length of each_match, in this case)
julia> [int(c.match) for c in each_match(r"\d+", Zlib.zlib_version)]
no method length(RegexMatchIterator,)
in method_missing at base.jl:70
in anonymous at no file
julia> # Works, but I can't compare lists properly, and need to know the number of components
julia> ver = [int(c) for c in match(r"(\d+)\.(\d+)\.(\d+)\.?(\d)?", Zlib.zlib_version).captures]
4-element Any Array:
1
2
3
4
julia> ver < [1,2,3,5]
< not defined for arrays. Try .< or isless.
in < at abstractarray.jl:803
julia> ver .< [1,2,3,5]
4-element Bool Array:
false
false
false
true
julia> ver .< [1,2,7]
argument dimensions must match
in promote_shape at operators.jl:148
in .< at array.jl:979
julia> # This one works, but only if I know the exact number of components
julia> tuple([int(c) for c in match(r"(\d+)\.(\d+)\.(\d+)\.?(\d)?", Zlib.zlib_version).captures]...)
(1,2,3,4)
julia> tuple([int(c) for c in match(r"(\d+)\.(\d+)\.(\d+)\.?(\d)?", "1.2.7").captures]...)
no method convert(Type{Int64},Nothing)
in method_missing at base.jl:70
in int at base.jl:75
in anonymous at no file
julia> # Working version (somewhat messy, IMHO)
julia> [int(c.match) for c in [each_match(r"\d+", Zlib.zlib_version)...]]
4-element Any Array:
1
2
3
4
julia> # Version I ended up with
julia> tuple([int(c) for c in split(Zlib.zlib_version, '.')]...)
(1,2,3,4)
julia> # better yet...
julia> tuple(int(split(Zlib.zlib_version, '.'))...)
(1,2,3,4) So, in the end, I didn't need regular expressions, but would have been happiest if the first list comprehension had worked.
julia> function all_matches(r::Regex, s::String)
matches = RegexMatch[]
for m in each_match(r, s)
push(matches, m)
end
return matches
end
julia> [int(c.match) for c in all_matches(r"\d+", Zlib.zlib_version)]
4-element Int64 Array:
1
2
3
4 Might also be nice if this worked: julia> int(all_matches(r"\d+", Zlib.zlib_version))
no method iround(BitsKind,RegexMatch)
in method_missing at base.jl:70
in iround_to at abstractarray.jl:121
in int at abstractarray.jl:138 Perhaps this would be reasonable once Stephan's ideas in #1448 are addressed. |
@kmsquire – this doesn't address all these bumps, which are general issues, but there is a julia> convert(VersionNumber,"1")
v"1.0.0"
julia> convert(VersionNumber,"1.2")
v"1.2.0"
julia> convert(VersionNumber,"1.2.3")
v"1.2.3" Valid version numbers are determined as specified by the semantic versioning standard. They compare as specified by the precedence rules in the standard, which is just as you would want them to. |
Of course, four numbers isn't supported by the standard, so that kind of shoots using that. |
As the error message suggested, |
Thanks for the feedback. I wasn't aware of the VersionNumber type (though I vaguely remember it mentioned, now that you brought it up). Zlib itself only uses 3 numbers, but the Debian/Ubuntu package uses 4. I did miss julia> isless([1,2,3], [1,2,3,4])
true I assumed it was the same as I'm sure I'll get faster at things like this as I become more familiar with the language. I'm most used to Python, where the solution is typically obvious or easy to figure out (though it may not work as efficiently). I'm hoping Julia will get there at some point, and I'm hoping my reports and contributions help it along. Cheers! |
…l comprehensions due to JuliaLang#1457
+1 Ran into this with |
Collect works for this. |
I just came across this issue while refactoring a loop into a list comprehension. Here's what I tried to write: [item for item in @task(x, y)] After getting an error about how there's no method length(Task), I ended up turning the original code: for item in @task f(x, y) display(item) end into the following: convert(Array{ItemType}, collect(@task f(x, y))) (The ItemType conversion is there so that my custom As someone fairly new to the language, the fact that comprehensions only work on some iterables (those that have a defined length) came as a surprise – I had been thinking of comprehensions as something like syntax sugar for map or a loop that collects its results. |
this also uses the new lowering for typed comprehensions, allowing all comprehensions on unknown-length iterables (fixes #1457)
this also uses the new lowering for typed comprehensions, allowing all comprehensions on unknown-length iterables (fixes #1457)
this also uses the new lowering for typed comprehensions, allowing all comprehensions on unknown-length iterables (fixes #1457)
this also uses the new lowering for typed comprehensions, allowing all comprehensions on unknown-length iterables (fixes #1457)
JuliaLang#4867) this also uses the new lowering for typed comprehensions, allowing all comprehensions on unknown-length iterables (fixes JuliaLang#1457)
Currently, there's very little I can do with an iterable that doesn't support
length
, such asFilter
andTask
. Examples:Would it be reasonable to provide a fallback for these cases (when
length
is missing) to e g allocate aVector
and push each item as it is produced? The minimal request would be to get[xs...]
to work in this case, since that at least would allow to circumvent the others with modest effort.One thing to consider is that while iterables with a
length
method usually can be reiterated to produce the same items again, e gTask
does not behave like that.Maybe the fallback should be only for 1d comprehensions, since
would be asking for trouble. (Consumes all output from one task in the first step of the outer loop,
hard to expand a 2d Array) All the more reason to be able to capture the output first, like
If there were a fallback for
[x*y for x in task1, y in task2]
, it should do that implicitly.See also #1454, and discussion at commit 3a0e570.
The text was updated successfully, but these errors were encountered: