Functions
Named functions
Declared a
let
binding (like a variable)Naming convention: camelCase
No
return
keyword: the function always returns the last expression in its bodyNo
()
around all parameters, no,
between parameters()
required around parameter with type annotation (1) or deconstruction (2)
Functions of 2 or more parameters
Separate parameters and arguments with spaces:
Inference
The inference of the add
function can be confusing: the +
works for any numbers and for strings too, but add
is limited to int
! To get it work, we need to write let inline add ...
ā Related to a special kind of generics: statically resolved type parameters (SRTP) š
ā ļøļø ,
creates another kind of functions using tuples š
Functions without parameter
Use ()
(like in C#)
āļø unit
means "nothing" š
Multi-line function
Indentation required, but no need for {}
Can contain sub-function
Anonymous function
A.k.a. Lambda, arrow function
Syntax:
fun {parameters} -> body
(ā in CāÆ{parameters} ā body
)In general,
()
required all around, for precedence reason
_.Member shorthand (F⯠8)
Naming convention related to functions
It's usual in F⯠to use short names:
x
,y
,z
: parameters for values of the same typef
,g
,h
: parameters for input functionsxs
: list ofx
_
: discard an element not used (like in C⯠7.0)
āļø Suited for a short function body or for a generic function:
Piping
Pipe operator |>
: same idea that in UNIX with |
ā value |> function
send a value to a function
ā match left-to-right reading order: "subject verb"
ā same order than with OOP: object.method
Pipeline: chain of pipings
Style of coding to emphasize the data flowing from functions to functions ā without intermediary variable š
Similar to a built-in fluent API ā no need to return the object at the end of each method š
If/then/else expression
In FāÆ, if/then(/else)
is an expression, not a statement, so every branch (then
and else
) should return a value and both returned values should be type-compatible.
š” if b then x else y
ā C⯠ternary operator b ? x : y
ā When then
returns "nothing", else
is optional:
š” We can use elif
keyword instead of else if
.
Match expression
Equivalent in C⯠8 :
Last updated
Was this helpful?