# Best practices

**URL:** <https://discourse.julialang.org/t/best-practices/85138>\
**Category:** Numerics\
**Tags:** performance\
**Created:** [August 2, 2022, 3:24am UTC](https://discourse.julialang.org/t/best-practices/85138 "2022-08-02T03:24:08Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [March 24, 2023, 1:49am UTC](https://discourse.julialang.org/t/best-practices/85138/21 "2023-03-24T01:49:51Z")

</div>

`pairs` doesn’t seem to be compatible with `map` the way that `enumerate` is… is there an OffsetArrays-compatible alternative to `enumerate` that is `map` compatible?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 24, 2023, 8:07am UTC](https://discourse.julialang.org/t/best-practices/85138/22 "2023-03-24T08:07:14Z")

</div>

Can you show an example of the incompatibility?

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [March 24, 2023, 12:23pm UTC](https://discourse.julialang.org/t/best-practices/85138/23 "2023-03-24T12:23:53Z")

</div>

`enumerate` can be mapped over:

```julia
julia> map(enumerate(rand(10))) do (i,x)
               (i,x)
       end
10-element Vector{Tuple{Int64, Float64}}:
 (1, 0.3408824363518682)
 (2, 0.052613595813357006)

```

`pairs` can not:

```julia
julia> map(pairs(rand(10))) do (i,x)
               (i,x)
       end
ERROR: map is not defined on dictionaries

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 25, 2023, 8:49am UTC](https://discourse.julialang.org/t/best-practices/85138/24 "2023-03-25T08:49:44Z")

</div>

Perhaps not as straightforward, but one may define

```julia
julia> _pairs(v) = zip(eachindex(v), v)
_pairs (generic function with 1 method)

```

which can be mapped over:

```julia
julia> v = OffsetArray(1:4, 2);

julia> map(_pairs(v)) do x
       x
       end
4-element OffsetArray(::Vector{Tuple{Int64, Int64}}, 3:6) with eltype Tuple{Int64, Int64} with indices 3:6:
 (3, 1)
 (4, 2)
 (5, 3)
 (6, 4)

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 25, 2023, 9:31am UTC](https://discourse.julialang.org/t/best-practices/85138/25 "2023-03-25T09:31:28Z")

</div>

maybe this helps

```julia
 function mapover(ints)
      isodd(length(ints)) && error("input must be of even length")
      xys = Iterators.flatmap(pairs(ints)) do x
          x
      end
      ixs = collect(xys)
      twotuples = ((ixs[i], ixs[i+1]) for i=1:2:length(ixs))
      collect(twotuples)
  end

 ints = rand(1:999, 6);
 result = mapover(ints);

 ints'
 # 1×6 adjoint(::Vector{Int64}) with eltype Int64:
 # 157 11 305 977 626 989

 result
 #= 6-element Vector{Tuple{Int64, Int64}}:
 (1, 157)
 (2, 11)
 (3, 305)
 (4, 977)
 (5, 626)
 (6, 989)
=#

```

[Previous page](https://discourse.julialang.org/t/best-practices/85138.md?page=1)
