### Install elm-units Package Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This command line snippet shows how to install the `elm-units` package using the Elm package manager. It is executed in a command prompt within your Elm project directory, assuming Elm is already installed. ```bash elm install ianmackenzie/elm-units ``` -------------------------------- ### Working with Rates of Change (Speed, Time, Distance) in Elm Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This comprehensive example illustrates the use of rate-related functions like `Quantity.per`, `Quantity.at`, and `Quantity.at_` to perform calculations involving speed, distance, and time. It covers deriving speed from length and duration, calculating distance from duration and speed, and determining time from length and speed, including unit conversions and a real-world example with the speed of light. ```elm -- How fast are we going if we travel 30 meters in -- 2 seconds? speed = Length.meters 30 |> Quantity.per (Duration.seconds 2) -- How far do we go if we travel for 2 minutes -- at that speed? Duration.minutes 2 -- duration |> Quantity.at speed -- length per duration |> Length.inKilometers -- gives us a length! --> 1.8 -- How long will it take to travel 20 km -- if we're driving at 60 mph? Length.kilometers 20 |> Quantity.at_ (Speed.milesPerHour 60) |> Duration.inMinutes --> 12.427423844746679 -- How fast is "a mile a minute", in kilometers per hour? Length.miles 1 |> Quantity.per (Duration.minutes 1) |> Speed.inKilometersPerHour --> 96.56064 -- Reverse engineer the speed of light from defined -- lengths/durations (the speed of light is 'one light -- year per year') speedOfLight = Length.lightYears 1 |> Quantity.per (Duration.julianYears 1) speedOfLight |> Speed.inMetersPerSecond --> 299792458 -- One astronomical unit is the (average) distance from the -- Sun to the Earth. Roughly how long does it take light to -- reach the Earth from the Sun? Length.astronomicalUnits 1 |> Quantity.at_ speedOfLight |> Duration.inMinutes --> 8.316746397269274 ``` -------------------------------- ### Perform Unit Conversions with elm-units Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This snippet provides practical examples of performing unit conversions using `elm-units`. It showcases how to convert durations, lengths, speeds, and temperatures between different units seamlessly, highlighting the library's ability to handle conversions while maintaining type safety. ```elm Duration.hours 3 |> Duration.inSeconds --> 10800 Length.feet 10 |> Length.inMeters --> 3.048 Speed.milesPerHour 60 |> Speed.inMetersPerSecond --> 26.8224 Temperature.degreesCelsius 30 |> Temperature.inDegreesFahrenheit --> 86 ``` -------------------------------- ### Applying Rate Functions to Generic Units (Pixel Density) in Elm Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This example demonstrates the versatility of the rate functions, showing that they are not restricted to typical physical units like speed. It calculates pixel density by dividing `Pixels` by `Length` and then uses this density to convert a `Length` into `Pixels`, highlighting the generic applicability of `Quantity.per` and `Quantity.at`. ```elm pixelDensity = Pixels.pixels 96 |> Quantity.per (Length.inches 1) Length.centimeters 3 -- length |> Quantity.at pixelDensity -- pixels per length |> Pixels.inPixels -- gives us pixels! --> 113.38582677165354 ``` -------------------------------- ### Compare and Sort Elm Quantity Values Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Illustrates various comparison and sorting functions available for `Quantity` values. Examples include `Quantity.greaterThan` for boolean comparison, `Quantity.compare` for ordering, `Quantity.max` for finding the maximum of two values, `Quantity.maximum` for a list, and `Quantity.sort` for sorting a list of quantities. ```elm Length.meters 1 |> Quantity.greaterThan (Length.feet 3) --> True Quantity.compare (Length.meters 1) (Length.feet 3) --> GT Quantity.max (Length.meters 1) (Length.feet 3) --> Length.meters 1 Quantity.maximum [ Length.meters 1, Length.feet 3 ] --> Just (Length.meters 1) Quantity.sort [ Length.meters 1, Length.feet 3 ] --> [ Length.feet 3, Length.meters 1 ] ``` -------------------------------- ### Apply Generic Operations on elm-units Quantity Types Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This Elm code demonstrates generic operations available for `Quantity` types in `elm-units`. It includes examples of comparison (`Quantity.lessThan`), addition (`Quantity.plus`), multiplication (`Quantity.times`, which can change the resulting unit type, e.g., Length * Length = Area), and sorting (`Quantity.sort`), showcasing the flexibility and power of the `Quantity` type for unit-aware arithmetic. ```elm Length.feet 3 |> Quantity.lessThan (Length.meters 1) --> True Duration.hours 2 |> Quantity.plus (Duration.minutes 30) |> Duration.inSeconds --> 9000 -- Some functions can actually convert between units! -- Multiplying two Length values gives you an Area Length.centimeters 60 |> Quantity.times (Length.centimeters 80) --> Area.squareMeters 0.48 Quantity.sort [ Angle.radians 1 , Angle.degrees 10 , Angle.turns 0.5 ] --> [ Angle.degrees 10 , Angle.radians 1 , Angle.turns 0.5 ] ``` -------------------------------- ### Perform Mathematical Operations with Custom Elm Tiles Unit Source: https://github.com/ianmackenzie/elm-units/blob/master/doc/CustomUnits.md Demonstrates how to use the custom `Tiles` unit in various mathematical operations. Examples include summing quantities, converting between tiles and pixels using rates, and calculating distances based on speed and duration, showcasing integration with `elm-units`' `Quantity` module. ```Elm import Game exposing (tiles, inTiles) import Pixels exposing (pixels, inPixels) import Duration exposing (seconds, milliseconds) import Quantity Quantity.sum [ tiles 5, tiles 2.3, tiles 0.6 ] --> tiles 7.9 pixelsPerTile : Quantity Float (Rate Pixels Tiles) pixelsPerTile = pixels 24 |> Quantity.per (tiles 1) tiles 3 |> Quantity.at pixelsPerTile --> pixels 72 speed : Quantity Float (Rate Tiles Seconds) speed = tiles 12 |> Quantity.per (seconds 1) milliseconds 30 |> Quantity.at speed --> tiles 0.36 ``` -------------------------------- ### Understanding Argument Order Conventions in Elm Quantity Functions Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This section clarifies the argument order for various functions within the `Quantity` module. It explains why some functions like `Quantity.minus` and `Quantity.lessThan` take the 'second argument first' to enable natural piping (`x |> Quantity.lessThan y`) and correct behavior with `List.map`. It contrasts these with functions like `Quantity.difference` and `Quantity.product` that follow a 'normal' argument order, aligning with common English phrasing. ```APIDOC Functions with 'second argument first' convention (e.g., for piping): - Quantity.lessThan x y: means y < x - Usage with pipe: x |> Quantity.lessThan y (means x < y) - Quantity.minus x y: means y - x - Usage with List.map: List.map (Quantity.minus x) [a, b, c] (results in [a - x, b - x, c - x]) Functions with 'normal' argument order: - Quantity.difference a b - Quantity.product a b - Quantity.rate a b - Quantity.ratio a b - Quantity.compare a b General Principle: Function names often align with English phrasing (e.g., 'the difference of a and b' vs. 'a minus b'). ``` -------------------------------- ### Perform Arithmetic Operations with Elm Quantity Types Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Demonstrates how to perform common arithmetic operations like addition, subtraction, and summation on `Quantity` values. It showcases the use of `Quantity.plus`, `Quantity.minus`, and `Quantity.sum` functions, along with unit conversion functions like `Length.inMeters` and `Duration.inMinutes`. ```elm Length.feet 6 |> Quantity.plus (Length.inches 3) |> Length.inMeters --> 1.9050000000000002 Duration.hours 1 |> Quantity.minus (Duration.minutes 15) |> Duration.inMinutes --> 45 -- pi radians plus 45 degrees is 5/8 of a full turn Quantity.sum [ Angle.radians pi, Angle.degrees 45 ] |> Angle.inTurns --> 0.625 ``` -------------------------------- ### Elm Quantity Type Expansion for Volume Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Explains how a single quantity type like `Volume` can be expressed in multiple equivalent ways within `elm-units`. It shows the expansion of type aliases such as `CubicMeters` into their underlying `Product`, `Squared`, and `Cubed` forms, which is useful for understanding compiler error messages. ```Elm Quantity Float CubicMeters Quantity Float (Cubed Meters) Quantity Float (Product (Product Meters Meters) Meters) Quantity Float (Product (Squared Meters) Meters) Quantity Float (Product SquareMeters Meters) ``` -------------------------------- ### Calculate Area of a Triangle using Quantity.product in Elm Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This snippet demonstrates how to calculate the area of a triangle by multiplying two `Length` quantities to obtain an `Area` using `Quantity.product`. It also shows the use of `Quantity.half` for scaling and unit conversion to `Area.inSquareInches`. ```elm -- Area of a triangle with base of 2 feet and -- height of 8 inches base = Length.feet 2 height = Length.inches 8 Quantity.half (Quantity.product base height) |> Area.inSquareInches --> 96 ``` -------------------------------- ### Define Type-Safe Data Structures with elm-units Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This Elm code demonstrates how to define a record type (`Camera`) using `elm-units` types like `Angle`, `Duration`, and `Temperature` to ensure type safety. It also includes a function (`canOperateAt`) that performs a comparison between `Temperature` values, showcasing how unit-aware operations integrate into application logic. ```elm type alias Camera = { fieldOfView : Angle , shutterSpeed : Duration , minimumOperatingTemperature : Temperature } canOperateAt : Temperature -> Camera -> Bool canOperateAt temperature camera = temperature |> Temperature.greaterThan camera.minimumOperatingTemperature ``` -------------------------------- ### Construct elm-units Quantity Values from Floats Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This snippet illustrates the common pattern for creating instances of `elm-units` quantity types (e.g., `Length`, `Duration`, `Angle`, `Temperature`) from raw `Float` values. Each function takes a float and returns a quantity type, ensuring the numeric input is correctly interpreted in the specified unit. ```elm Length.feet : Float -> Length Length.meters : Float -> Length Duration.seconds : Float -> Duration Angle.degrees : Float -> Angle Temperature.degreesFahrenheit : Float -> Temperature ``` -------------------------------- ### Create Constructor and Extractor Functions for Elm Tiles Unit Source: https://github.com/ianmackenzie/elm-units/blob/master/doc/CustomUnits.md Provides the `tiles` constructor function to create a `Quantity` of `Tiles` from a number, and the `inTiles` extractor function to convert a `Quantity` of `Tiles` back to a plain number. These functions are designed to work with both whole (`Int`) and partial (`Float`) numbers. ```Elm {-| Construct a quantity representing a given number of tiles -} tiles : number -> Quantity number Tiles tiles numTiles = Quantity numTiles {-| Convert a quantity of tiles back to a plain Float or Int -} inTiles : Quantity number Tiles -> number inTiles (Quantity numTiles) = numTiles ``` -------------------------------- ### Define Quantity Type and Aliases in Elm Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Illustrates the generic `Quantity` type definition, which serves as the foundation for all unit-aware values in `elm-units`. It also shows how specific unit types like `Length` are aliased using `Quantity` with a `Float` number and a specific unit type (e.g., `Meters`). ```elm type Quantity number units = Quantity number ``` ```elm type alias Length = Quantity Float Meters ``` -------------------------------- ### Use Custom Kinetic Energy Function with Elm Units Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Demonstrates the type-safe usage of the custom `kineticEnergy` function. Callers can supply arguments using various units (e.g., `Mass.tonnes`, `Speed.milesPerHour`) and extract results in desired units (e.g., `Energy.inKilowattHours`), showcasing the flexibility of the `elm-units` library. ```Elm kineticEnergy (Mass.tonnes 1.5) (Speed.milesPerHour 60) |> Energy.inKilowattHours --> 0.14988357119999998 ``` -------------------------------- ### Convert elm-units Quantity Values to Floats Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md This Elm code demonstrates functions used to convert `elm-units` quantity types back into plain `Float` values, specified in a desired unit. This is useful for extracting numeric representations for display, serialization, or interaction with external systems, regardless of the original unit used during construction. ```elm Length.inCentimeters : Length -> Float Length.inMiles : Length -> Float Duration.inHours : Duration -> Float Angle.inRadians : Angle -> Float Temperature.inDegreesCelsius : Temperature -> Float ``` -------------------------------- ### Define Custom Elm Unit Type for Tiles Source: https://github.com/ianmackenzie/elm-units/blob/master/doc/CustomUnits.md Illustrates how to define a new custom unit type, `Tiles`, within an Elm module. This establishes the basic unit structure for specific applications, such as representing units in a 2D tile-based game. ```Elm module Game import Quantity exposing (Quantity(..)) {-| Units in the game world -} type Tiles = Tiles ``` -------------------------------- ### Define Custom Kinetic Energy Function in Elm Source: https://github.com/ianmackenzie/elm-units/blob/master/README.md Illustrates how to define a custom function `kineticEnergy` in Elm for calculations that cannot be expressed using built-in `Quantity` functions. It highlights the need to work with raw `Float` values internally and ensure unit consistency, while maintaining a type-safe interface for callers. ```Elm kineticEnergy : Mass -> Speed -> Energy kineticEnergy (Quantity m) (Quantity v) = Quantity (0.5 * m * v^2) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.