https://github.com/tfc/purescript-ziprecord
Purescript Example: How to map a binary function generically over (nested) records
https://github.com/tfc/purescript-ziprecord
meta-programming purescript row-types
Last synced: about 2 months ago
JSON representation
Purescript Example: How to map a binary function generically over (nested) records
- Host: GitHub
- URL: https://github.com/tfc/purescript-ziprecord
- Owner: tfc
- Created: 2023-01-25T09:51:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T08:01:52.000Z (about 2 years ago)
- Last Synced: 2025-01-13T16:47:47.065Z (4 months ago)
- Topics: meta-programming, purescript, row-types
- Language: Nix
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Purescript example: How to Zip Records with a Binary Function
Given an example typeclass `Foo` that provides a function `foo :: a -> a -> a`
for all members of the type class, how to make all possible records part of
this type class (Assuming all record members are themselves part of `Foo`)?This is the target functionality:
```purescript
> import ZipRecord-- simple merging of type class members Int, Number, and String:
> foo 1 2
3
> foo 1.0 2.0
3.0
> foo "foo" "bar"
"foobar"-- Now let's merge records that have Int, Number, and String values:
> r1 = { a: 1, b: 1.0, c: { d: "foo", e: 10.0 } }
> r2 = { a: 2, b: 2.0, c: { d: "bar", e: 20.0 } }> zipRecord r1 r2
{ a: 3, b: 3.0, c: { d: "foobar", e: 30.0 } }
```## How to build and test
Either install `node`, `spago`, and `purescript` yourself, or just run a
[nix shell](https://nixos.org/download.html):```sh
$ nix-shell# build the product and run the unit tests
$ spago test
```