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

Inconsistent tuple pattern matching #18887

Closed
evanfields opened this issue Oct 12, 2016 · 3 comments
Closed

Inconsistent tuple pattern matching #18887

evanfields opened this issue Oct 12, 2016 · 3 comments

Comments

@evanfields
Copy link

Julia allows pattern matching on tuples in some cases (iteration) but not others (applying anonymous functions). This isn't a major issue, but it does seem a bit of a design inconsistency and could be confusing to new users:

julia> versioninfo()
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
  System: NT (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, sandybridge)

julia> x = [(1,2),(3,4),(5,6)]
3-element Array{Tuple{Int64,Int64},1}:
 (1,2)
 (3,4)
 (5,6)

julia> for (a,b) in x
           println(b)
       end
2
4
6

julia> [b for (a,b) in x]
3-element Array{Int64,1}:
 2
 4
 6

julia> sum(b for (a,b) in x)
12

julia> map((a,b) -> b, x)
ERROR: MethodError: no method matching (::##125#126)(::Tuple{Int64,Int64})
Closest candidates are:
  #125(::Any, ::Any) at REPL[20]:1
 in _collect(::Array{Tuple{Int64,Int64},1}, ::Base.Generator{Array{Tuple{Int64,Int64},1},##125#126}, ::Base.EltypeUnknown, ::Base.HasShape) at .\array.jl:320
 in map(::Function, ::Array{Tuple{Int64,Int64},1}) at .\abstractarray.jl:1683
@yuyichao
Copy link
Contributor

What's the inconsistency? The function you want to map to takes the same number of arguments as the number of collections you are mapping to.

@JeffBezanson
Copy link
Member

See #6614. The syntax in that case would be map( ((a,b),)->b, x ).

It's vaguely possible to have a language where a function of 1 2-tuple is the same as a function of 2 arguments, but overall I think that leads to worse problems and more confusion.

@KristofferC
Copy link
Member

Seems like there is nothing actionable here.

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

No branches or pull requests

4 participants