# Is Julia lazy?

**URL:** <https://discourse.julialang.org/t/is-julia-lazy/64192>\
**Category:** New to Julia\
**Created:** [July 7, 2021, 4:25am UTC](https://discourse.julialang.org/t/is-julia-lazy/64192 "2021-07-07T04:25:57Z")\
**Posts on this page:** 1\
**Showing post:** 23

<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:** [July 7, 2021, 4:19pm UTC](https://discourse.julialang.org/t/is-julia-lazy/64192/23 "2021-07-07T16:19:54Z")

</div>

> [@Gilles\_Dubois](#):
>
> I will study this in detail for myself. I don’t think I will use it on my site, because for education I don’t take care about power, performance, elegance, I privilege ‘readability’ that’s why my first choice was Python.

So, it never really occurred to me that elegance was the enemy of true mathematics 🙃 But anyway, I just wanted to show a simpler example:

```julia
struct Z{P} <: Integer
    x::Int
    Z{P}(n::Integer) where {P} = new{P}(mod(n, P))
end

Base.promote_rule(::Type{<:Number}, ::Type{Z{P}}) where {P} = Z{P}
Base.show(io::IO, z::Z) = show(io, z.x)

Base.:+(a::Z{P}, b::Z{P}) where {P} = Z{P}(a.x + b.x)
Base.:-(a::Z{P}, b::Z{P}) where {P} = Z{P}(a.x - b.x)
Base.:-(a::Z{P}) where {P} = Z{P}(-a.x)
Base.:*(a::Z{P}, b::Z{P}) where {P} = Z{P}(a.x * b.x)
Base.div(a::Z{P}, b::Z{P}) where {P} = Z{P}(a.x ÷ b.x) # I don't think this is needed, really

```

Now you can

```julia
jl> z = zeros(Z{7}, 3, 3)
3×3 Matrix{Z{7}}:
 0 0 0
 0 0 0
 0 0 0

jl> one(z)
3×3 Matrix{Z{7}}:
 1 0 0
 0 1 0
 0 0 1

jl> z = Z{11}.(rand(0:10, 3, 3))
3×3 Matrix{Z{11}}:
  3 3 4
  3 4 5
 10 4 10

jl> z.^2 .+ 1
3×3 Matrix{Z{11}}:
 10 10 6
 10 6 4
  2 6 2

jl> maximum(z.^2 .+ 1)
10

jl> (2 + 3im) * Z{11}(2)
4 + 6im

# etc.

```

I think comparison and random sampling is really neat, but whatever.

```julia
Base.:<(a::Z{P}, b::Z{P}) where {P} = (a.x < b.x)
Base.:<=(a::Z{P}, b::Z{P}) where {P} = (a.x <= b.x)
function Random.rand(rng::AbstractRNG, ::Random.SamplerType{Z{P}}) where {P}
    return Z{P}(rand(rng, 0:P-1))
end

```

Anyway, if you are teaching this to students, you shouldn’t drag macros into it, and for all that is holy, don’t use `global`(!)

---

_[View the full topic](https://discourse.julialang.org/t/is-julia-lazy/64192)._
