Hi! I am learning Nim via exercism.io, and find myself doubting about the function call style. Take this exercise, for example:
import math, sequtils
# We are going to exploit that digits 0-9 are consecutive in ASCII.
const ord0 = ord('0')
func isArmstrongNumber*(n: int): bool =
let nstr = $n
let nlen = len(nstr)
n == foldl(nstr, a + (ord(b) - ord0)^nlen, 0)
Would a Nim developer write it like that? Or rather
import math, sequtils
# We are going to exploit that digits 0-9 are consecutive in ASCII.
const ord0 = '0'.ord
func isArmstrongNumber*(n: int): bool =
let nstr = $n
let nlen = nstr.len
n == nstr.foldl(a + (b.ord - ord0)^nlen, 0)
? Or maybe both are considered good style and everyone picks their own?
the way I usually go about this is:
to use dot calls for cases when a function "belongs" to an object (like in the object oriented sense). to use normal calls for cases where all parameters are "equal" (e.g. min(a, b), distance(a, b), intersection(first, second)).
Though yeah it all comes down to what looks best in your eyes, that's what syntactic sugar is for!
This is very much a matter of preference. But there are some things that are more common than others. In generally I'd say if you extract a property of something, or if the procedure name sounds like a verb I'd use . so I can put the "subject" of the statement to the left. So for example I'd use str.len and str.foldl (short for "fold left") but I'd use ord('0') because this is calling a procedure that doesn't look like a verb and the ordinal value of '0' isn't really a property. If it was called toOrd on the other hand I'd use '0'.toOrd.
But again, this is all just what I prefer, at the end of the day it's just up to you and what you find the most readable (unless you're sharing your code of course).
I hear you wrt foldl!
My first implementation was a straightforward for loop that anyone understand just by looking at it, vs foldl makes me think for some reason. Finally looked up in the docs how to do it that way, but in normal circumstances I'd write the for loop.