Convenience functions for 3-Tuples (also known as Triple).
Please remember the following from the Tuple documentation:
For more complex data, it is best to switch to records. So instead of representing a 3D point as
(3,4,5)and not having any helper functions, represent it as{ x = 3, y = 4, z = 5 }and use all the built-in record syntax!
This is good advice! However triples do have their usecases for anonymous groupings and as such having some helper functions can be quite helpful.
Create a triple.
Occasionally you might want to create a Triple from a single value. This does just that.
Triple.Extra.from 1 --> ( 1, 1, 1 )Extract the first value from a triple.
Extract the second value from a triple.
Extract the third value from a triple.
Given a function that takes three arguments, apply that function to the three values contained in a Triple.
( 1, 2, Array.fromList [0,1,2,3,4] )
|> Triple.Extra.apply Array.slice
--> Array.fromList [1,2,3]Similar to List.sortWith but for Triples instead of lists. Instead of
converting values contained in a Triple to comparables, instead supply a
function that will produce an Order directly.
Triple.Extra.sortWith Basics.compare ( 2, 1, 3 )
--> ( 1, 2, 3 )Apply a function to all values contained in a Triple.
Triple.Extra.map negate ( -3, 10, -7 )
--> ( 3, -10, 7 )Transform the first value in a triple.
Triple.Extra.mapFirst negate ( -3, 10, '9' )
--> ( 3, 10, '9' )Transform the second value in a triple.
Triple.Extra.mapSecond negate ( -3, 10, '9' )
--> ( -3, 010, '9' )Transform the third value in a triple.
Triple.Extra.mapThird String.toUpper ( -3, 10, "a" )
--> ( -3, 10, "A" )Turns a triple into a list of three elements.
Triple.Extra.toList ( 1, 2, 3 )
--> [ 1, 2, 3 ]
By type aliasing Triples into a "normal" type, we remove the (small) effort required in reading types and signatures that have Triples in. This is most beneficial when a Triple is nested inside another type. Visually, the Triple type is now no different to List, Maybe, or Result.
For example, this:
becomes: