Function capture argument evaluation #4944
-
I have this code from an Exercism exercise: pub fn is_pangram(sentence: String) -> Bool {
"abcdefghijklmnopqrstuvwxyz"
|> string.to_graphemes
|> list.all(fn(letter) {
string.contains(string.lowercase(sentence), letter)
})
} In this code, I know that Now, suppose I replace that anonymous function with a function capture: pub fn is_pangram(sentence: String) -> Bool {
"abcdefghijklmnopqrstuvwxyz"
|> string.to_graphemes
|> list.all(string.contains(string.lowercase(sentence), _))
} In this case, will *I realize this could be optimized by the runtime, but this is just a trivial example. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The two examples compile to the exact same code (with the exception of different variable names), so yes 26 times for both. |
Beta Was this translation helpful? Give feedback.
The two examples compile to the exact same code (with the exception of different variable names), so yes 26 times for both.
If you only want to call it once, you should declare a variable beforehand