# Question on lazy map function

**URL:** <https://discourse.julialang.org/t/question-on-lazy-map-function/53450>\
**Category:** New to Julia\
**Tags:** function, lazy-evaluation\
**Created:** [January 16, 2021, 8:03pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450 "2021-01-16T20:03:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)\
**Post date:** [January 16, 2021, 8:03pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/1 "2021-01-16T20:03:28Z")

</div>

Can anyone please point me to overview of using (maybe different kinds) of map/reduce/filter functions in Julia (and it’s standard packages)?

Here is is what I’m missing now: I want `any(map(x -> x > 0, big_rand_array))` to lazy evaluate without computing on all elements, yet it appears to allocates big resulting array of `map` and only then passes it to `any`.

(this is “first steps” question)  
Thank you very much,

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [January 16, 2021, 9:19pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/2 "2021-01-16T21:19:34Z")

</div>

Hi there,

you may be interested in this thread [Best Data Manipulation packages 2020-09 [video]](https://discourse.julialang.org/t/best-data-manipulation-packages-2020-09-video/46690)

Lazy.jl can give you lazy evaluation: [https://github.com/MikeInnes/Lazy.jl](https://github.com/MikeInnes/Lazy.jl)

And then there is Transducers.jl and the things that use it, not lazy AFAIK but pretty cool: [GitHub - JuliaFolds/Transducers.jl: Efficient transducers for Julia](https://github.com/JuliaFolds/Transducers.jl)

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [January 16, 2021, 9:29pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/3 "2021-01-16T21:29:21Z")

</div>

One option is to use Iterators ([Iteration utilities · The Julia Language](https://docs.julialang.org/en/v1/base/iterators/))

```julia
a = Iterators.filter(x -> x > 0, big_rand_array)
b = Iterators.take(a, 1)
c = length(collect(b)) > 0

```

Will lazily iterate over your list and return `true` if any matches are found.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 16, 2021, 9:30pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/4 "2021-01-16T21:30:56Z")

</div>

You can use a generator, like

```julia
any((x > 0 for x in -5:5)) # true

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 16, 2021, 9:45pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/5 "2021-01-16T21:45:01Z")

</div>

Many of these reduction functions can take a function as the first argument: `any(>(0), randn(2))` ought to be efficient. There is a lazy map, but here you may as well combine them: `any(f, Iterators.map(g, xs)) == any(f∘g, xs)`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 16, 2021, 10:01pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/6 "2021-01-16T22:01:41Z")

</div>

@ericphanson, why not single parentheses?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [January 16, 2021, 10:35pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/7 "2021-01-16T22:35:16Z")

</div>

> [@mcabbott](#):
>
> There is a lazy map

In Julia v1.5, `Iterators.map` does not appear to be lazy:

```julia
julia> Iterators.map(x -> 2x, Iterators.repeated(1, 3))
3-element Array{Int64,1}:
 2
 2
 2

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 16, 2021, 10:41pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/8 "2021-01-16T22:41:56Z")

</div>

Oh I forgot which version, again, sorry, it’s in 1.6 then. It is however identical to writing the generator:

```julia
julia> Iterators.map(sqrt, 1:10)
Base.Generator{UnitRange{Int64}, typeof(sqrt)}(sqrt, 1:10)

julia> (sqrt(x) for x in 1:10)
Base.Generator{UnitRange{Int64}, typeof(sqrt)}(sqrt, 1:10)

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 16, 2021, 11:28pm UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/9 "2021-01-16T23:28:06Z")

</div>

True, forgot you don’t need two sets there

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 17, 2021, 12:55am UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/10 "2021-01-17T00:55:06Z")

</div>

> [@FPGro](#):
>
> And then there is Transducers.jl and the things that use it, not lazy AFAIK but pretty cool: [GitHub - JuliaFolds/Transducers.jl: Efficient transducers for Julia](https://github.com/JuliaFolds/Transducers.jl)

Transducers are lazy.

```julia
julia> using Transducers

julia> Iterators.repeated(1, 3) |> Map(x -> 2x)
Base.Iterators.Take{Base.Iterators.Repeated{Int64}} |>
    Map(Main.λ❓)

julia> Iterators.repeated(1, 3) |> Map(x -> 2x) |> collect
3-element Vector{Int64}:
 2
 2
 2

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 17, 2021, 1:50am UTC](https://discourse.julialang.org/t/question-on-lazy-map-function/53450/11 "2021-01-17T01:50:50Z")

</div>

> [@CameronBieganek](#):
>
> In Julia v1.5, `Iterators.map` does not appear to be lazy:

FYI, there’s `Compat.Iterators.map` for older Julia.

> [@mcabbott](#):
>
> It is however identical to writing the generator:

Somewhat unfortunately, as of [lowering: remove incorrect comprehension eta reduction by vtjnash · Pull Request #39139 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/39139) it’s not the case:

```julia
julia> (sqrt(x) for x in 1:10)
Base.Generator{UnitRange{Int64}, var"#1#2"}(var"#1#2"(), 1:10)

```

(but it’s required for correctness)
