Fun One Liners

@StevenSiew - I think it’s funny because a very large number is interpreted as zero. This thing called humor has a Personal element to it. It wasn’t expected to me - in Python if you do ```2**100`` you get a long/BigInt of the appropriate size.

@dataDiver - I like that idea. sharing nifty and elegant solutions could lead to a good learning experience and show off the actual power of Julia (which is not in 1 liners).

1 Like

Of course what you find funny can be subjective, just note that overflow behavior in Julia is documented very early in the manual:

https://docs.julialang.org/en/latest/manual/integers-and-floating-point-numbers/#Overflow-behavior-1

2 Likes

Well, I thought it was funny, even though I knew about integer overflow. It’s an odd and interesting way to write zero. Seems to be in the spirit of the thread.

5 Likes

r = 0; while true print("$r "); r = 1//(1 + floor( r ) - r + trunc( r )) end

This is an enumeration of the rational numbers which I learned on this blog.
The creation of numbers out of nothing.

3 Likes

Really cool!

But the code didn’t run for me :frowning:
I wrote it like this and it did though :smiley:

r = 0
while true 
    global r
    print("$r "); r = 1//(1 + floor( r ) - r + trunc( r )) 
end

Neat one though!!

2 Likes

One of my favorite “golfs” I like to show people new to Julia is the dot product of vectors:

dot(x,y) = x'y

For example, if you want to normalize a vector so it has Euclidean norm 1:

unitize(x) = x/√(x'x)

Before someone lectures me about golfing: I’m aware it’s not good practice. That doesn’t take away the fun!

9 Likes

@TheCedarPrince Nice one!

@Deidre_House no lectures just fun one liners :slight_smile:

2 Likes

Thanks - had to redact it though after further testing as I realized it was somewhat fragile and not robust enough to handle other cases.

2 Likes

No worries :slight_smile: share when you have something you’re comfortable with sharing. It’s just for funsies.

2 Likes

So there is cumsum but recently I needed a cumdiff!

cumdiff( a ) = [ 0; -cumsum( reverse( diff( reverse( a ) ) ) ) ]

Basically as you scroll down a vector of numbers it returns an equal length vector as the original of the displacements

1 Like

Maybe I am missing something, but isn’t this equivalent to

cumdiff(a) = a .- first(a)

?

6 Likes

I find the following hack is helpful at the REPL to print every element of A:

julia> println.(A);
14 Likes

Tamas - you are missing nothing :smiley: . Ever get really tired and work your way into a very weird solution to a problem? Because that’s what happened here hehehe. In retrospect your solution is obvious and definitely preferred!

5 Likes

That’s basically my job description :wink:

6 Likes

I think you should be proud! It’s kind of a Rube Goldberg one-liner😉

5 Likes

Code to reverse an array inplace along a given dimension, since reverse! does not have a dims argument:

ipr = Iterators.product
ci  = CartesianIndices
reversedim!(a;d::Int) = foreach(i->reverse!(view(a,i[1].I...,:,i[2].I...)),ipr(ci(axes(a)[1:(d-1)]),ci(axes(a)[d+1:ndims(a)])))
1 Like
f(v::Vector{T}, n=length(v), w=zeros(T, n)) where T = w

That one does nothing interesting, except showcasing the fact you can use both given value and parametric type in the definition of optional arguments. And this (somehow) brings me immense joy.

2 Likes
identity.(a)

to constrain the element type of an array as much as possible, for example (look at the element type):


julia> a = Any[1.0, 1.0f0, big"1.0"]
3-element Array{Any,1}:
 1.0
 1.0f0
 1.0

julia> identity.(a)
3-element Array{AbstractFloat,1}:
 1.0
 1.0f0
 1.0

julia> a = Any[1.0, 2.0, 3.0]
3-element Array{Any,1}:
 1.0
 2.0
 3.0

julia> identity.(a)
3-element Array{Float64,1}:
 1.0
 2.0
 3.0
32 Likes

This is quite mind blowing… :upside_down_face: Is it expected behavior though?

This is neat! I am curious. Is this an implementation detail and can change any time? Or is this a reliable way to narrow down the element type?