# Seven Lines of Julia (examples sought)

**URL:** <https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416>\
**Category:** General Usage\
**Created:** [November 19, 2020, 7:37am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416 "2020-11-19T07:37:29Z")\
**Posts on this page:** 20\
**Page:** 7

<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 18, 2022, 9:57am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/133 "2022-01-18T09:57:34Z")

</div>

Checked the Laplacian: `Δ(u) == ∇⋅∇(u) # and got it is true`

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [February 2, 2022, 8:09pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/134 "2022-02-02T20:09:06Z")

</div>

Addition of uniform, identically distributed random variables. In one case we are adding a variable to itself (dependent distribution), and in the other we we are adding it to an independent, identically distributed variable.

```julia
using Plots, MonteCarloMeasurements

a = 1..2
b = 1..2

default(alpha=0.5, nbins=20, normalize=true)
plot(a+a, label="a+a")
plot!(a+b, label="a+b")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d582ed8cd8c12ee822d2ee734f9713925d1c3c3.png)

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [February 9, 2022, 4:03pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/135 "2022-02-09T16:03:51Z")

</div>

Adjacency matrix of hyper-rectangular/toroidal lattices of any dimensions

```julia
using LinearAlgebra, SparseArrays
A⊕B = kron(I(size(B,1)), A)+kron(B, I(size(A,1)))
lattice(n) = spdiagm(1=>trues(n-1),-1=>trues(n-1))
lattice(L...) = lattice(L[1])⊕lattice(L[2:end]...)
clattice(n) = (A=lattice(n); A[1,end]=A[end,1]=true; A)
clattice(L...) = clattice(L[1])⊕clattice(L[2:end]...)

```

I find this pretty damn useful besides being cool

```julia
julia> clattice(3,4,2,5) # toroidal lattice of dimensions 3,4,2,5
120×120 SparseMatrixCSC{Int64, Int64} with 840 stored entries:
⢞⣱⢄⠑⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⣄⠀⠀⠀⠀⠀⠀
⢄⠑⢞⣱⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⣄⠀⠀⠀⠀
⠑⢄⠀⠀⢞⣱⢄⠑⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⣄⠀⠀
⠀⠀⠑⢄⢄⠑⢞⣱⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⣄
⠑⢄⠀⠀⠀⠀⠀⠀⢞⣱⢄⠑⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠑⢄⠀⠀⠀⠀⢄⠑⢞⣱⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⢞⣱⢄⠑⠀⠀⠀⠀⠓⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⢄⠑⢞⣱⠀⠀⠀⠀⠀⠀⠓⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⢞⣱⢄⠑⠓⣄⠀⠀⠓⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⢄⠑⢞⣱⠀⠀⠓⣄⠀⠀⠓⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢤⠀⠀⠙⢤⠀⠀⢏⡵⢄⠑⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢤⠀⠀⠙⢤⢄⠑⢏⡵⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢤⠀⠀⠀⠀⠀⠀⢏⡵⢄⠑⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢤⠀⠀⠀⠀⢄⠑⢏⡵⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⢏⡵⢄⠑⠀⠀⠀⠀⠑⢄⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⢄⠑⢏⡵⠀⠀⠀⠀⠀⠀⠑⢄
⠙⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⢏⡵⢄⠑⠑⢄⠀⠀
⠀⠀⠙⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⢄⠑⢏⡵⠀⠀⠑⢄
⠀⠀⠀⠀⠙⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⠀⠀⢏⡵⢄⠑
⠀⠀⠀⠀⠀⠀⠙⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠑⢄⢄⠑⢏⡵

```

---

<div class="post-metadata">

**Author:** ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)\
**Post date:** [February 12, 2022, 10:36pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/136 "2022-02-12T22:36:05Z")

</div>

Plotting an animated heart

```julia
using Plots
n = 400
t = range(0, 2π, length = n)
x = 16sin.(t).^3
y = 13cos.(t) .- 5cos.(2t) .- 2cos.(3t) .- cos.(4t)
@gif for i ∈ 1:n
    circleplot(x, y, i, line_z = 1:n, cbar = false, c = :reds,framestyle = :none)
end when i > 40 && mod1(i, 10) == 5

```

![jl_K9sme4](https://global.discourse-cdn.com/julialang/original/3X/8/a/8aab85e202f6cff3df0d9b6da46f7a832ed7a7ef.gif)

---

<div class="post-metadata">

**Author:** ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)\
**Post date:** [February 12, 2022, 11:01pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/138 "2022-02-12T23:01:56Z")

</div>

Plots.jl

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [February 13, 2022, 3:33pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/140 "2022-02-13T15:33:06Z")

</div>

> [@Leticia-maria](#):
>
> Plotting an animated heart
> 
> ```julia
> using Plots
> n = 400
> t = range(0, 2π, length = n)
> x = 16sin.(t).^3
> y = 13cos.(t) .- 5cos.(2t) .- 2cos.(3t) .- cos.(4t)
> @gif for i ∈ 1:n
> circleplot(x, y, i, line_z = 1:n, cbar = false, c = :reds,framestyle = :none)
> end when i > 40 && mod1(i, 10) == 5
> 
> ```
> 
> ![jl_K9sme4](https://global.discourse-cdn.com/julialang/original/3X/8/a/8aab85e202f6cff3df0d9b6da46f7a832ed7a7ef.gif)

Is’nt that from Plots.jl documentation ? It’s still beautifull 🙂

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 13, 2022, 3:39pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/141 "2022-02-13T15:39:56Z")

</div>

I get: UndefVarError: circleplot not defined

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 13, 2022, 3:53pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/142 "2022-02-13T15:53:51Z")

</div>

I think there is no rule the code has to be originally written by the Discourse user. It only need to be short, memorable, and not under a restrictive license.

---

<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:** [February 13, 2022, 4:24pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/143 "2022-02-13T16:24:29Z")

</div>

that is the way of greater benefit

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [February 13, 2022, 4:34pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/144 "2022-02-13T16:34:34Z")

</div>

My current favourite

```julia
using ForwardDiff, ReverseDiff
function callprice(r, T, σ, S₀, K; n = 100)
    memo = Dict(); dt = T / n; u = exp(σ * √dt); q = (exp(r * dt) - 1/u) / (u - 1/u)
    function helper(i, pos)
        get!(memo, (i, pos)) do
            if i == n; max(S₀ * u^pos - K, 0) else exp(- r * dt) * (q*helper(i+1, pos+1) + (1-q)*helper(i+1, pos-1)) end end end
    helper(0, 0) end
(ForwardDiff.derivative(s -> callprice(r, T, σ, s, K), S₀), ReverseDiff.gradient(s -> callprice(r, T, σ, s[1], K), [S₀]))

```

Automatic differentiation through a memoized recursive function … would probably not even have tried that in any other language.  
Unfortunately, I did not get it to work when using an array instead of a dictionary as the memo-table. Maybe I should have another look at Zygote buffers …

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [February 13, 2022, 5:08pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/145 "2022-02-13T17:08:45Z")

</div>

> [@Henrique\_Becker](#):
>
> I think there is no rule the code has to be originally written by the Discourse user.

Still I would expect mentioning the source - actually this is required by most FOSS licences.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 11, 2022, 12:42pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/146 "2022-03-11T12:42:53Z")

</div>

The other day, a friend came to me asking for the survival probabilities of the normal distributions from 9 to 16 sigmas. I answered “Why dont you compute them yourself, you know how they are defined, and you know how to get them in both R and python !”. The precision of the question ticked in my head for a second…

We both know that the Normal distribution is light tailed, so we expected these probabilities to be pretty low. Then, firering up Julia, I understood why he had troubles founding them :

```julia
using Distributions
N = Normal();
1 .- cdf(N,1:16)
1 .- cdf(N,big.(1:16))

```

 ![Julia_Is_The_Best](https://global.discourse-cdn.com/julialang/original/3X/5/1/511c007161304590e0daeef5d4b32f1868a58bc8.png)

This is really cool. One of the principal reasons I love Julia.

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [March 24, 2022, 6:00am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/147 "2022-03-24T06:00:13Z")

</div>

The survival function/complementary CDF actually comes with Distributions.jl:

```julia
using Distributions
N = Normal();
ccdf(N,1:16)
ccdf(N,big.(1:16))

```

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [March 24, 2022, 5:48pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/148 "2022-03-24T17:48:21Z")

</div>

ASCII Mandelbrot set (had to golf it a bit to fit in 7 lines)

```julia
function mandelbrot(a, z=0)
    for i=1:2e4
        z = z^2 + a
    end
    abs(z)
end
[prod(x->mandelbrot(complex(x, y)) < 2 ? "∘" : " ",-2.0:0.03125:0.5) for y=-1:0.05:1].|>println;

```

in the REPL it prints

```julia
                                                                ∘                
                                                                                 
                                                                                 
                                                            ∘∘                   
                                                         ∘∘∘∘∘∘∘                 
                                                         ∘∘∘∘∘∘∘                 
                                                          ∘∘∘∘∘                  
                                                            ∘                    
                                               ∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘            
                                               ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘      
                                                ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
                                              ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘       
                                             ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
                                            ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                                           ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘     
                           ∘∘ ∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                            ∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                         ∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                         ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘     
                     ∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
 ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘        
                     ∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
                         ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘     
                         ∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                            ∘∘∘∘∘∘∘∘∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                           ∘∘ ∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                                           ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘     
                                            ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘    
                                             ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
                                              ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘       
                                                ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘      
                                               ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘ ∘∘∘      
                                               ∘∘ ∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘            
                                                            ∘                    
                                                          ∘∘∘∘∘                  
                                                         ∘∘∘∘∘∘∘                 
                                                         ∘∘∘∘∘∘∘                 
                                                            ∘∘                   
                                                                                 
                                                                                 
                                                                ∘ 

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 25, 2022, 4:42pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/149 "2022-03-25T16:42:11Z")

</div>

I just come across this implementation of Mathematica’s Nest:

`nest( f::Function, x0, n::Int) = (n <= 0) ? x0 : (f ∘ nest)(f,x0,n-1)`

in my search for an efficient Julia implementation of Mathematica’s NestList. Does anyone have such a thing? Just createing a list recursively like this implementation would be expensive, so it’d be necessary to allocate the list at the beginning then fill it. Is there any way the matrix construction notation `[f(x) for i in range]` might be useful?

Thanks,  
Niall.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 25, 2022, 5:41pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/150 "2022-03-25T17:41:01Z")

</div>

```julia
julia> nestlist(f::Function, x0, n::Integer) = n==0 ? [x0] : push!(nestlist(f, x0, n-1), f(nestlist(f, x0, n-1)[end]))
nestlist (generic function with 1 method)

julia> nestlist(x->x*2, 1, 3)
4-element Vector{Int64}:
 1
 2
 4
 8

```

This only works if `f(x0)` has the same type as `x0`. Otherwise, I need to use an array of elements of generic type, like

```julia
nestlist(f::Function, x0, n::Integer) = n==0 ? Any[x0] : push!(nestlist(f, x0, n-1), f(nestlist(f, x0, n-1)[end]))

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 25, 2022, 6:12pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/151 "2022-03-25T18:12:58Z")

</div>

Oh, right - thanks! There are two things that are surprising me here, but I’m relatively new to Julia. First, I had thought that the multiple push!es would bring a significant overhead, and second, I had thought the recursion would have an overhead. So I was thinking more of something along the lines of:

```julia
function nestlist( f::Function, x0, n::Integer)
	if n ≤ 0
		[x0]
	else
		nl = fill(x0,n+1)
		for i in 1:n
			nl[i+1] = f(nl[i])
		end
		nl
	end
end

```

So am I totaly mistaken in these concerns?

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 25, 2022, 7:14pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/152 "2022-03-25T19:14:03Z")

</div>

`push!` is not expensive as it has amortized cost of O(1), though it’s certainly advantageous to pre-allocate the array and avoid recursion. By the way, if you really want to make it as efficient as possible, you could change `nl = fill(x0,n+1)` into

```julia
nl = Vector{typeof(x0)}(undef, n+1)
nl[1] = x0

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 26, 2022, 12:47am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/153 "2022-03-26T00:47:36Z")

</div>

The only real performance killer in @greatpet’s code is calling `nestlist` a second time just to get the last element. And I think this is being done there just to fit it into a single line, instead of two lines.

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 26, 2022, 5:30am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/154 "2022-03-26T05:30:41Z")

</div>

😁 Many thanks.

Best wishes,

Niall.

[Previous page](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416.md?page=6)

[Next page](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416.md?page=8)
