I'm obviously late to the party here, but it would be interesting to know why you want to create a divBy2
function. There are two problems here, and solving one of them might be enough, depending on your needs.
The first problem is that there's no LanguagePrimitives.GenericTwo
. That's easy to fix, but the solution is of limited use if you want to define specific division functions for divisors other than 2:
let inline divBy2 n =
n / (LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne)
For less typing, you can assign LanguagePrimitives.GenericOne
to a variable, which becomes more useful as the magnitude of your divisor increases:
let inline divBy4 n =
let one = LanguagePrimitives.GenericOne
let two = one + one
n / (two + two)
This solution is also unhelpful if you want to create a general function. The "custom" way would be
let inline divBy divisor dividend = dividend / divisor
We can use this with partial function application to halve a list of bytes, for example, like this:
let halfBytes = [ 1uy .. 10uy ] |> List.map (divBy 2uy)
But we can do better. This problem applies to all non-commutative operators, including subtraction. To get around it, we can define
let flip f a b = f b a
This allows, for example
let scaledInThousands = [ 0m .. 500m .. 3000m ] |> List.map (flip (/) 1000m)
let decrementedIntegers = [ 1 .. 10 ] |> List.map (flip (-) 1)
If you want, you can still define the divBy function:
let inline divBy n = flip (/) n
let halfInts = [ 1 .. 10 ] |> List.map (divBy 2)
let halfLongs = [ 1L .. 10L ] |> List.map (divBy 2L)
let fifthLongs = [ 1L .. 10L ] |> List.map (divBy 5L)
let oneThroughTenOverPi = [ 1.0 .. 10.0 ] |> List.map (divBy System.Math.PI)