https://github.com/athanclark/purescript-tuples-native
Implementation of tuples as a flat heterogeneous array, for FFI
https://github.com/athanclark/purescript-tuples-native
arrays heterogeneous-data purescript typelevel-programming
Last synced: 3 months ago
JSON representation
Implementation of tuples as a flat heterogeneous array, for FFI
- Host: GitHub
- URL: https://github.com/athanclark/purescript-tuples-native
- Owner: athanclark
- License: bsd-3-clause
- Created: 2017-10-07T00:49:48.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-15T17:53:24.000Z (almost 2 years ago)
- Last Synced: 2025-01-28T17:09:22.156Z (about 1 year ago)
- Topics: arrays, heterogeneous-data, purescript, typelevel-programming
- Language: PureScript
- Size: 18.6 KB
- Stars: 15
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# purescript-tuples-native
This library defines tuple data types that are represented under-the-hood as heterogeneous arrays to JavaScript - useful for
foreign interfaces. A set of `xt` functions are exposed to translate these native Tuples into Purescript pairs
or Nested tuples. So for example:
```purescript
-- Main.purs
import Data.Tuple.Native (T2, xt)
import Data.Tuple (Tuple)
foreign import lenTupleImpl :: String -> T2 String Int
lenTuple :: String -> Tuple String Int
lenTuple s = xt $ lenTupleImpl s
```
Could let you wrap this Javascript function
```javascript
"use strict";
function lenTuple (string) {
return [string, string.length]
}
exports.lenTupleImpl = lenTuple
```
You can also extract indidual elements directly from the Native tuple using `prj`.
```purescript
import Data.Tuple.Native (T3, t3, prj)
import Data.TypeLevel.Num.Reps (d0, d1, d2)
xs :: T3 String Int Boolean
xs = t3 "foo" 13 false
prj d0 xs == "foo" -- true
prj d1 xs == 13 -- true
prj d2 xs == false -- true
```