---
title: "Discussion on \"Straightforward Functional Programming Examples in Julia\" | Hashnode"
description: "Discussion on \"Straightforward Functional Programming Examples in Julia\". Functional programming has gained quite some popularity in recent years. Yet if you code with the Julia language you probably already used a lot of functional programming concepts without really thinking about it. In it's essence functional programmi..."
url: "https://hashnode.com/posts/straightforward-functional-programming-examples-in-julia/66ea9f397fbba42647d1a479"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Straightforward Functional Programming Examples in Julia](https://scientificcoder.com/straightforward-functional-programming-examples-in-julia?utm_source=hashnode&utm_medium=feed)

Functional programming has gained quite some popularity in recent years. Yet if you code with the Julia language you probably already used a lot of functional programming concepts without really thinking about it. In it's essence functional programmi...

Sep 18, 2024 · 5 min read · scientificcoder.com  
Read the article

-   [#julia](https://hashnode.com/tag/julia)
-   [#functional-programming](https://hashnode.com/tag/functional-programming)

## Written by

[Matthijs Cox](https://hashnode.com/@matthijscox)

@matthijscox · The Netherlands

  
[View profile](https://hashnode.com/@matthijscox)

Scientific Software Engineer

A scientific software developer with over a decade of experience in academia, startups and industry. My mission is to turn you into an elite numerical computing professional.

## Responses 1

Markdown supported · ⌘↵ to post

[Wolf Nederpel](https://hashnode.com/@wnederpel)

[Oct 8, 2024](https://hashnode.com/posts/straightforward-functional-programming-examples-in-julia/66ea9f397fbba42647d1a479/comment/6704e6380c41bccb83c67e3e)

Regarding the first example it's interesting to note that while for small arrays and searching for a frequent occurence using x->xx<num seems directly related to the rarity of the searched for condition. Even for small arrays where the condition does not occur, this approah is significantly slower than the other approaches (and the vectorized approach is fastest).

[Matthijs Cox](https://hashnode.com/@matthijscox)

[Oct 8, 2024](https://hashnode.com/posts/straightforward-functional-programming-examples-in-julia/66ea9f397fbba42647d1a479/comment/6704e6380c41bccb83c67e3e#reply-6705295d4836dcf7075e2df0)

Hi Wolf, not sure what you are testing. Note that the timing depends on how you test it.

The `any(x -> x < 3, numbers)` might look slow if you use `@time`, but that's because the anonymous function keeps getting recompiled for every call. Maybe that's what you mean?

You can easily bypass this by placing it inside another function. Also you can use `BenchmarkTools.@btime` instead to ignore the compilation time.

```
julia> numbers = [1, 2, 3, 4, 5];

julia> @time any(x -> x < 3, numbers)
  0.006801 seconds (2.74 k allocations: 183.680 KiB, 99.01% compilation time)
true

julia> @time any(numbers .< 3)
  0.000012 seconds (3 allocations: 128 bytes)
true

julia> any_less(numbers, value) = any(x -> x < value, numbers)
any_less (generic function with 1 method)

julia> @time any_less(numbers, 3) # first call compiles the `any_less`
  0.009657 seconds (6.90 k allocations: 456.047 KiB, 99.76% compilation time)
true

julia> @time any_less(numbers, 3) # now it's fast
  0.000005 seconds
true
```

But perhaps you are talking about something else?

[Wolf Nederpel](https://hashnode.com/@wnederpel)

[Oct 9, 2024](https://hashnode.com/posts/straightforward-functional-programming-examples-in-julia/66ea9f397fbba42647d1a479/comment/6704e6380c41bccb83c67e3e#reply-670670c3e04931f33be331ed)

I had made a mistake, wrote my code as a script as such:

```
using BenchmarkTools

numbers = rand(100)
num = 0.001

println("Size of test array $(length(numbers))")
println("Roughly $(num*100)% of entries satisfy condition (entry < $num)")
println()

println("result with <($num)")
@btime any(<(num), numbers)

println("result with x -> x < $num")
@btime any(x -> x < num, numbers)

println("numbers .< $num")
@btime any(numbers .< num)
```

However this has to allocate space for the array and number for each call to any. In the `any(x -> x < num)` case it even has to allocate num for each iteration done by any.

This lead to confusion on my part as `any(x -> x < num)` slowed down more than the other approaches when more iterations were required to find a match which I initially did not expect.
