# Fun One Liners

**URL:** <https://discourse.julialang.org/t/fun-one-liners/28352>\
**Category:** General Usage\
**Created:** [September 3, 2019, 11:13pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352 "2019-09-03T23:13:18Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 6, 2019, 7:15pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/41 "2019-09-06T19:15:15Z")

</div>

> [@e3c6](#):
>
> Maybe one should include the time required to do `using ForwardDiff` into the comparison? If one has a nice analytic function with a complex implementation already, then, depending on what you want, `ComplexStepDerivative` can be better than adding a dependency to `ForwardDiff` .

It really just depends on the number of partials since complex step will repeat the primal.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 6, 2019, 9:20pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/42 "2019-09-06T21:20:57Z")

</div>

I’m (100-ε )% shure that this Animation can be done better, but have to got to bad now:

```julia
j=0;i=0; d=2; while true ; sleep(0.05);global i=i +0.02;global d; global j = j+1; if floor(i)%d==1 print("\e[4D\e[K")end; if floor(i)%d==0 print("\e[K") end ; print(Char((128512+j%128))); if i%10==0 d= rand(2:5) end ; end

```

_tomorrow i will write `ForwardDiff` as an one liner . . . not_

---

<div class="post-metadata">

**Author:** ![YermolenkoIgor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yermolenkoigor/32/8479_2.png) [@YermolenkoIgor](https://discourse.julialang.org/u/YermolenkoIgor)\
**Post date:** [September 11, 2019, 10:15am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/43 "2019-09-11T10:15:51Z")

</div>

My favorute:

```julia
julia> fib(n) = (BigInt.([1 1; 1 0])^(n-1))[1]
fib (generic function with 1 method)

julia> fib.([1:10;])
10-element Array{BigInt,1}:
  1
  1
  2
  3
  5
  8
 13
 21
 34
 55

julia> fib(100)
354224848179261915075

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 11, 2019, 11:20am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/44 "2019-09-11T11:20:10Z")

</div>

> [@anon92994695](#):
>
> ```julia
> rbinomial( p, size... ) = map( x -> ( x < p ) ? 1 : 0, rand( size... ) )
> 
> ```

How about

```julia
rbinomial( p, size... ) = rand( size... ) .< p

```

?

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [September 12, 2019, 1:02am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/45 "2019-09-12T01:02:10Z")

</div>

> [@Tamas\_Papp](#):
>
> This is like saying that you found a nice approximation to e^x that is ok for small x, when you have `exp` built into the language.

You mean like this one liner?

```julia
my_exp(x; n=1e10) = (1 + 1/n)^(n*x)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 12, 2019, 1:50am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/46 "2019-09-12T01:50:11Z")

</div>

One of the first packages I made is [Fatou.jl](https://github.com/chakravala/Fatou.jl) which is intended to provide a one-liner interface for [fractals](https://github.com/chakravala/Fatou.jl/wiki/Explore-Fatou-sets-&-fractals)

> [@\[ANN\] Fatou.jl : Easily share Julia fractals](https://discourse.julialang.org/t/ann-fatou-jl-easily-share-julia-fractals/12278/1):
>
> ```julia
> using Fatou, ImageInTerminal # or PyPlot with plot
> mandelbrot(:(ℯ^(-z)+cos(c)+im*sin(c*z)),∂=[-π,π,-2,2],n=700,N=80,cmap="jet",iter=false,p=-0.3) |> fatou
> 
> ```
> 
> ![outc](https://global.discourse-cdn.com/julialang/original/2X/8/8b9a624ce3ec4d4f3e76b55b99299b0682f63bdd.png)

but I haven’t seen anyone post their own version Fatou one-liner fractals anywhere yet

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 6:04am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/47 "2019-09-12T06:04:55Z")

</div>

Precisely! This is a nice example, as it exhibits all the problems I mentioned above:

1. it is absolutely unnecessary, as we have `exp`,

2. ~~it is about 1000x slower:~~ this is incorrect, thanks @DNF

3. it is of course inaccurate:

---

<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:** [September 12, 2019, 7:57am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/48 "2019-09-12T07:57:27Z")

</div>

> [@Tamas\_Papp](#):
>
> it is about 1000x slower:

Actually, I’m not sure about that. I don’t know if I’m using this correctly, but I believe you may have to force the computation to happen during runtime, instead of letting the compiler figure out the answer:

```julia
julia> @btime exp(Ref($x)[]);
  28.093 ns (0 allocations: 0 bytes)

julia> @btime my_exp(Ref($x)[]);
  23.459 ns (0 allocations: 0 bytes)

```

I don’t think I did that quite right, but if you look at the broadcasted version:

```julia
julia> x = 100 * rand(1000);

julia> @btime exp.($x);
  9.457 μs (1 allocation: 7.94 KiB)

julia> @btime my_exp.($x);
  22.152 μs (1 allocation: 7.94 KiB)

```

it’s a lot closer than 1000x.

Is the benchmarking `Ref` trick documented anywhere? I couldn’t find it.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 8:05am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/49 "2019-09-12T08:05:34Z")

</div>

Good point. Sub-ns timings should have been a warning. Cf  
[https://github.com/JuliaCI/BenchmarkTools.jl/issues/130](https://github.com/JuliaCI/BenchmarkTools.jl/issues/130)

---

<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:** [September 12, 2019, 8:19am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/50 "2019-09-12T08:19:00Z")

</div>

Yeah, that’s a good idea. But I frequently see this `Ref($x)[]` (or something similar) trick. Could that be automated? Or maybe it should just be explained in the [manual](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md)? I cannot find it there.

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [September 12, 2019, 9:43am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/51 "2019-09-12T09:43:40Z")

</div>

I

> [@Tamas\_Papp](#):
>
> This is a nice example

I am glad that you liked my example! Hopefully I wont derail the conversation again, but I believe that we have stumbled onto something simultaneously profound and silly at the same time.

A lot of people can be seduced by elegance. Here we see an example where one line of code can be used to approximate one of the most important/beautiful functions in mathematics. The approximation directly uses the definition

e=\lim\_{n\to\infty}\left(1+\frac{1}{n}\right)^n,

so the approximation is highly elegant. Furthermore, the approximation can get you 6 digits of accuracy in mere nanoseconds! (As long as you are using a fast language like Julia.)

Although, as you pointed out the elegance conceals just how impractical the one liner is. I would not want to see anybody using this in production code.

Also, as a side note I have realized that:

\exp(x)=\lim\_{n\to\infty}\left(1+\frac{x}{n}\right)^n

is a slightly better approximation and would also work as a one liner.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 10:30am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/52 "2019-09-12T10:30:12Z")

</div>

> [@JackDevine](#):
>
> Furthermore, the approximation can get you 6 digits of accuracy in mere nanoseconds!

AFAIK Pade approximants (with some judicious scaling and squaring, if necessary) dominate this, but I may not remember correctly. As far as I am aware, actual implementations are table-driven, eg

> Tang, Ping-Tak Peter. “Table-driven implementation of the exponential function in IEEE floating-point arithmetic.” _ACM Transactions on Mathematical Software (TOMS)_ 15.2 (1989): 144-157.

---

<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:** [September 12, 2019, 1:56pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/53 "2019-09-12T13:56:09Z")

</div>

@chakravala that is awesome!

Bound an angle to be between 0 and 2pi (or tau, hey can we get tau as a constant?)

```julia
Wrap(x) = ( (x % 2pi) + 2pi ) % 2pi

```

Hamilton Product

```julia
HamiltonProduct(a, b) = [a[1]*b[1] - a[2]*b[2] - a[3]*b[3] - a[4]*b[4],
                          a[1]*b[2] + a[2]*b[1] + a[3]*b[4] - a[4]*b[3],
                          a[1]*b[3] - a[2]*b[4] + a[3]*b[1] + a[4]*b[2],
                          a[1]*b[4] + a[2]*b[3] - a[3]*b[2] + a[4]*b[1]
                        ]

```

---

<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:** [September 12, 2019, 3:36pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/54 "2019-09-12T15:36:34Z")

</div>

> [@DNF](#):
>
> Yeah, that’s a good idea. But I frequently see this `Ref($x)[]` (or something similar) trick. Could that be automated? Or maybe it should just be explained in the [manual](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md)? I cannot find it there.

In fact, I have a PR that does just that, but I don’t think it’ll be accepted. [Document the practice of referencing and dereferencing variables to stop constant prop by MasonProtter · Pull Request #140 · JuliaCI/BenchmarkTools.jl · GitHub](https://github.com/JuliaCI/BenchmarkTools.jl/pull/140)

For those curious, the correct thing to do is generally `$(Ref(x))[]` instead of `Ref($x)[]`, since you don’t want to accidentally measure the time it takes to apply `Ref` to `x`.

---

<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:** [September 12, 2019, 8:10pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/55 "2019-09-12T20:10:13Z")

</div>

there is [https://github.com/JuliaMath/Tau.jl](https://github.com/JuliaMath/Tau.jl)  
and `mod2pi` comes with Julia.

---

<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:** [September 14, 2019, 8:35pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/56 "2019-09-14T20:35:28Z")

</div>

I think I’ll just continue to define Tau myself rather then import this package. Maybe there’s some elaborate argument why it should be “tau ~ 2pi” but for my uses, it’s just 2pi.

```julia
tau = 2pi

```

Cool to know about mod2pi. Didn’t know it was in the base library, because I had no idea there was a name for this function, the code for it is well, interesting: [https://github.com/JuliaLang/julia/blob/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L801-L987](https://github.com/JuliaLang/julia/blob/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L801-L987)  
Guess this is why I’d never call myself a soft-ware engineer!

Here’s another reason why I love Julia:

```julia
"""Convenience function for performing Redheffer Star Products."""
⋆( A, B ) = RedhefferStarProduct( A, B )

```

The above function won’t run on your machine - but, you can define unicode operators for shorthand! So if I wanna Redheffer star some stuff

```julia
F=A⋆B⋆C⋆D⋆E

```

---

<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:** [September 14, 2019, 9:03pm UTC](https://discourse.julialang.org/t/fun-one-liners/28352/57 "2019-09-14T21:03:05Z")

</div>

Here is a useful (maybe not so fun) oneliner that I use everywhere. Just put the following line after `module MyPackage` so that `?MyPackage` shows README

```julia
@doc read(joinpath(dirname(@ __DIR__ ), "README.md"), String) MyPackage

```

(It’s based on code I saw somewhere. Maybe it was @mbauman?)

There is also a multi-line version if you want to run doctest on README:

````julia
@doc let path = joinpath(dirname(@ __DIR__ ), "README.md")
    include_dependency(path)
    replace(read(path, String), "```julia" => "```jldoctest")
end MyPackage

````

---

<div class="post-metadata">

**Author:** ![MrUrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrurq/32/8924_2.png) [@MrUrq](https://discourse.julialang.org/u/MrUrq)\
**Post date:** [September 16, 2019, 7:40am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/58 "2019-09-16T07:40:54Z")

</div>

Simulate the frog problem shown recently on [standupmaths](https://www.youtube.com/watch?v=ZLTyX4zL2Fc) with one line and

```julia
frog(n,cur_lily,steps) = (cur_lily == n) ? steps : frog(n,rand(cur_lily+1:n),steps+1)

```

estimate the mean number of steps needed to jump a 10 step wide river with another

```julia
mean(frog(10,0,0) for _ in 1:1_000_000)

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 16, 2019, 8:35am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/59 "2019-09-16T08:35:11Z")

</div>

```julia
a=[1:3;] # equal to collect(1:3)

```

---

<div class="post-metadata">

**Author:** ![mhinsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhinsch/32/26676_2.png) [@mhinsch](https://discourse.julialang.org/u/mhinsch)\
**Post date:** [September 16, 2019, 10:50am UTC](https://discourse.julialang.org/t/fun-one-liners/28352/60 "2019-09-16T10:50:26Z")

</div>

single line grep:

`grep(file, pattern) = collect(Iterators.filter(x -> occursin(pattern, x), eachline(file)))`

[Previous page](https://discourse.julialang.org/t/fun-one-liners/28352.md?page=2)

[Next page](https://discourse.julialang.org/t/fun-one-liners/28352.md?page=4)
