Julia: How to Understand the Dot Syntax

Hongtao Hao / 2021-07-10


The following codes were tested under Julia v1.6.1 and DataFrames v1.2.0.

The Doc Syntax in Julia is in the form of FUN.(A) where A is an array. This syntax will apply the function to each element in the array you input.

For example:

julia> using DataFrames

julia> a = 1:5
1:5

julia> b = 'a':'e'
'a':1:'e'

julia> df = DataFrame("A" => a, "B" => b)
5×2 DataFrame
 Row │ A      B    
     │ Int64  Char 
─────┼─────────────
   1 │     1  a
   2 │     2  b
   3 │     3  c
   4 │     4  d
   5 │     5  e

julia> PlusOne = x -> x + 1 # Create a function
#1 (generic function with 1 method)

julia> df.C = PlusOne.(df.A)
5-element Vector{Int64}:
 2
 3
 4
 5
 6

julia> df
5×3 DataFrame
 Row │ A      B     C     
     │ Int64  Char  Int64 
─────┼────────────────────
   1 │     1  a         2
   2 │     2  b         3
   3 │     3  c         4
   4 │     4  d         5
   5 │     5  e         6

Last modified on 2021-10-05