Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wking-io/pair
Canonical Product Type that allows you to hold and use two values in one type.
https://github.com/wking-io/pair
Last synced: about 1 month ago
JSON representation
Canonical Product Type that allows you to hold and use two values in one type.
- Host: GitHub
- URL: https://github.com/wking-io/pair
- Owner: wking-io
- License: other
- Created: 2019-01-04T16:18:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-11T17:28:13.000Z (almost 6 years ago)
- Last Synced: 2024-04-08T09:07:10.779Z (7 months ago)
- Language: Elm
- Size: 37.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pair
A Product Type that allows you to hold and use two values in one type. This useful for working with more than one related value similar to tuple or list, but since it is limited to only two values the API is more focused.
There are many uses for the Pair type and, if you know of a good one that is not below please add a PR. I use the Pair type primarily for computations on values that have "metadata" or some extra information about the value that needs to travel with it.
```elm
import Pair-- A math example
average : List Float -> Float
average list =
list
|> Pair.branchWith List.sum List.length
|> Pair.merge (/)average [ 9, 11, 34 ] -- > 18
-- More complex example.
type alias Item =
{ name : String
, cost : Float
}sumCart : List Items -> Float
sumCart items =
List.foldl (\acc { price } -> total + price ) 0 itemscalculateCart : List Items -> Pair (List Items) Float
calculateCart items =
branch items
|> Pair.map sumCartcalculateCart
[ { name = "Apple", price = 0.75 }
, { name = "Banana", price = 0.53 }
, { name = "Tangerine", price = 0.98 }
]{- > Pair
[ { name = "Apple", price = 0.75 }
, { name = "Banana", price = 0.53 }
, { name = "Tangerine", price = 0.98 }
]
2.26-}
```