# 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:** 3

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 21, 2020, 9:41am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/50 "2020-12-21T09:41:40Z")

</div>

> [@Juan](#):
>
> Or a simulation of moving people getting infected with a virus when they meet other people.

[https://raw.githubusercontent.com/m3g/CKP/master/figures/md.gif(image larger than 4 MB)](https://raw.githubusercontent.com/m3g/CKP/master/figures/md.gif)

I have one of those, but not in a compact code.

From here: [GitHub - m3g/CKP](https://github.com/m3g/CKP)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 21, 2020, 10:58am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/51 "2020-12-21T10:58:00Z")

</div>

> [@Juan](#):
>
> What about a simulation with balls bouncing against each other?

@LaurentPlagne posted this beautiful example: ![](https://global.discourse-cdn.com/julialang/original/2X/d/d05fed09ac4f08699f53f02a661ad10229534e72.gif)

See [Plot a circle with a given radius with Plots.jl](https://discourse.julialang.org/t/plot-a-circle-with-a-given-radius-with-plots-jl/23295). I don’t know if he has published the code for it though.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [December 21, 2020, 11:58am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/52 "2020-12-21T11:58:48Z")

</div>

```julia
] activate .
instantiate

```

This is some of my favorite Julia code.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 22, 2020, 10:03am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/53 "2020-12-22T10:03:46Z")

</div>

Thank you for the comment 😉

Unfortunately I don’t think that it can fit the 7 lines example list (\>1000 SLOC)…

We use this example to show how to structure a not-too-small project (PkgSkeleton, types, modules, sub-modules, tests, export, dependencies, multiple dispatch, profile, //isme …) in our Julia training lectures.

Although I enjoy reading the examples in this thread, I would second @Tamas_Papp saying that Julia’s power is more strongly demonstrated through slightly larger examples.

We must take some time to refactor the package from its training material form and publish it : now I have an extra motivation to see if a Julia wizard could compact it significantly 😃

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 22, 2020, 12:21pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/54 "2020-12-22T12:21:04Z")

</div>

Side note: Love the color scheme. What are you using for it?

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [December 22, 2020, 12:21pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/55 "2020-12-22T12:21:59Z")

</div>

I think seven lines of code is excellent. It showcases what Julia can do, and hopefully shows how Jukia can express scientific notation or equations in a compact way.

However one small pleading from me. Let’s now go down the road of Obfuscated Perl. Back in the day when Perl was a dominant language for Web programming, and many other things, there were a lot of smart people involved. Still are probably! There were regular ‘Obfuscated Perl’ competitions.  
Also the whitespace as code stuff (where you translate code into …err… meaningful whitespace ) etc.  
I know I am being like the Grinch but IMHO these did the language no good

> **[Obfuscated Perl Contest](https://en.wikipedia.org/wiki/Obfuscated_Perl_Contest)**
>
> The Obfuscated Perl Contest was a competition for programmers of Perl which was held annually between 1996 and 2000. Entrants to the competition aimed to write "devious, inhuman, disgusting, amusing, amazing, and bizarre Perl code". It was run by The Perl Journal and took its name from the International Obfuscated C Code Contest.
> The entries were judged on aesthetics, output and incomprehensibility. One entrant per year received the Best of Show award.
> Entrants were advised to try and demonstra...

Don’t get me wrong - there is nothing bad about using a language feature but please, please explain what it does so other people can learn.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 22, 2020, 12:33pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/56 "2020-12-22T12:33:22Z")

</div>

I recently started using [SatelliteToolbox.jl](https://github.com/JuliaSpace/SatelliteToolbox.jl), so here is my submission. This code calculates the location (latitude, longitude, altitude) of the ISS “right now”. The readability suffers a bit trying to stick to seven, but it’s not too bad. This is based off of the example [here](https://www.ronanarraes.com/2019/01/the-satellite-toolbox-for-julia/) (see the link for full details).

```julia
using Dates, SatelliteToolbox  
# Download data for ISS
download("https://celestrak.com/NORAD/elements/stations.txt", joinpath(pwd(), "space_stations.txt")) 
# Initialize the orbit propagator
orbp = init_orbit_propagator(Val(:sgp4), read_tle("space_stations.txt")[1])
# Get the current time (Julian date)
rightnow = DatetoJD(now())
# Propogate the orbit to "right now"
o, r_teme, v_teme = propagate_to_epoch!(orbp, rightnow)
# Get the position (radians, radians, meters)
lat, lon, h = ECEFtoGeodetic(rECItoECEF(TEME(), ITRF(), rightnow, get_iers_eop())*r_teme)
# Nice print out after conversions
println("Current location of the ISS: $(rad2deg(lat))° $(rad2deg(lon))° $(h/1000) km")

```

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [December 22, 2020, 1:15pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/57 "2020-12-22T13:15:23Z")

</div>

@mihalybaci Is Santa tracking data available on 24 December?

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 22, 2020, 1:30pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/58 "2020-12-22T13:30:16Z")

</div>

😆

Not sure, I poked around [here](https://celestrak.com/NORAD/elements//) a bit, and didn’t see anything for Santa. But this site gets its data from NORAD, and they’re the ones that do the Santa tracking, so it could be hidden in a file somewhere.

---

<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:** [December 24, 2020, 5:09pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/60 "2020-12-24T17:09:35Z")

</div>

Möbius discovered the one-sided [non-orientable strip](https://en.wikipedia.org/wiki/M%C3%B6bius_strip) in 1858 while serving as the chair of astronomy at the University of Leipzig. The concept inspired mathematicians in the development of the field of topology as well as artists like [M.C. Escher](https://www.pleacher.com/mp/puzzles/tricks/mobstrip.html).  
Fifteen lines of Julia were required to compute and plot the Mobius strip and a moving normal vector, which changes color after each `2π` revolution. This exceeds the limit but someone might be able to compress it further.  
_(PS: edited code for better readability. Without `sleep`, the last `for` loop could be written as a comprehension too)_

```julia
using Makie, ForwardDiff, LinearAlgebra
const FDd = ForwardDiff.derivative

M(u,v) = ((r + v/2*cos(u/2))*cos(u), (r + v/2*cos(u/2))*sin(u), v/2*sin(u/2))
∂M∂u(u,v) = [FDd(u->M(u,v)[1],u), FDd(u->M(u,v)[2],u), FDd(u->M(u,v)[3],u)]
∂M∂v(u,v) = [FDd(v->M(u,v)[1],v), FDd(v->M(u,v)[2],v), FDd(v->M(u,v)[3],v)]

r = 1.0; w = 0.5; m = 90; clr = (:cyan,:red,:cyan,:red,:cyan)
u = range(0, 8π, length = 4m); v = range(-w, w, length = 3)

N = [-cross(∂M∂u(ui, 0.0), ∂M∂v(ui, 0.0))[j] for ui in u, j in 1:3 ]
N = N ./ sqrt.(sum(abs2,N,dims=2)); N = vec.(Point3f0.(N[:,1],N[:,2],N[:,3]))
xs, ys, zs = [[p[i] for p in M.(u, v')] for i in 1:3]
P0 = vec.(Point3f0.(xs[:,2], ys[:,2], zs[:,2]))

surface(xs[1:m+1,:], ys[1:m+1,:], zs[1:m+1,:], backgroundcolor=:black)
for i in 1:4m
    sleep(0.01); k = 1 + i÷(m+0.5);
    arrows!([P0[i]],[N[i]],lengthscale=0.3,arrowsize=0.05,arrowcolor=clr[k],linecolor=clr[k])
end

```

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

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [December 24, 2020, 7:02pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/61 "2020-12-24T19:02:46Z")

</div>

Instead of compressing it further - I’d be more interested to see it broken out to be more readable :). Awesome plot though!

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [February 21, 2021, 8:48pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/62 "2021-02-21T20:48:08Z")

</div>

For what it is worth, Makie’s ˋbandˋ function actually makes 2d bands in 3d space… comes in handy for your Möbius strips:

```julia
t = 0:0.1:2pi+0.1
lower = [Point3f0(.2cos(3s),0.0, 0.0) .+ (1 + 0.2sin(3s))*Point3f0(0, cos(s), sin(s)) for s in t]
upper = [Point3f0(-.2cos(3s),0.0, 0.0) .+ (1 - 0.2sin(3s)).*Point3f0(0, cos(s), sin(s)) for s in t]
band(lower, upper, color = [1.0:length(t)÷2; length(t)÷2:-1:1.0; length(t)÷2:-1:1.0; 1.0:length(t)÷2])

```

 ![Screenshot 2021-02-21 at 21.46.27](https://global.discourse-cdn.com/julialang/original/3X/2/0/20b2eb046967548b8e0bb9f137814e067fe22d4c.jpeg)

---

<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:** [February 21, 2021, 9:57pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/63 "2021-02-21T21:57:09Z")

</div>

Thanks. As a curiosity, plotting the normal around that new strip seems to indicate that it is orientable?

 ![Makie_band_is_orientable](https://global.discourse-cdn.com/julialang/original/3X/8/2/826c75366ca8f0af5fe3f8dbcd268a043426fa49.png)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 21, 2021, 10:19pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/64 "2021-02-21T22:19:49Z")

</div>

Would you mind sharing the code for that normal vector?

The orientability depends on the parity of the number of twists.

---

<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:** [February 21, 2021, 10:20pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/65 "2021-02-21T22:20:23Z")

</div>

That depends on how many times the band is twisted before it is glued back together.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [February 21, 2021, 10:25pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/66 "2021-02-21T22:25:19Z")

</div>

Depends whether it is an even or an odd number of turns 🙂, factor `3s` corresponds to 6 turns which is even, here is `s/2` one turn

```julia
lower = [Point3f0(.2cos(s/2),0.0, 0.0) .+ (1 + 0.2sin(s/2))*Point3f0(0, cos(s), sin(s)) for s in t]
upper = [Point3f0(-.2cos(s/2),0.0, 0.0) .+ (1 - 0.2sin(s/2)).*Point3f0(0, cos(s), sin(s)) for s in t]
band(lower, upper, color = [1.0:length(t); length(t):-1:1.0], show_axis=false)

```

![Screenshot 2021-02-21 at 23.19.47](https://global.discourse-cdn.com/julialang/original/3X/b/e/bed86c81f346f5aaad3a081a8e43953e786120c8.png)  
Not orientable. I also do a little trick with the colouring.

---

<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:** [February 21, 2021, 10:27pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/67 "2021-02-21T22:27:11Z")

</div>

@dpsanders, the code is the same as in the Mobius strip further above (using the partial derivatives and cross product). Just changed M(u,v) consistently with @mschauer’s strip definition:

```julia
M(u,v) = (v/2*cos(3u), (r + v/2*sin(3u))*cos(u), (r + v/2*sin(3u))*sin(u)) # other strip by @mschauer

```

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [March 12, 2021, 10:49am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/68 "2021-03-12T10:49:59Z")

</div>

I saw this one [on Twitter](https://twitter.com/milankloewer/status/1370081555566235649):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/c/5c8ed0972343358f2262833f9c04052adf9bf48b.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/a/5a0c3d3bf831f7793a4812af5e629c8cf3296e6f.png)

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 12, 2021, 1:21pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/69 "2021-03-12T13:21:09Z")

</div>

Very nice, I hope you’ll forgive me stealing this in a math.stackexchange comment at [real analysis - For what values does the geothmetic meandian converge? - Mathematics Stack Exchange](https://math.stackexchange.com/questions/4057261/for-what-values-does-the-geothmetic-meandian-converge#comment8385268_4057261) 🙂

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [March 12, 2021, 3:40pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/70 "2021-03-12T15:40:52Z")

</div>

Compare w Python

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

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

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