elmcraft / core-extra / Basics.Extra

Additional basic functions.

Numbers

maxSafeInteger : number

The maximum safe value for an integer, defined as 2^53 - 1. Anything larger than that and behaviour becomes mathematically unsound.

maxSafeInteger + 1 --> maxSafeInteger + 2

minSafeInteger : number

The minimum safe value for an integer, defined as -(2^53 - 1). Anything smaller than that, and behaviour becomes mathematically unsound.

minSafeInteger - 1 --> minSafeInteger - 2

isSafeInteger : Basics.Int -> Basics.Bool

Checks if a given integer is within the safe range, meaning it is between -(2^53 - 1) and 2^53 - 1.

isSafeInteger 5 --> True

isSafeInteger maxSafeInteger --> True

isSafeInteger (maxSafeInteger + 1) --> False

Math

safeDivide : Basics.Float -> Basics.Float -> Maybe Basics.Float

Perform floating-point division (like Elm's / operator) that will never crash the app. If the y argument in safeDivide x y is zero, we return Nothing.

safeDivide 5 2 --> Just 2.5

-- the interesting part
safeDivide 5 0 --> Nothing

safeIntegerDivide : Basics.Int -> Basics.Int -> Maybe Basics.Int

Perform integer division (like Elm's // operator) that will never crash the app. If the y argument in safeIntegerDivide x y is zero, we return Nothing.

safeIntegerDivide 5 2 --> Just 2

-- the interesting part
safeIntegerDivide 5 0 --> Nothing

safeModBy : Basics.Int -> Basics.Int -> Maybe Basics.Int

Perform modular arithmetic that will never crash the app. If the modulus argument in safeModBy modulus x is zero, we return Nothing.

safeModBy 2 4 --> Just 0

safeModBy 2 5 --> Just 1

-- the interesting part
safeModBy 0 4 --> Nothing

Use safeRemainderBy for a different treatment of negative numbers, or read Daan Leijen’s Division and Modulus for Computer Scientists for more information.

safeRemainderBy : Basics.Int -> Basics.Int -> Maybe Basics.Int

Get the remainder after division in a way that will never crash the app. If the divisor argument in safeRemainderBy divisor x is zero, we return Nothing.

safeRemainderBy 2 4 --> Just 0

safeRemainderBy 2 5 --> Just 1

-- the interesting part
safeRemainderBy 0 4 --> Nothing

Use safeModBy for a different treatment of negative numbers, or read Daan Leijen’s Division and Modulus for Computer Scientists for more information.

Angles

inDegrees : Basics.Float -> Basics.Float

Convert standard Elm angles (radians) to degrees.

inDegrees (turns 2) --> 720

inDegrees pi --> 180

inRadians : Basics.Float -> Basics.Float

Convert standard Elm angles (radians) to radians.

inRadians (degrees 90) == pi / 2

inRadians (turns 1) == 2 * pi

inTurns : Basics.Float -> Basics.Float

Convert standard Elm angles (radians) to turns. One turn is equal to 360°.

inTurns (degrees 180) == 0.5

inTurns (3 * pi) == 1.5

Higher-Order Helpers

flip : (a -> b -> c) -> b -> a -> c

Flip the order of the first two arguments to a function.

curry : (( a, b ) -> c) -> a -> b -> c

Change how arguments are passed to a function. This splits paired arguments into two separate arguments.

uncurry : (a -> b -> c) -> ( a, b ) -> c

Change how arguments are passed to a function. This combines two arguments into a single pair.