Fun One Liners

The goal of the code is to get the “first and last number in a sequence of consecutive numbers”, or by example, from:

df = DataFrame([]) 
df.list = [[1,2,3,6,12,13,14],[20,21,23,24,50],[1,3,5,7,10,11,12,13]]

to add the following field:

df.new = [[1,3,12,14],[20,21,23,24],[10,13]] #it is important that they are made up of pairs!

Various solutions with better run-time are shown in thread:
https://discourse.julialang.org/t/how-to-reduce-redundancy-in-a-list-inside-a-dataframe/88585
But a neat one-line solution is:

transform(df, :list => ByRow(f->filter(x->(x-1 ∈ f) ⊻ (x+1 ∈ f),f)) => :new)

Kudos to @rafael.guerra for pointing me to this thread.

3 Likes