Standard functions
The FSharp.Core.dll library is automatically imported into an Fβ― project or into the FSI console. It provides standard operators and functions (doc). Here are the main functions:
Conversion
box, tryUnbox, unbox : boxing and unboxing (attempt)
let i = 123 // val i : int = 123
let o = box i // val o : obj = 123
let i2: int = unbox o // val i3 : int = 123
let i3 = unbox<int> o // val i2 : int = 123
let ok = tryUnbox<int> o // val ok : int option = Some 123
let ko = tryUnbox<string> o // val ko : string option = None
unbox<float> o // π₯ System.InvalidCastExceptionbyte, char, decimal, float, int, string : to convert into byte, char, ...
let c_ko = char "ab" // π₯ System.FormatException
let c1 = char "c" // val c1 : char = 'c'
let c2 = char 32 // val c2 : char = ' '
let i_ko = int " " // π₯ System.FormatException
let i1 = int "30" // val i1 : int = 30
let i2 = int ' ' // val i2 : int = 32
let i3 = int 12.6 // val i3 : int = 12enum<'TEnum> : conversion to the specified enum π Composite types
Reflection
nameof, typeof :
typedefof is interesting with a generic type:
Math
abs,sign: absolute value, sign (-1 if < 0...)(a)cos(h),(a)sin,(a)tan: (co)sine/tangent (inverse/hyperbolic)ceil,floor,round: rounding (lower, upper)exp,log,log10: exponential, logarithm...pown x (n: int): power =xto thenpowersqrt: square root
Identity: id
idDefinition let id x = x
Signature : (x: 'T) -> 'T
β Single input parameter function
β Only returns this parameter
Why such a function β
β Name id = abbreviation of identity
β Zero / Neutral element of function composition
+ Addition
0
0 + 5 β‘ 5 + 0 β‘ 5
* Multiplication
1
1 * 5 β‘ 5 * 1 β‘ 5
>> Composition
id
id >> fn β‘ fn >> id β‘ fn
id use cases
id use casesWith a high-order function doing 2 things:
1 operation
1 value mapping via param
'T -> 'U
E.g. List.collect fn list = flatten + mapping
How to do just the operation and not the mapping?
list |> List.collect (fun x -> x)πlist |> List.collect idπβ Best alternative :
List.concat listπ―
Others
compare a b : int: returns -1 if a < b, 0 if =, 1 if >.hash: compute the hash (HashCode)max,min: maximum and minimum of 2 comparable valuesignore: to "swallow" a value; returns()
Extras
The following non-standard functions are commonly used, so recode them or use those in the Prelude module (source) in the NuGet package FSharpx.Extras.
Flip
flip, flip3 et flip4 are used to reverse the order of a function's parameters, so that the last parameter becomes the first.
Curry
curryandcurry3can be used to curry tuplified functions with 2 or 3 parameters.uncurryanduncurry3do the opposite.
βοΈ Comparison
dateIn2022Curry3: (int -> int -> System.DateTime)dateIn2022Manual: month: int -> day: int -> System.DateTime
π dateIn2022Manual has a little longer implem. but is easier to use thanks to named parameters.
Konst
konst is a sink function due to its second argument that is ignored in order to always return the first argument, the constant.
Example: generate an always-true predicate, to pass to the higher-order function filterMap:
π‘ ignore β
konst ()
Tee
tee (also called tap) is used to apply a value to a function and return that value, ignoring any return from the function. This is useful with a side-effect function, for example to log an intermediate value in a pipeline:
Parse
The primitive types Boolean, Byte, SByte, UInt16, Int16, UInt32, Int32, UInt64, Int64, Decimal, Single, Double, DateTime, DateTimeOffset are extended with the static method parse, which encapsulates the classic .NET static method TryParse. The aim is to return an Option π rather than a bool and an out variable.
String
The String module encapsulates string instance methods in functions pipeline-friendly. Here are a few examples:
Last updated
Was this helpful?