Functional programming in Julia

Some things that don’t directly allow functional syntax, but still support a functional style:

  • Underscores.jl gives you better syntax for lambdas, Chain.jl is a similar alternative
  • Base.Iterators and IterTools.jl give you some lazy evaluation, e.g. when combined with Underscores.jl you can write things like
function k_digit_numbers_from_sequence(k, my_sequence_fn)
    nums = @_ Iterators.countfrom() |> 
    IterTools.imap(my_sequence_fn(_), __) |> 
    Iterators.takewhile(<(10^k), __) |> 
    Iterators.filter(>=(10^(k-1), __) |> 
    collect
    return nums
end
  • Transducers.jl is an alternative to Iterators, inspired by Clojure
  • ThreadsX.jl makes it easy to parallelize functional code, especially with ThreadsX.map
8 Likes