# Using zip() with count()

**URL:** <https://discourse.julialang.org/t/using-zip-with-count/29549>\
**Category:** New to Julia\
**Created:** [October 6, 2019, 9:11am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549 "2019-10-06T09:11:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![j.copsey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j.copsey/32/18234_2.png) [@j.copsey](https://discourse.julialang.org/u/j.copsey)\
**Post date:** [October 6, 2019, 9:11am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/1 "2019-10-06T09:11:30Z")

</div>

I’m trying to use `count` where the iterator is taken from `zip`.

Here is the code I’ve written:

```julia
function hamming(c, d)
    count((x,y) -> (x != y), zip(c,d))
end

```

However, when this is run on any non-empty collections, e.g. `hamming("123","1X3")`, an exception is thrown:

> **ERROR:** MethodError: no method matching (::getfield(Main, Symbol(“##9#10”)))(::Tuple{Char,Char})
> 
> Closest candidates are:
> 
> #9(::Any, ::Any) at REPL[71]:2
> 
> Stacktrace:
> 
> [1] **count(** ::getfield(Main, Symbol(“##9#10”)), ::Base.Iterators.Zip{Tuple{String,String}} **)** at **./reduce.jl:772**
> 
> [2] **hamming(** ::String, ::String **)** at **./REPL[71]:2**
> 
> [3] top-level scope at **REPL[72]:1**

I can’t see any obvious reason this wouldn’t work [based on the documentation for `count()`.](https://docs.julialang.org/en/v1/base/collections/#Base.count)

There is also no problem when `count` is replaced by an equivalent `for` loop, something like:

```julia
for (x,y) in zip("123","1X3")
    print(x == y ? 0 : 1)
end

```

Can someone help me out? What have I misunderstood?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 6, 2019, 9:27am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/2 "2019-10-06T09:27:12Z")

</div>

FYI:  
[https://github.com/JuliaStats/Distances.jl/blob/7f3a28c0d1372e3b3edbcbc28f00ba5645e1bbdb/src/metrics.jl#L332](https://github.com/JuliaStats/Distances.jl/blob/7f3a28c0d1372e3b3edbcbc28f00ba5645e1bbdb/src/metrics.jl#L332)

* * *

Regarding your question:  
I noticed the following would work:

```julia
julia> count(x -> (x[1] == x[2]), zip("1X3","123"))
2

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 6, 2019, 9:27am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/3 "2019-10-06T09:27:19Z")

</div>

The result of zip is a tuple. How about this?

```julia
function hamming(c, d)
    count(t -> (t[1] == t[2]), zip(c,d))
end

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 6, 2019, 9:27am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/4 "2019-10-06T09:27:22Z")

</div>

`count( ( (x,y)::Tuple{Any,Any} )->(x==y),zip("123","1X3"))`

I think

```julia
(x,y) -> (x == y)

```

is interpreted as a function with two parameters x::Any and y::Any and not as a single Tuple parameter

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 6, 2019, 9:37am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/5 "2019-10-06T09:37:50Z")

</div>

As others have pointed out, `zip` produces `Tuple`s, as a single argument. You can also do

```julia
hamming(c, d) = count(Base.splat(==), zip(c,d))

```

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [October 6, 2019, 9:47am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/6 "2019-10-06T09:47:21Z")

</div>

> [@Tamas\_Papp](#):
>
> ```julia
> hamming(c, d) = count(Base.splat(==), zip(c,d))
> 
> ```

Very nice!

As an alternative, similar to oheil’s solution but imho more canonical, I’d suggest using an anonymous function with _one_ argument used with _tuple destructuring_.  
So you can write:

```julia
hamming(c,d) = count( ((x,y),) -> x == y, zip(c,d))

```

where we have a anonymous function `((x,y),) -> x == y` which has _one_ argument (note that the one-argument tuple is `(a,)` not `(a)` - in the latter case the parens are interpreted as showing precedence which doesn’t make sense here.  
This one argument is then destructured as a tuple of two elements.

This works in general and doesn’t rely on unexported Base functions.

---

<div class="post-metadata">

**Author:** ![j.copsey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j.copsey/32/18234_2.png) [@j.copsey](https://discourse.julialang.org/u/j.copsey)\
**Post date:** [October 6, 2019, 11:45am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/7 "2019-10-06T11:45:24Z")

</div>

Perfect, all makes sense - thanks everyone!

It didn’t occur to me that `(x,y) -> x == y` would be interpreted as a function of two parameters, whereas I was aiming for a function of one (tuple) parameter.

---

<div class="post-metadata">

**Author:** ![j.copsey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j.copsey/32/18234_2.png) [@j.copsey](https://discourse.julialang.org/u/j.copsey)\
**Post date:** [October 6, 2019, 11:55am UTC](https://discourse.julialang.org/t/using-zip-with-count/29549/8 "2019-10-06T11:55:18Z")

</div>

I also just noticed I supplied the wrong condition in the first post for the Hamming distance.

Original: `count(x -> x[1] == x[2], zip(s1,s2))`  
Fixed: `count(x -> x[1] != x[2], zip(s1,s2))`

Apologies to everyone whose replies are now slightly wrong.
