diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7c68b07 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# https://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md new file mode 100644 index 0000000..b79b995 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -0,0 +1,19 @@ +--- +name: Bug report +about: Report an issue +title: "" +labels: bug +assignees: "" +--- + +**Describe the bug** +A clear and concise description of the bug. + +**To Reproduce** +A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue. + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/change-request.md b/.github/ISSUE_TEMPLATE/change-request.md new file mode 100644 index 0000000..a2ee685 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/change-request.md @@ -0,0 +1,21 @@ +--- +name: Change request +about: Propose an improvement to this library +title: "" +labels: "" +assignees: "" +--- + +**Is your change request related to a problem? Please describe.** +A clear and concise description of the problem. + +Examples: + +- It's frustrating to have to [...] +- I was looking for a function to [...] + +**Describe the solution you'd like** +A clear and concise description of what a good solution to you looks like, including any solutions you've already considered. + +**Additional context** +Add any other context about the change request here. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..8d7661e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: PureScript Discourse + url: https://discourse.purescript.org/ + about: Ask and answer questions on the PureScript discussion forum. + - name: PureScript Discord + url: https://purescript.org/chat + about: Ask and answer questions on the PureScript chat. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..d8780f7 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,11 @@ +**Description of the change** +Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR. + +--- + +**Checklist:** + +- [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username +- [ ] Linked any existing issues or proposals that this pull request should close +- [ ] Updated or added relevant documentation in the README and/or documentation directory +- [ ] Added a test for the contribution (if applicable) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fe9f2da --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v7 + + - uses: actions/setup-node@v7 + with: + node-version: 24 + + - name: Set up a PureScript toolchain + uses: purescript-contrib/setup-purescript@main + with: + purescript: "unstable" + spago: "latest" + purs-tidy: "latest" + + - name: Cache PureScript dependencies + uses: actions/cache@v6 + with: + key: ${{ runner.os }}-spago-${{ hashFiles('spago.yaml', 'spago.lock') }} + path: | + .spago + output + + - name: Install dependencies + run: spago install + + - name: Build source and tests + run: spago build + + - name: Run tests + run: spago test + + - name: Check formatting + run: purs-tidy check src test diff --git a/.gitignore b/.gitignore index e4de85d..7e82b68 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ -*~ -output/ -bower_components/ -node_modules/ -.psci -.psci_modules/ -yarn-error.log -yarn.lock -generated-docs/ +.* +!.gitignore +!.github +!.editorconfig +!.tidyrc.json + +output +generated-docs +bower_components diff --git a/.tidyrc.json b/.tidyrc.json new file mode 100644 index 0000000..4f013c1 --- /dev/null +++ b/.tidyrc.json @@ -0,0 +1,10 @@ +{ + "importSort": "source", + "importWrap": "source", + "indent": 2, + "operatorsFile": null, + "ribbon": 1, + "typeArrowPlacement": "first", + "unicode": "never", + "width": null +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..43930d7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,92 @@ +# Changelog + +Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## Unreleased + +Breaking changes: + +New features: + +Bugfixes: + +Other improvements: + +## [v13.2.1](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.2.1) - 2026-10-11 + +Other improvements: + +- Migrate to registry-backed Spago and update CI tooling. + +## [v13.2.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.2.0) - 2023-02-13 + +Other improvements: + +- Correct `Typed` `slice` and `subArray` docs (#51 by @jamesdbrock) + +## [v13.1.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.1.0) - 2022-12-01 + +New features: + +- `Data.ArrayBuffer.Cast` (#46 by @jamesdbrock) + +## [v13.0.0](https://github.com/purescript-contrib/purescript-arraybuffer/releases/tag/v13.0.0) - 2022-04-27 + +Breaking Changes: +- Migrate FFI to ES modules (#41 by @JordanMartinez) +- Replaced polymorphic proxies with monomorphic `Proxy` (#41 by @JordanMartinez) + +## v12.0.0 + +Delete the `TypedArray` polyfill which was preventing this +library from working with `purs bundle` v0.14.4. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + +### Breaking Changes + +May lose partial polyfill `TypedArray` support for only the methods present +in regular JavaScript Arrays. +https://web.archive.org/web/20171019084331/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill + +## v11.0.3 + +Revert to v11.0.1. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/37 + +## v11.0.2 + +Delete the `TypedArray` polyfill which was preventing this +library from working with `purs bundle` v0.14.4. +https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + +## v11.0.1 + +Regenerate `bower.json`. + +## v11.0.0 + +Jorge Acereda has graciously donated this package to __purescript-contrib__. + +For version 11, we have refactored this library so that it depends only on +other packages in __purescript-contrib__. + +https://github.com/purescript-contrib/governance/issues/40 + +We have removed the dependencies on these non-__purescript-contrib__ packages: + +* https://pursuit.purescript.org/packages/purescript-typelevel +* https://pursuit.purescript.org/packages/purescript-quickcheck-combinators + +In v11.0.0 of this package, we have also upgraded to PureScript v0.14. + +### Breaking Changes + +To upgrade to v11.0.0, you might need to do a few substitutions +to the type declarations in your own dependent code: + +* Replace the type `AProxy` with `Proxy` from the Prelude. +* Remove most of the `Nat` typeclass constraints. https://github.com/purescript-contrib/purescript-arraybuffer/issues/29 +* Replace any `BytesPerValue a b` typeclass constraints with `BytesPerType a`. + +We have also privatized `Typed.part'`. https://github.com/purescript-contrib/purescript-arraybuffer/issues/32 + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..bff4e66 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,5 @@ +# Contributing to Arraybuffer + +Thanks for your interest in contributing to `arraybuffer`! We welcome new contributions regardless of your level of experience or familiarity with PureScript. + +Every library in the Contributors organization shares a simple handbook that helps new contributors get started. With that in mind, please [read the short contributing guide on purescript-contrib/governance](https://github.com/purescript-contrib/governance/blob/main/contributing.md) before contributing to this library. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f6503c4 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 PureScript Contrib + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index e318957..ea4f39e 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,102 @@ -# purescript-arraybuffer +# arraybuffer -ArrayBuffer bindings for PureScript. +[![CI](https://github.com/purescript-contrib/purescript-arraybuffer/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-arraybuffer/actions?query=workflow%3ACI+branch%3Amain) +[![Release](https://img.shields.io/github/release/purescript-contrib/purescript-arraybuffer.svg)](https://github.com/purescript-contrib/purescript-arraybuffer/releases) +[![Pursuit](https://pursuit.purescript.org/packages/purescript-arraybuffer/badge)](https://pursuit.purescript.org/packages/purescript-arraybuffer) +[![Maintainer: jacereda](https://img.shields.io/badge/maintainer-jacereda-teal.svg)](https://github.com/jacereda) +[![Maintainer: jamesdbrock](https://img.shields.io/badge/maintainer-jamesdbrock-teal.svg)](https://github.com/jamesdbrock) + + +Bindings and implementation for mutable JavaScript `ArrayBuffer`s. + +An `ArrayBuffer` is a built-in JavaScript object for storage of a flat continuous +region of memory. + +The `Typed` module provides a view into an `ArrayBuffer` for array +access of aligned local-machine-endian types, for in-process flat memory operations. + +The `DataView` module provides a view into an `ArrayBuffer` for inter-process +flat memory operations. + +* [MDN `ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +* [ECMA-262 `ArrayBuffer`](https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffer-objects) ## Installation -``` - bower install purescript-arraybuffer +Install `arraybuffer` with [Spago](https://github.com/purescript/spago): + +```sh +spago install arraybuffer ``` ## Documentation -Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-arraybuffer). +`arraybuffer` documentation is stored in a few places: + +1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-arraybuffer). +2. Written documentation is kept in the [docs directory](./docs). +3. Usage examples can be found in [the test suite](./test). + +If you get stuck, there are several ways to get help: + +- [Open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues) if you have encountered a bug or problem. +- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. + +## Contributing + +You can contribute to `arraybuffer` in several ways: + +1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-arraybuffer/issues). We'll do our best to work with you to resolve or answer it. + +2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions. + +3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. + +## Usage -See https://github.com/AlexaDeWit/purescript-arraybuffer-codecs if you need text/base64 encoding/decoding functions. +### Polyfill + +This library relies on runtime implementations of +[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) +and +[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) +([Structured Data](https://tc39.es/ecma262/multipage/structured-data.html#sec-structured-data)), +and +[`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) +([Indexed Collections](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-indexed-collections)). + +If you want to be sure that those implementations are available in your target +runtime environment, you might want to consider using a polyfill such as +[__core-js__ Typed Arrays](https://github.com/zloirock/core-js#ecmascript-typed-arrays). + +## Related packages + +These are some other packages which provide more `ArrayBuffer` features. + +### Reading and Writing + +* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class) +* [__dynamic-buffers__](https://pursuit.purescript.org/packages/purescript-dynamic-buffers) +* [__parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview) +* [__arraybuffer-builder__](https://pursuit.purescript.org/packages/purescript-arraybuffer-builder) + +### Node.js + +* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer) + +### UTF + +* [__web-encoding__](https://pursuit.purescript.org/packages/purescript-web-encoding) + +### Base64 + +* [__base64-codec__](https://pursuit.purescript.org/packages/purescript-base64-codec) + +## Development + +Run the tests with + +```sh +spago test +``` diff --git a/bower.json b/bower.json deleted file mode 100644 index 783e0a8..0000000 --- a/bower.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "purescript-arraybuffer", - "license": "MIT", - "repository": { - "type": "git", - "url": "git://github.com/jacereda/purescript-arraybuffer.git" - }, - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "output" - ], - "dependencies": { - "purescript-functions": "^4.0.0", - "purescript-arraybuffer-types": "^2.0.0", - "purescript-maybe": "^4.0.0", - "purescript-effect": "^2.0.0", - "purescript-nullable": "^4.1.0", - "purescript-typelevel": "^4.0.0", - "purescript-parseint": "^1.1.0", - "purescript-uint": "^5.1.0", - "purescript-sized-vectors": "^3.1.0" - }, - "devDependencies": { - "purescript-debug": "^4.0.0", - "purescript-quickcheck": "^5.0.0", - "purescript-partial": "^2.0.0", - "purescript-quickcheck-combinators": "^0.1.0" - } -} diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..2e2f3dd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,3 @@ +# Arraybuffer Documentation + +This directory contains documentation for `arraybuffer`. If you are interested in contributing new documentation, please read the [contributor guidelines](../CONTRIBUTING.md) and [What Nobody Tells You About Documentation](https://documentation.divio.com) for help getting started. diff --git a/package.json b/package.json deleted file mode 100644 index 110d9f1..0000000 --- a/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "purescript-arraybuffer", - "version": "9.0.0", - "main": "index.js", - "repository": "git@github.com:jacereda/purescript-arraybuffer.git", - "author": "https://github.com/jacereda", - "license": "MIT", - "devDependencies": {} -} \ No newline at end of file diff --git a/spago.lock b/spago.lock new file mode 100644 index 0000000..b2ce779 --- /dev/null +++ b/spago.lock @@ -0,0 +1,1309 @@ +{ + "workspace": { + "packages": { + "arraybuffer": { + "path": "./", + "core": { + "dependencies": [ + { + "arraybuffer-types": ">=3.0.2 <4.0.0" + }, + { + "arrays": ">=7.2.0 <8.0.0" + }, + { + "effect": ">=4.0.0 <5.0.0" + }, + { + "float32": ">=2.0.0 <3.0.0" + }, + { + "functions": ">=6.0.0 <7.0.0" + }, + { + "gen": ">=4.0.0 <5.0.0" + }, + { + "maybe": ">=6.0.0 <7.0.0" + }, + { + "nullable": ">=6.0.0 <7.0.0" + }, + { + "prelude": ">=6.0.1 <7.0.0" + }, + { + "tailrec": ">=6.1.0 <7.0.0" + }, + { + "uint": ">=7.0.0 <8.0.0" + }, + { + "unfoldable": ">=6.0.0 <7.0.0" + } + ] + }, + "test": { + "dependencies": [ + "console", + "foldable-traversable", + "partial", + "quickcheck", + "quickcheck-laws", + "refs", + "tuples" + ] + } + } + }, + "package_set": { + "address": { + "registry": "81.4.0" + }, + "compiler": ">=0.15.15 <0.16.0", + "content": { + "abc-parser": "2.1.0", + "ace": "9.1.0", + "address-rfc2821": "0.1.1", + "aff": "8.0.0", + "aff-bus": "6.0.0", + "aff-coroutines": "9.0.0", + "aff-promise": "4.0.0", + "aff-retry": "2.0.0", + "affjax": "13.0.0", + "affjax-node": "1.0.0", + "affjax-web": "1.0.0", + "ansi": "7.0.0", + "apexcharts": "0.5.0", + "applicative-phases": "1.0.0", + "argonaut": "9.0.0", + "argonaut-aeson-generic": "0.4.1", + "argonaut-codecs": "9.1.0", + "argonaut-core": "7.0.0", + "argonaut-generic": "8.0.0", + "argonaut-traversals": "10.0.0", + "argparse-basic": "2.0.0", + "array-builder": "0.1.2", + "array-search": "0.6.0", + "arraybuffer": "13.2.0", + "arraybuffer-builder": "3.1.0", + "arraybuffer-types": "3.0.2", + "arrays": "7.3.0", + "arrays-extra": "0.6.2", + "arrays-zipper": "2.0.1", + "ask": "1.0.0", + "assert": "6.0.0", + "assert-multiple": "0.4.0", + "avar": "5.0.1", + "axon": "0.0.3", + "b64": "0.0.9", + "barbies": "1.0.1", + "barlow-lens": "1.0.0", + "baskerville-core": "0.1.3", + "baskerville-mycroft": "0.1.0", + "benchlib": "0.0.4", + "bifunctors": "6.1.0", + "bigints": "7.0.1", + "blessed": "1.0.0", + "bolson": "0.3.9", + "bookhound": "0.1.7", + "bower-json": "3.0.0", + "bytestrings": "9.0.0", + "call-by-name": "4.0.1", + "canvas": "6.0.0", + "canvas-action": "9.0.0", + "cartesian": "1.0.6", + "catenable-lists": "7.0.0", + "cbor-stream": "1.3.0", + "chameleon": "1.0.0", + "chameleon-halogen": "1.0.3", + "chameleon-react-basic": "1.1.0", + "chameleon-styled": "2.5.0", + "chameleon-transformers": "1.0.0", + "channel": "1.0.0", + "chartjs": "0.2.0", + "chartjs-halogen": "0.2.0", + "checked-exceptions": "3.1.1", + "choku": "1.0.2", + "classless": "0.1.1", + "classless-arbitrary": "0.1.1", + "classless-decode-json": "0.1.1", + "classless-encode-json": "0.1.3", + "classnames": "2.0.0", + "codec": "6.1.0", + "codec-argonaut": "10.0.0", + "codec-json": "2.0.0", + "colors": "7.0.1", + "compile-fail": "0.4.0", + "concur-core": "0.5.0", + "concur-react": "0.5.0", + "concurrent-queues": "3.0.0", + "console": "6.1.0", + "const": "6.0.0", + "contravariant": "6.0.0", + "control": "6.0.0", + "convertable-options": "1.0.0", + "coroutines": "7.0.0", + "css": "6.0.0", + "css-class-name-extractor": "0.0.4", + "css-frameworks": "1.1.0", + "csv-stream": "2.3.0", + "currency": "0.1.0", + "data-mvc": "0.0.2", + "datetime": "6.1.0", + "datetime-parsing": "0.2.0", + "debounce": "0.1.2", + "debug": "6.0.2", + "decimals": "7.1.0", + "default-values": "1.0.1", + "deku": "0.9.24", + "deno": "0.0.5", + "dissect": "1.0.0", + "distributive": "6.0.0", + "dodo-printer": "2.2.3", + "dom-filereader": "7.0.0", + "dom-indexed": "13.0.0", + "dom-simple": "0.4.0", + "dotenv": "4.0.3", + "droplet": "0.6.0", + "dts": "1.0.0", + "dual-numbers": "1.0.3", + "dynamic-buffer": "3.0.1", + "echarts-simple": "0.0.1", + "effect": "4.0.0", + "either": "6.1.0", + "elmish": "0.16.0", + "elmish-enzyme": "0.1.1", + "elmish-hooks": "0.12.0", + "elmish-html": "0.14.0", + "elmish-testing-library": "0.5.0", + "email-validate": "7.0.0", + "encoding": "0.0.10", + "enums": "6.0.1", + "env-names": "0.4.0", + "environment": "2.0.0", + "error": "2.0.0", + "eta-conversion": "0.3.2", + "exceptions": "6.1.0", + "exists": "6.0.0", + "exitcodes": "4.0.0", + "expect-inferred": "3.0.0", + "express": "0.9.1", + "ezfetch": "1.1.1", + "fahrtwind": "2.0.0", + "faker-ffi": "0.1.1", + "fakerjs": "0.0.1", + "fallback": "0.1.0", + "fast-vect": "1.2.0", + "fetch": "4.1.0", + "fetch-argonaut": "1.0.1", + "fetch-core": "5.1.0", + "fetch-yoga-json": "1.1.0", + "ffi-simple": "0.5.1", + "fft": "0.3.0", + "fft-js": "0.1.0", + "filterable": "5.0.0", + "fix-functor": "0.1.0", + "fixed-points": "7.0.0", + "fixed-precision": "5.0.0", + "flame": "1.6.0", + "float32": "2.0.0", + "fmt": "0.2.1", + "foldable-traversable": "6.0.0", + "foldable-traversable-extra": "0.0.6", + "foreign": "7.0.0", + "foreign-object": "4.1.0", + "foreign-readwrite": "3.4.0", + "forgetmenot": "0.1.0", + "fork": "6.0.0", + "form-urlencoded": "7.0.0", + "formatters": "7.0.0", + "framer-motion": "3.1.0", + "free": "7.1.0", + "freeap": "7.0.0", + "freer-free": "0.0.1", + "freet": "7.0.0", + "functions": "6.0.0", + "functor1": "3.0.0", + "functors": "5.0.0", + "fuzzy": "0.4.0", + "gen": "4.0.0", + "generate-values": "1.0.1", + "generic-router": "0.0.1", + "geojson": "0.0.5", + "geometria": "2.2.0", + "gesso": "1.0.1", + "gojs": "0.1.1", + "golden-test": "0.1.0", + "golem-fetch": "0.1.0", + "grain": "3.0.0", + "grain-router": "3.0.0", + "grain-virtualized": "3.0.0", + "graphql-client": "10.1.1", + "graphs": "8.1.0", + "group": "4.1.1", + "halogen": "7.0.0", + "halogen-bootstrap5": "5.3.2", + "halogen-canvas": "1.0.0", + "halogen-css": "10.0.0", + "halogen-declarative-canvas": "0.0.8", + "halogen-echarts-simple": "0.0.4", + "halogen-formless": "4.0.3", + "halogen-helix": "1.1.0", + "halogen-hooks": "0.6.3", + "halogen-hooks-extra": "0.9.0", + "halogen-infinite-scroll": "1.1.0", + "halogen-store": "0.5.4", + "halogen-storybook": "2.0.0", + "halogen-subscriptions": "2.0.0", + "halogen-svg-elems": "8.0.0", + "halogen-typewriter": "1.0.4", + "halogen-use-trigger-hooks": "1.0.0", + "halogen-vdom": "8.0.0", + "halogen-vdom-string-renderer": "0.5.0", + "halogen-widgets": "0.3.1", + "halogen-xterm": "2.0.0", + "harmonia": "0.2.0", + "heckin": "2.0.1", + "heterogeneous": "0.7.0", + "homogeneous": "0.4.0", + "http-methods": "6.1.0", + "httpurple": "4.0.0", + "huffman": "0.4.0", + "humdrum": "0.0.1", + "hylograph-canvas": "0.1.0", + "hylograph-components": "0.1.0", + "hylograph-d3-kernel": "0.1.0", + "hylograph-graph": "0.3.0", + "hylograph-layout": "0.3.0", + "hylograph-music": "0.4.0", + "hylograph-optics": "0.1.0", + "hylograph-selection": "0.5.3", + "hylograph-simulation": "0.6.0", + "hylograph-simulation-core": "0.1.0", + "hylograph-simulation-halogen": "0.5.1", + "hylograph-transitions": "0.1.0", + "hylograph-wasm-kernel": "0.1.0", + "hyrule": "2.3.9", + "i18next": "0.1.0", + "idb": "0.2.0", + "identity": "6.0.0", + "identy": "4.0.1", + "indexed-db": "1.0.0", + "indexed-monad": "3.0.0", + "ink": "0.0.1", + "int64": "3.0.0", + "integers": "6.0.0", + "interpolate": "5.0.2", + "intersection-observer": "1.0.1", + "invariant": "6.0.0", + "jarilo": "1.0.1", + "jelly": "0.10.0", + "jelly-router": "0.3.0", + "jelly-signal": "0.4.0", + "jest": "1.0.0", + "js-abort-controller": "1.0.0", + "js-bigints": "2.2.1", + "js-date": "8.0.0", + "js-fetch": "0.2.1", + "js-fileio": "3.0.0", + "js-intl": "1.3.0", + "js-iterators": "0.1.1", + "js-maps": "0.1.2", + "js-promise": "1.0.0", + "js-promise-aff": "1.0.0", + "js-temporal": "3.0.0", + "js-timers": "6.1.0", + "js-uri": "3.1.0", + "jsdom": "1.0.0", + "jsinc": "0.5.1", + "json": "1.1.0", + "json-codecs": "5.0.0", + "justifill": "0.5.0", + "jwt": "0.0.9", + "labeled-data": "0.2.0", + "language-cst-parser": "0.14.1", + "lazy": "6.0.0", + "lazy-joe": "1.0.0", + "lcg": "4.0.0", + "leibniz": "5.0.0", + "leveldb": "1.0.1", + "liminal": "1.0.1", + "linalg": "6.0.0", + "linear": "0.1.0", + "lists": "7.0.0", + "literals": "1.0.2", + "logging": "3.0.0", + "logging-journald": "0.4.0", + "lumi-components": "18.0.0", + "machines": "7.0.0", + "maps-eager": "0.5.0", + "marionette": "1.0.0", + "marionette-react-basic-hooks": "0.1.1", + "markdown-it-js": "0.1.0", + "marked": "0.1.0", + "matrices": "5.0.1", + "matryoshka": "1.0.0", + "maybe": "6.0.0", + "media-types": "6.0.0", + "meowclient": "1.0.0", + "midi": "4.0.0", + "milkdown": "0.1.0", + "milkis": "9.0.0", + "mimetype": "0.0.1", + "minibench": "4.0.1", + "mmorph": "7.0.0", + "monad-control": "5.0.0", + "monad-logger": "1.3.1", + "monad-loops": "0.5.0", + "monad-unlift": "1.0.1", + "monoid-extras": "0.0.1", + "monoidal": "0.16.0", + "mote": "3.0.0", + "motion": "4.0.0", + "motsunabe": "2.0.0", + "mvc": "0.0.1", + "mycroft": "0.1.1", + "mycroft-z3-wasm": "0.1.0", + "mysql": "6.0.1", + "n3": "0.1.0", + "nano-id": "1.1.0", + "nanoid": "0.1.0", + "naturals": "3.0.0", + "nested-functor": "0.2.1", + "newtype": "5.0.0", + "next-purs-rsc": "0.1.0", + "nextjs": "0.1.1", + "nextui": "0.2.0", + "node-buffer": "9.0.0", + "node-child-process": "11.1.0", + "node-event-emitter": "3.0.0", + "node-execa": "5.0.0", + "node-fs": "9.2.0", + "node-glob-basic": "2.0.0", + "node-http": "9.1.0", + "node-http2": "1.1.1", + "node-human-signals": "1.0.0", + "node-net": "5.1.0", + "node-os": "5.1.0", + "node-path": "5.0.1", + "node-process": "11.2.0", + "node-readline": "8.1.1", + "node-sqlite": "0.2.0", + "node-sqlite3": "8.0.0", + "node-stream-pipes": "2.1.6", + "node-streams": "9.0.1", + "node-tls": "0.3.1", + "node-url": "7.0.1", + "node-workerbees": "0.3.1", + "node-zlib": "0.4.0", + "nonempty": "7.0.0", + "now": "6.0.0", + "npm-package-json": "2.0.0", + "nqueens": "0.1.4", + "nullable": "6.0.0", + "numberfield": "0.2.2", + "numbers": "9.0.1", + "oak": "3.1.1", + "oak-debug": "1.2.2", + "object-maps": "0.3.0", + "ocarina": "1.5.4", + "oooooooooorrrrrrrmm-lib": "0.0.1", + "open-colors-scales-and-schemes": "1.0.0", + "open-drawing": "6.0.4", + "open-folds": "6.4.0", + "open-foreign-generic": "11.0.3", + "open-memoize": "6.2.0", + "open-mkdirp-aff": "1.2.0", + "open-pairing": "6.2.0", + "open-smolder": "12.0.2", + "options": "7.0.0", + "optparse": "5.0.1", + "ordered-collections": "3.2.0", + "ordered-set": "0.5.2", + "orders": "6.0.0", + "org-doc": "0.1.0", + "owoify": "1.2.0", + "pairs": "9.0.1", + "parallel": "7.0.0", + "parsing": "11.0.0", + "parsing-dataview": "3.2.4", + "partial": "4.0.0", + "pathy": "9.0.0", + "pha": "0.13.0", + "phaser": "0.7.0", + "phylio": "1.1.2", + "pipes": "8.0.0", + "pirates-charm": "0.0.1", + "play": "1.0.0", + "pmock": "1.1.0", + "point-free": "1.0.0", + "pointed-list": "0.5.1", + "polymorphic-vectors": "4.0.0", + "posix-types": "6.0.0", + "postgresql": "2.0.21", + "precise": "6.0.0", + "precise-datetime": "7.0.0", + "prelude": "6.0.2", + "prettier-printer": "3.0.0", + "printf": "0.1.0", + "priority-queue": "0.1.2", + "profunctor": "6.0.1", + "profunctor-lenses": "8.0.0", + "prospero": "0.1.0", + "protobuf": "4.4.0", + "ps-spa": "0.7.1", + "psa-utils": "8.0.0", + "psci-support": "6.0.0", + "punycode": "1.0.0", + "pursfmt": "0.17.0", + "qualified-do": "2.2.0", + "quantities": "12.2.0", + "quickcheck": "8.0.1", + "quickcheck-combinators": "0.1.3", + "quickcheck-laws": "7.0.0", + "quickcheck-utf8": "0.0.0", + "random": "6.0.0", + "rationals": "6.0.0", + "rdf": "0.1.0", + "react": "11.0.0", + "react-aria": "0.2.0", + "react-basic": "17.0.0", + "react-basic-classic": "3.0.0", + "react-basic-dnd": "10.1.0", + "react-basic-dom": "7.0.0", + "react-basic-dom-beta": "0.1.1", + "react-basic-emotion": "7.1.0", + "react-basic-hooks": "9.1.1", + "react-basic-storybook": "2.0.0", + "react-dnd-kit": "0.6.0", + "react-dom": "8.0.0", + "react-halo": "3.0.0", + "react-icons": "1.1.6", + "react-markdown": "0.1.0", + "react-testing-library": "4.0.1", + "react-virtuoso": "1.0.0", + "reactix": "0.6.1", + "read": "1.0.1", + "recharts": "1.1.0", + "record": "4.0.0", + "record-extra": "5.0.1", + "record-extra-srghma": "0.2.8", + "record-ptional-fields": "0.1.2", + "record-studio": "1.0.4", + "ref-lifted": "0.0.2", + "refs": "6.0.0", + "remotedata": "5.0.1", + "repr": "0.5.0", + "resize-arrays": "0.0.1", + "resize-observer": "1.0.0", + "resource": "2.0.1", + "resourcet": "1.0.0", + "result": "1.0.3", + "return": "0.2.0", + "ring-modules": "5.0.1", + "rito": "0.3.4", + "roman": "0.4.0", + "rough-notation": "1.0.2", + "routing": "11.0.0", + "routing-duplex": "0.7.0", + "run": "5.0.0", + "safe-coerce": "2.0.0", + "safely": "4.0.1", + "school-of-music": "1.3.0", + "selection-foldable": "0.2.0", + "selective-functors": "1.0.1", + "semirings": "7.0.0", + "seowebchecker": "1.0.4", + "shuffle": "2.0.0", + "sigil": "0.3.0", + "sigil-hats": "0.3.0", + "signal": "13.0.0", + "simple-emitter": "3.0.1", + "simple-i18n": "2.0.1", + "simple-json": "9.0.0", + "simple-json-generics": "0.2.1", + "simple-ulid": "3.0.0", + "sized-matrices": "1.0.0", + "sized-vectors": "5.0.2", + "slug": "3.2.0", + "small-ffi": "4.0.1", + "soundfonts": "4.1.0", + "sparse-matrices": "2.0.1", + "sparse-polynomials": "3.0.1", + "spec": "8.1.2", + "spec-discovery": "8.4.1", + "spec-mocha": "5.1.1", + "spec-node": "0.0.3", + "spec-quickcheck": "5.0.2", + "spec-reporter-xunit": "0.7.1", + "splitmix": "2.1.0", + "ssrs": "1.0.0", + "st": "6.2.0", + "statistics": "0.3.2", + "strictlypositiveint": "1.0.1", + "string-parsers": "8.0.0", + "strings": "6.0.1", + "strings-extra": "4.0.0", + "stringutils": "0.0.13", + "structured-logging": "1.0.0", + "substitute": "0.2.3", + "supabase": "0.1.0", + "supply": "0.2.0", + "svg-parser": "3.0.0", + "systemd-journald": "0.3.0", + "tagged": "4.0.2", + "tailrec": "6.1.0", + "tanstack-query": "3.0.0", + "tecton": "0.2.1", + "tecton-halogen": "0.2.0", + "temporal": "0.2.0", + "test-unit": "17.0.0", + "text-formatting": "0.1.0", + "thermite": "6.3.1", + "thermite-dom": "0.3.1", + "these": "6.0.0", + "threading": "0.0.3", + "tidy": "0.11.1", + "tidy-codegen": "4.0.1", + "tldr": "0.0.0", + "toestand": "0.9.0", + "tramaj-halogen": "0.4.1", + "tramaj-purs": "0.4.1", + "transformation-matrix": "1.0.1", + "transformers": "6.1.0", + "transit": "1.0.0", + "tree-rose": "4.0.2", + "trivial-unfold": "0.5.0", + "ts-bridge": "4.0.0", + "tuples": "7.0.0", + "two-or-more": "1.0.0", + "type-equality": "4.0.1", + "typedenv": "2.0.1", + "typelevel": "6.0.0", + "typelevel-lists": "2.1.0", + "typelevel-peano": "1.0.1", + "typelevel-prelude": "7.0.0", + "typelevel-regex": "0.0.3", + "typelevel-rows": "0.1.0", + "typisch": "0.4.0", + "uint": "7.0.0", + "ulid": "3.0.1", + "uncurried-transformers": "1.1.0", + "undefined": "2.0.0", + "undefined-is-not-a-problem": "1.1.0", + "unfoldable": "6.0.0", + "unicode": "6.0.0", + "unique": "0.6.1", + "unlift": "1.0.1", + "unordered-collections": "3.1.0", + "unsafe-coerce": "6.0.0", + "unsafe-reference": "5.0.0", + "untagged-to-tagged": "0.1.4", + "untagged-union": "1.0.0", + "uri": "9.0.0", + "url-immutable": "1.0.0", + "url-regex-safe": "0.1.1", + "uuid": "9.0.0", + "uuidv4": "1.0.0", + "validation": "6.0.0", + "variant": "8.0.0", + "variant-encodings": "2.0.0", + "variant-gen": "1.0.0", + "vectorfield": "1.0.1", + "vectors": "2.1.0", + "versions": "7.0.0", + "visx": "0.0.2", + "vitest": "2.0.1", + "wasm-base": "0.2.0", + "web-chain": "1.0.1", + "web-clipboard": "6.0.0", + "web-cssom": "2.0.0", + "web-cssom-view": "0.1.0", + "web-dom": "6.0.0", + "web-dom-parser": "8.0.0", + "web-dom-xpath": "3.0.0", + "web-encoding": "3.0.0", + "web-events": "4.0.0", + "web-fetch": "4.0.1", + "web-file": "4.0.0", + "web-geometry": "0.1.0", + "web-html": "4.1.1", + "web-pointerevents": "2.0.0", + "web-proletarian": "1.0.0", + "web-promise": "3.2.0", + "web-resize-observer": "2.1.0", + "web-router": "1.0.0", + "web-socket": "4.0.0", + "web-storage": "5.0.0", + "web-streams": "4.0.0", + "web-touchevents": "4.0.0", + "web-uievents": "5.0.0", + "web-url": "2.0.0", + "web-workers": "2.0.0", + "web-xhr": "5.0.1", + "webb-aff-list": "0.0.2", + "webb-array": "0.0.2", + "webb-channel": "0.0.3", + "webb-commandline": "0.0.3", + "webb-directory": "0.0.7", + "webb-file": "0.0.1", + "webb-map": "0.0.1", + "webb-monad": "0.0.2", + "webb-mutex": "0.0.7", + "webb-parsing": "0.0.5", + "webb-random": "0.0.1", + "webb-refer": "0.0.3", + "webb-set": "0.0.1", + "webb-slot": "0.0.1", + "webb-stateful": "0.0.3", + "webb-string": "0.0.1", + "webb-test": "0.0.1", + "webb-thread": "0.0.2", + "webb-writer": "0.0.3", + "webextension-polyfill": "0.1.0", + "webgpu": "0.0.1", + "which": "2.0.0", + "whine-core": "0.0.34", + "xterm": "1.0.0", + "yaml-next": "3.1.1", + "yoga-acp-om": "0.1.1", + "yoga-better-auth": "0.2.2", + "yoga-bun-yoga": "0.2.1", + "yoga-config": "0.2.1", + "yoga-country": "3.0.0", + "yoga-docker-compose": "0.1.1", + "yoga-dynamodb": "0.1.1", + "yoga-elasticsearch": "0.1.1", + "yoga-fastify": "0.5.4", + "yoga-fastify-om": "0.4.7", + "yoga-fetch": "1.0.1", + "yoga-fetch-om": "0.8.0", + "yoga-format": "1.0.0", + "yoga-heroui": "2.0.3", + "yoga-http-api": "0.3.4", + "yoga-jaeger": "0.1.1", + "yoga-json": "5.2.1", + "yoga-language": "1.0.0", + "yoga-next-fastify": "0.2.0", + "yoga-om": "2.2.1", + "yoga-om-layer": "2.1.0", + "yoga-om-strom": "0.4.3", + "yoga-om-workerbees": "0.1.2", + "yoga-opentelemetry": "0.2.0", + "yoga-options": "0.1.1", + "yoga-pino": "0.1.1", + "yoga-postgres": "6.0.0", + "yoga-react": "1.0.2", + "yoga-react-dom": "2.0.1", + "yoga-react-native": "0.2.1", + "yoga-react-om": "0.2.0", + "yoga-redis": "0.1.1", + "yoga-scylladb": "0.1.1", + "yoga-shadcn": "1.0.5", + "yoga-sql-types": "0.2.0", + "yoga-sqlite": "0.3.4", + "yoga-sqlite-om": "0.3.0", + "yoga-sse": "0.1.1", + "yoga-subtlecrypto": "0.1.0", + "yoga-test-docker": "0.1.2", + "yoga-timezone": "1.0.0", + "yoga-tree": "1.0.0", + "yoga-tree-svg": "0.1.0", + "yoga-tree-utils": "1.0.0", + "z3": "0.0.2", + "zipperarray": "2.0.0" + } + }, + "extra_packages": {} + }, + "packages": { + "arraybuffer-types": { + "type": "registry", + "version": "3.0.2", + "integrity": "sha256-p05cJnSkyeoB7VHzMyc2Eb1RwUaB7Nl0sQSqEGNEUD8=", + "dependencies": [] + }, + "arrays": { + "type": "registry", + "version": "7.3.0", + "integrity": "sha256-GD4z7LCi9wzi7FCQqbWhvs8paoUK9NO+HwgXZ+KP8Rk=", + "dependencies": [ + "bifunctors", + "control", + "foldable-traversable", + "functions", + "maybe", + "nonempty", + "partial", + "prelude", + "safe-coerce", + "st", + "tailrec", + "tuples", + "unfoldable", + "unsafe-coerce" + ] + }, + "bifunctors": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-CvZRb8uI75eKIqiYntR+k05Wk6tKlMS5w8sCA2AFna0=", + "dependencies": [ + "const", + "either", + "newtype", + "prelude", + "tuples" + ] + }, + "console": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-3T5PPz1mATZmV0C4GzkCkpLlCa5xG7u8ZNgnLrrrI1Q=", + "dependencies": [ + "effect", + "prelude" + ] + }, + "const": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-S+m0lQsxnji0z1XRUmkn+3RFuWJ5CXWuIr1u3i8Bfoc=", + "dependencies": [ + "invariant", + "newtype", + "prelude" + ] + }, + "contravariant": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-nuckEWqL80NaPmR/l9AUyrfycNh18XcvlZV9PAamwqc=", + "dependencies": [ + "const", + "either", + "newtype", + "prelude", + "tuples" + ] + }, + "control": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-0vxyYh5FL+ilxgLo954w1LqCUC0RSIivNNlb9CaNUQg=", + "dependencies": [ + "newtype", + "prelude" + ] + }, + "distributive": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-netyJK4B32PDYHD0tF2AenKgo3urzbI770ycM0pArAo=", + "dependencies": [ + "identity", + "newtype", + "prelude", + "tuples", + "type-equality" + ] + }, + "effect": { + "type": "registry", + "version": "4.0.0", + "integrity": "sha256-DdqU3bncRTjgMVPPRlAuGHVeajSPEcRaRhNuio7if6s=", + "dependencies": [ + "prelude" + ] + }, + "either": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-tBHx3PgtH4GZOApZCDkcM7szRTYJinrUrVGWazQCUUg=", + "dependencies": [ + "control", + "invariant", + "maybe", + "prelude" + ] + }, + "enums": { + "type": "registry", + "version": "6.0.1", + "integrity": "sha256-sdZOmLX5+5pASGpvVyT0vSD5BBYQjZiayjV5XiPutso=", + "dependencies": [ + "control", + "either", + "gen", + "maybe", + "newtype", + "nonempty", + "partial", + "prelude", + "tuples", + "unfoldable" + ] + }, + "exceptions": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-nRrr+N0wk0TUFOWLR7e1n1oxcXo/X345bQUtoY9lW6A=", + "dependencies": [ + "effect", + "either", + "maybe", + "prelude" + ] + }, + "exists": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-vtLbrWNaI+pzx/2fw2AQnCovoLtcosClIhjO26QKkh8=", + "dependencies": [ + "unsafe-coerce" + ] + }, + "float32": { + "type": "registry", + "version": "2.0.0", + "integrity": "sha256-lO8pshPBj5ExM+/ZjZ8nXVltb37XGJdRP8vakl0LBTE=", + "dependencies": [ + "gen", + "maybe", + "prelude" + ] + }, + "foldable-traversable": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-NGGWyCio/xjce6fPP7y3wwg7CheqllID6aJudDxbWhA=", + "dependencies": [ + "bifunctors", + "const", + "control", + "either", + "functors", + "identity", + "maybe", + "newtype", + "orders", + "prelude", + "tuples" + ] + }, + "functions": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-0jgtOr+SFO4UScKc40oA/9Vdcf2rLn3h3vZlY0KfqVo=", + "dependencies": [ + "prelude" + ] + }, + "functors": { + "type": "registry", + "version": "5.0.0", + "integrity": "sha256-W1o/4wcp6S3chodYL8uixlXK2mfr1L+SzM1R+whqUco=", + "dependencies": [ + "bifunctors", + "const", + "contravariant", + "control", + "distributive", + "either", + "invariant", + "maybe", + "newtype", + "prelude", + "profunctor", + "tuples", + "unsafe-coerce" + ] + }, + "gen": { + "type": "registry", + "version": "4.0.0", + "integrity": "sha256-kyOateeOtQa07vfTMYeSuCKdelgKE1R65fwKkZ10wsY=", + "dependencies": [ + "either", + "foldable-traversable", + "identity", + "maybe", + "newtype", + "nonempty", + "prelude", + "tailrec", + "tuples", + "unfoldable" + ] + }, + "identity": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-RY/iXPpxpqvKHYfJeFVNI1J06kohB9rbtWYuha8t0LE=", + "dependencies": [ + "control", + "invariant", + "newtype", + "prelude" + ] + }, + "integers": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-Y8yozC1uHRLcruobEyv5/bMKzE1BoKJ/olHv6o+bgm8=", + "dependencies": [ + "maybe", + "numbers", + "prelude" + ] + }, + "invariant": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-HyoT1I5qIHRq8RSZji6zZDvqnpVjLw748utrKwZtqsw=", + "dependencies": [ + "control", + "prelude" + ] + }, + "lazy": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-MHRC+1Pc+AjortBp4b/e1e6KpEpsDwaJBNBB7TOL+1I=", + "dependencies": [ + "control", + "foldable-traversable", + "invariant", + "prelude" + ] + }, + "lcg": { + "type": "registry", + "version": "4.0.0", + "integrity": "sha256-bUYyfXYmjTn7zSpj2hTiePywzsuD80fCjkonsrpdGOw=", + "dependencies": [ + "effect", + "integers", + "maybe", + "numbers", + "partial", + "prelude", + "random" + ] + }, + "lists": { + "type": "registry", + "version": "7.0.0", + "integrity": "sha256-/mGd7wLoU+wsTcqii7SSUByxsvwvjnu2PjwAD47yYb4=", + "dependencies": [ + "bifunctors", + "control", + "foldable-traversable", + "lazy", + "maybe", + "newtype", + "nonempty", + "partial", + "prelude", + "tailrec", + "tuples", + "unfoldable" + ] + }, + "maybe": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-8X6XEgtZ5lqfkEuc1hWOofnL6mwhk8gSJTxwvbR/SbY=", + "dependencies": [ + "control", + "invariant", + "newtype", + "prelude" + ] + }, + "newtype": { + "type": "registry", + "version": "5.0.0", + "integrity": "sha256-dt6w0cty4OS+Pjt9fsh3iU6ottqSU83mdAZkaEEvo6c=", + "dependencies": [ + "prelude", + "safe-coerce" + ] + }, + "nonempty": { + "type": "registry", + "version": "7.0.0", + "integrity": "sha256-Ctkq8/+KiGqCE53Y/LvibA/coy7ev9HtRZU+GYS7yfQ=", + "dependencies": [ + "control", + "foldable-traversable", + "maybe", + "prelude", + "tuples", + "unfoldable" + ] + }, + "nullable": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-CDAZk+L9Sc0GCFTkE8n+qdp4Ys8avvnkU+m2hVALt7M=", + "dependencies": [ + "functions", + "maybe", + "prelude" + ] + }, + "numbers": { + "type": "registry", + "version": "9.0.1", + "integrity": "sha256-ZydYckgwRAnRaFC7pu6fADhzdwX+UqHZrfJyet64zls=", + "dependencies": [ + "functions", + "maybe", + "prelude" + ] + }, + "orders": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-nSMxZK7wxyDULkjZqvMyB71mVo6b0AyLLHosDVbv1XM=", + "dependencies": [ + "newtype", + "prelude" + ] + }, + "partial": { + "type": "registry", + "version": "4.0.0", + "integrity": "sha256-mdrlJBAgFMh79hNRW39uv7c+BwnnaOrAMmdh7+VhEhs=", + "dependencies": [] + }, + "prelude": { + "type": "registry", + "version": "6.0.2", + "integrity": "sha256-IqykrDRquGUD/LRSad7y75hZv3bUsGQ4knc8tdez8Qw=", + "dependencies": [] + }, + "profunctor": { + "type": "registry", + "version": "6.0.1", + "integrity": "sha256-Ow0mJC+PIIRq/a3mzCWY5vxuTgR/e+Ee68+mWeSRnWY=", + "dependencies": [ + "control", + "distributive", + "either", + "exists", + "invariant", + "newtype", + "prelude", + "tuples" + ] + }, + "quickcheck": { + "type": "registry", + "version": "8.0.1", + "integrity": "sha256-SfW+maesCt0jnnFhfrgJZGPcWLj8oFcGbcDIVklmdzc=", + "dependencies": [ + "arrays", + "console", + "control", + "effect", + "either", + "enums", + "exceptions", + "foldable-traversable", + "gen", + "identity", + "integers", + "lazy", + "lcg", + "lists", + "maybe", + "newtype", + "nonempty", + "numbers", + "partial", + "prelude", + "record", + "st", + "strings", + "tailrec", + "transformers", + "tuples" + ] + }, + "quickcheck-laws": { + "type": "registry", + "version": "7.0.0", + "integrity": "sha256-hDd4Q99TNlEz9sU0Cssm7n6+BswAq69HL8KqNP5UKOU=", + "dependencies": [ + "arrays", + "console", + "control", + "effect", + "enums", + "foldable-traversable", + "maybe", + "newtype", + "prelude", + "quickcheck" + ] + }, + "random": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-7sZRo6P9UIiXDZ9s2QL9T8h3wlIwLdQrR4+oZKOijQw=", + "dependencies": [ + "effect", + "integers", + "prelude" + ] + }, + "record": { + "type": "registry", + "version": "4.0.0", + "integrity": "sha256-WQ8fqp4X1wTA70J4qUcPW4Z7DCwx6e5KJnT/EuTdWSc=", + "dependencies": [ + "functions", + "prelude", + "unsafe-coerce" + ] + }, + "refs": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-KKD5G9dR2SauuDGULUJsxjqDOEi1Jqm+2Sq5U2mY8YU=", + "dependencies": [ + "effect", + "prelude" + ] + }, + "safe-coerce": { + "type": "registry", + "version": "2.0.0", + "integrity": "sha256-EJrxtKt5xC7TYjBZrSBO/J7Aw3DmtilYjt4olpAXWVc=", + "dependencies": [ + "unsafe-coerce" + ] + }, + "st": { + "type": "registry", + "version": "6.2.0", + "integrity": "sha256-WI4MEkkwUd4pnwZQ3G8VKWgX5t1RSNJludm5e9boaRo=", + "dependencies": [ + "effect", + "partial", + "prelude", + "tailrec", + "unsafe-coerce" + ] + }, + "strings": { + "type": "registry", + "version": "6.0.1", + "integrity": "sha256-FNto2hoNW5Da6W6crIk77YXMagBvOGHZE1kO7eZ1vLM=", + "dependencies": [ + "arrays", + "control", + "either", + "enums", + "foldable-traversable", + "gen", + "integers", + "maybe", + "newtype", + "nonempty", + "partial", + "prelude", + "tailrec", + "tuples", + "unfoldable", + "unsafe-coerce" + ] + }, + "tailrec": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-xrorCfP0xV1wubs27idQCHQqdvSUuqmmL/am7Ji1wVU=", + "dependencies": [ + "bifunctors", + "effect", + "either", + "identity", + "maybe", + "partial", + "prelude", + "refs" + ] + }, + "transformers": { + "type": "registry", + "version": "6.1.0", + "integrity": "sha256-QJmgNT/y7ljPHSsXaW4zxF/982BFiQ90KNL1g/NtEOQ=", + "dependencies": [ + "control", + "distributive", + "effect", + "either", + "exceptions", + "foldable-traversable", + "identity", + "lazy", + "maybe", + "newtype", + "prelude", + "st", + "tailrec", + "tuples", + "unfoldable" + ] + }, + "tuples": { + "type": "registry", + "version": "7.0.0", + "integrity": "sha256-BKy7EfK8S1KeTCwyWqyW1wwcpSAQuh3IsczSVgS6fzY=", + "dependencies": [ + "control", + "invariant", + "prelude" + ] + }, + "type-equality": { + "type": "registry", + "version": "4.0.1", + "integrity": "sha256-BBqYOSnAKmayRvR5mtZoCf4SlYs2ipatD/+5nQx73aw=", + "dependencies": [] + }, + "uint": { + "type": "registry", + "version": "7.0.0", + "integrity": "sha256-MCTYyDPFLoNLweSY1oEjKKo0xhqCGrAbiBQNdva1B3w=", + "dependencies": [ + "enums", + "gen", + "maybe", + "numbers", + "prelude" + ] + }, + "unfoldable": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-BK/6By1sKp/ztk+9CF3q43SV0QDTLVdsGqFerKHSmbg=", + "dependencies": [ + "foldable-traversable", + "maybe", + "partial", + "prelude", + "tuples" + ] + }, + "unsafe-coerce": { + "type": "registry", + "version": "6.0.0", + "integrity": "sha256-0L1QsaY20OILjfU5TV72d3U/tSjhmL9hJ32WMp857Lk=", + "dependencies": [] + } + } +} diff --git a/spago.yaml b/spago.yaml new file mode 100644 index 0000000..33e6321 --- /dev/null +++ b/spago.yaml @@ -0,0 +1,37 @@ +package: + name: arraybuffer + description: Bindings and implementation for mutable JavaScript ArrayBuffers + publish: + version: 13.2.1 + license: MIT + location: + githubOwner: purescript-contrib + githubRepo: purescript-arraybuffer + build: + strict: true + dependencies: + - arraybuffer-types: ">=3.0.2 <4.0.0" + - arrays: ">=7.2.0 <8.0.0" + - effect: ">=4.0.0 <5.0.0" + - float32: ">=2.0.0 <3.0.0" + - functions: ">=6.0.0 <7.0.0" + - gen: ">=4.0.0 <5.0.0" + - maybe: ">=6.0.0 <7.0.0" + - nullable: ">=6.0.0 <7.0.0" + - prelude: ">=6.0.1 <7.0.0" + - tailrec: ">=6.1.0 <7.0.0" + - uint: ">=7.0.0 <8.0.0" + - unfoldable: ">=6.0.0 <7.0.0" + test: + main: Test.Main + dependencies: + - console + - foldable-traversable + - partial + - refs + - tuples + - quickcheck + - quickcheck-laws +workspace: + packageSet: + registry: 81.4.0 diff --git a/src/Data/ArrayBuffer/ArrayBuffer.js b/src/Data/ArrayBuffer/ArrayBuffer.js index 0952ca2..9bb2fc1 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.js +++ b/src/Data/ArrayBuffer/ArrayBuffer.js @@ -1,15 +1,13 @@ -"use strict"; - // module Data.ArrayBuffer.ArrayBuffer -exports.empty = function empty (s) { +export function emptyImpl(s) { return new ArrayBuffer(s); }; -exports.byteLength = function byteLength (a) { +export function byteLength(a) { return a.byteLength; -}; +} -exports.sliceImpl = function sliceImpl (a, ms, me) { - return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); -}; +export function sliceImpl(a, s, e) { + return a.slice(s, e); +} diff --git a/src/Data/ArrayBuffer/ArrayBuffer.purs b/src/Data/ArrayBuffer/ArrayBuffer.purs index fe8a920..fecb62b 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer.purs @@ -1,6 +1,5 @@ -- | This module represents the functional bindings to JavaScript's `ArrayBuffer` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) for details. - module Data.ArrayBuffer.ArrayBuffer ( empty , byteLength @@ -9,23 +8,20 @@ module Data.ArrayBuffer.ArrayBuffer import Data.ArrayBuffer.Types (ArrayBuffer, ByteOffset, ByteLength) import Data.Function.Uncurried (Fn3, runFn3) -import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, notNull, null) -import Data.Tuple (Tuple(..)) - +import Effect (Effect) +import Effect.Uncurried (EffectFn1, runEffectFn1) -- | Create an `ArrayBuffer` with the given capacity. -foreign import empty :: ByteLength -> ArrayBuffer +empty :: ByteLength -> Effect ArrayBuffer +empty l = runEffectFn1 emptyImpl l + +foreign import emptyImpl :: EffectFn1 ByteLength ArrayBuffer -- | Represents the length of an `ArrayBuffer` in bytes. foreign import byteLength :: ArrayBuffer -> ByteLength -foreign import sliceImpl :: Fn3 ArrayBuffer (Nullable ByteOffset) (Nullable ByteOffset) ArrayBuffer - -- | Returns a new `ArrayBuffer` whose contents are a copy of this ArrayBuffer's bytes from begin, inclusive, up to end, exclusive. -slice :: ArrayBuffer -> Maybe (Tuple ByteOffset (Maybe ByteOffset)) -> ArrayBuffer -slice a mz = case mz of - Nothing -> runFn3 sliceImpl a null null - Just (Tuple s me) -> case me of - Nothing -> runFn3 sliceImpl a (notNull s) null - Just e -> runFn3 sliceImpl a (notNull s) (notNull e) +slice :: ByteOffset -> ByteOffset -> ArrayBuffer -> ArrayBuffer +slice s e a = runFn3 sliceImpl a s e + +foreign import sliceImpl :: Fn3 ArrayBuffer ByteOffset ByteOffset ArrayBuffer diff --git a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs index dbe909a..381931c 100644 --- a/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs +++ b/src/Data/ArrayBuffer/ArrayBuffer/Gen.purs @@ -6,8 +6,5 @@ import Data.ArrayBuffer.Typed.Gen (genTypedArray, genUint8) import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array) import Prelude ((<$>)) - -genArrayBuffer :: forall m - . MonadGen m - => m ArrayBuffer +genArrayBuffer :: forall m. MonadGen m => m ArrayBuffer genArrayBuffer = buffer <$> (genTypedArray genUint8 :: m Uint8Array) diff --git a/src/Data/ArrayBuffer/Cast.purs b/src/Data/ArrayBuffer/Cast.purs new file mode 100644 index 0000000..10e5b0a --- /dev/null +++ b/src/Data/ArrayBuffer/Cast.purs @@ -0,0 +1,46 @@ +-- | `DataView` represents unaligned memory of unknown endianness. +-- | +-- | `ArrayView` represents arrays of aligned elements of +-- | local-machine endianness. +-- | For the cases of `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, +-- | the elements +-- | are single bytes, so they are always aligned and they have no +-- | endianness. Therefore in those cases we can freely cast back and forth +-- | to `DataView`. +module Data.ArrayBuffer.Cast + ( fromInt8Array + , fromUint8Array + , fromUint8ClampedArray + , toInt8Array + , toUint8Array + , toUint8ClampedArray + ) where + +import Data.ArrayBuffer.DataView as DV +import Data.ArrayBuffer.Typed as AT +import Data.ArrayBuffer.Types (DataView, Uint8Array, Uint8ClampedArray, Int8Array) +import Effect (Effect) + +-- | Cast an `Int8Array` to a `DataView`. +fromInt8Array :: Int8Array -> Effect DataView +fromInt8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to an `Int8Array`. +toInt8Array :: DataView -> Effect Int8Array +toInt8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8Array` to a `DataView`. +fromUint8Array :: Uint8Array -> Effect DataView +fromUint8Array x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8Array`. +toUint8Array :: DataView -> Effect Uint8Array +toUint8Array x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) + +-- | Cast a `UInt8ClampedArray` to a `DataView`. +fromUint8ClampedArray :: Uint8ClampedArray -> Effect DataView +fromUint8ClampedArray x = DV.part (AT.buffer x) (AT.byteOffset x) (AT.byteLength x) + +-- | Cast a `DataView` to a `Uint8ClampedArray`. +toUint8ClampedArray :: DataView -> Effect Uint8ClampedArray +toUint8ClampedArray x = AT.part (DV.buffer x) (DV.byteOffset x) (DV.byteLength x) diff --git a/src/Data/ArrayBuffer/DataView.js b/src/Data/ArrayBuffer/DataView.js index d51835c..1eeb060 100644 --- a/src/Data/ArrayBuffer/DataView.js +++ b/src/Data/ArrayBuffer/DataView.js @@ -1,43 +1,41 @@ -"use strict"; - // module Data.ArrayBuffer.DataView -exports.whole = function whole (b) { +export function whole(b) { return new DataView(b); -}; +} -exports.remainderImpl = function remainderImpl (b,i) { +export function remainderImpl(b, i) { return new DataView(b,i); -}; +} -exports.partImpl = function partImpl (b,i,j) { +export function partImpl(b, i, j) { return new DataView(b,i,j); -}; +} -exports.buffer = function buffer (v) { +export function buffer(v) { return v.buffer; -}; +} -exports.byteOffset = function byteOffset (v) { +export function byteOffset(v) { return v.byteOffset; -}; +} -exports.byteLength = function byteLength (v) { +export function byteLength(v) { return v.byteLength; -}; +} -exports.getterImpl = function getterImpl (data, v, o) { +export function getterImpl(data, v, o) { return ((o + data.bytesPerValue) >>> 0) <= v.byteLength - ? data.just (v[data.functionName].call(v,o,data.littleEndian)) - : data.nothing; -}; + ? v[data.functionName].call(v,o,data.littleEndian) + : null; +} -exports.setterImpl = function setterImpl (data,v,n,o) { +export function setterImpl(data, v, o, n) { if (((o + data.bytesPerValue) >>> 0) <= v.byteLength) { v[data.functionName].call(v,o,n,data.littleEndian); return true; } else { return false; } -}; +} diff --git a/src/Data/ArrayBuffer/DataView.purs b/src/Data/ArrayBuffer/DataView.purs index 966b13a..badab60 100644 --- a/src/Data/ArrayBuffer/DataView.purs +++ b/src/Data/ArrayBuffer/DataView.purs @@ -1,45 +1,84 @@ -- | This module represents the functional bindings to JavaScript's `DataView` -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) for details. - module Data.ArrayBuffer.DataView - ( whole - , remainder - , part + ( Endian(..) , buffer - , byteOffset , byteLength - , AProxy (..) - , class DataView - , getBE, getLE, setBE, setLE + , byteOffset + , get + , getBE + , getFloat32be + , getFloat32le + , getFloat64be + , getFloat64le + , getInt16be + , getInt16le + , getInt32be + , getInt32le + , getInt8 + , getLE + , getUint16be + , getUint16le + , getUint32be + , getUint32le + , getUint8 + , part + , remainder + , set + , setBE + , setFloat32be + , setFloat32le + , setFloat64be + , setFloat64le + , setInt16be + , setInt16le + , setInt32be + , setInt32le + , setInt8 + , setLE + , setUint16be + , setUint16le + , setUint32be + , setUint32le + , setUint8 + , whole ) where -import Data.ArrayBuffer.Types (ByteOffset, DataView, ByteLength, ArrayBuffer, kind ArrayViewType, Int32, Int16, Int8, Uint32, Uint16, Uint8, Float32, Float64) -import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) -import Data.Maybe (Maybe(..)) -import Data.Typelevel.Num (toInt', class Nat) +import Data.ArrayBuffer.Types (ArrayBuffer, ByteLength, ByteOffset, DataView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, class ShowArrayViewType, byteWidth) +import Data.Float32 (Float32) as F +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toMaybe) +import Data.Symbol (class IsSymbol, reflectSymbol) import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, runEffectFn2, runEffectFn3, runEffectFn4) +import Prelude (class Eq, (<$>), (<>), (==)) import Type.Proxy (Proxy(..)) +-- | Endianness of a multi-byte type. Little-Endian or Big-Endian. +data Endian = LE | BE - +instance eqEndian :: Eq Endian where + eq LE LE = true + eq BE BE = true + eq _ _ = false -- | View mapping the whole `ArrayBuffer`. foreign import whole :: ArrayBuffer -> DataView -foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView - -- | View mapping the rest of an `ArrayBuffer` after an index. remainder :: ArrayBuffer -> ByteOffset -> Effect DataView remainder a o = runEffectFn2 remainderImpl a o -foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView +foreign import remainderImpl :: EffectFn2 ArrayBuffer ByteOffset DataView -- | View mapping a region of the `ArrayBuffer`. part :: ArrayBuffer -> ByteOffset -> ByteLength -> Effect DataView part a o l = runEffectFn3 partImpl a o l +foreign import partImpl :: EffectFn3 ArrayBuffer ByteOffset ByteLength DataView + -- | `ArrayBuffer` being mapped by the view. foreign import buffer :: DataView -> ArrayBuffer @@ -49,98 +88,266 @@ foreign import byteOffset :: DataView -> ByteOffset -- | Represents the length of this view. foreign import byteLength :: DataView -> ByteLength +getter + :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , littleEndian :: Boolean + } + -> DataView + -> ByteOffset + -> Effect (Maybe t) +getter data' dataView offset = + toMaybe <$> runEffectFn3 getterImpl + { functionName: data'.functionName + , littleEndian: data'.littleEndian + , bytesPerValue: data'.bytesPerValue + } + dataView + offset -data AProxy (a :: ArrayViewType) = AProxy +foreign import getterImpl + :: forall t + . EffectFn3 + { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } + DataView + ByteOffset + (Nullable t) +get + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Endian + -> Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) +get endian prx = do + let + le = endian == LE + pnm = "get" <> reflectSymbol (Proxy :: Proxy name) + bpv = byteWidth prx -class BinaryValue a t <= DataView (a :: ArrayViewType) t | a -> t where - getLE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) - getBE :: AProxy a -> DataView -> ByteOffset -> Effect (Maybe t) - setBE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean - setLE :: AProxy a -> DataView -> t -> ByteOffset -> Effect Boolean + getter + { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } +getBE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) +getBE = get BE -foreign import getterImpl :: forall t - . EffectFn3 { just :: t -> Maybe t - , nothing :: Maybe t - , functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView ByteOffset (Maybe t) +getLE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> Effect (Maybe t) +getLE = get LE -getter :: forall t - . { functionName :: String - , bytesPerValue :: ByteLength - , littleEndian :: Boolean - } - -> DataView -> ByteOffset -> Effect (Maybe t) -getter data' d o = - runEffectFn3 getterImpl - { just: Just - , nothing: Nothing - , functionName: data'.functionName - , littleEndian: data'.littleEndian - , bytesPerValue: data'.bytesPerValue - } d o - -foreign import setterImpl :: forall t - . EffectFn4 { functionName :: String - , littleEndian :: Boolean - , bytesPerValue :: ByteLength - } DataView t ByteOffset Boolean - -setter :: forall t - . { functionName :: String - , bytesPerValue :: ByteLength - , littleEndian :: Boolean - } -> DataView -> t -> ByteOffset -> Effect Boolean -setter d t o = runEffectFn4 setterImpl d t o - - -instance dataViewInt8 :: (BytesPerValue Int8 b, Nat b) => DataView Int8 Int where - getBE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewInt16 :: (BytesPerValue Int16 b, Nat b) => DataView Int16 Int where - getBE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewInt32 :: (BytesPerValue Int32 b, Nat b) => DataView Int32 Int where - getBE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setInt32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint8 :: (BytesPerValue Uint8 b, Nat b) => DataView Uint8 UInt where - getBE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint8", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint16 :: (BytesPerValue Uint16 b, Nat b) => DataView Uint16 UInt where - getBE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint16", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewUint32 :: (BytesPerValue Uint32 b, Nat b) => DataView Uint32 UInt where - getBE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setUint32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewFloat32 :: (BytesPerValue Float32 b, Nat b) => DataView Float32 Number where - getBE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setFloat32", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - -instance dataViewFloat64 :: (BytesPerValue Float64 b, Nat b) => DataView Float64 Number where - getBE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - setBE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: false} - getLE AProxy = getter {functionName: "getFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} - setLE AProxy = setter {functionName: "setFloat64", bytesPerValue: toInt' (Proxy :: Proxy b), littleEndian: true} +setter + :: forall t + . { functionName :: String + , bytesPerValue :: ByteLength + , littleEndian :: Boolean + } + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +setter dataView offset t = runEffectFn4 setterImpl dataView offset t + +foreign import setterImpl + :: forall t + . EffectFn4 + { functionName :: String + , littleEndian :: Boolean + , bytesPerValue :: ByteLength + } + DataView + ByteOffset + t + Boolean + +set + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Endian + -> Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +set endian prx = do + let + le = endian == LE + pnm = "set" <> reflectSymbol (Proxy :: Proxy name) + bpv = byteWidth prx + + setter + { functionName: pnm + , bytesPerValue: bpv + , littleEndian: le + } + +-- | Fetch int8 value at a certain index in a `DataView`. +getInt8 :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt8 = getLE (Proxy :: Proxy Int8) + +-- | Fetch big-endian int16 value at a certain index in a `DataView`. +getInt16be :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt16be = getBE (Proxy :: Proxy Int16) + +-- | Fetch little-endian int16 value at a certain index in a `DataView`. +getInt16le :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt16le = getLE (Proxy :: Proxy Int16) + +-- | Fetch big-endian int32 value at a certain index in a `DataView`. +getInt32be :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt32be = getBE (Proxy :: Proxy Int32) + +-- | Fetch little-endian int32 value at a certain index in a `DataView`. +getInt32le :: DataView -> ByteOffset -> Effect (Maybe Int) +getInt32le = getLE (Proxy :: Proxy Int32) + +-- | Fetch uint8 value at a certain index in a `DataView`. +getUint8 :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint8 = getLE (Proxy :: Proxy Uint8) + +-- | Fetch big-endian uint16 value at a certain index in a `DataView`. +getUint16be :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint16be = getBE (Proxy :: Proxy Uint16) + +-- | Fetch little-endian uint16 value at a certain index in a `DataView`. +getUint16le :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint16le = getLE (Proxy :: Proxy Uint16) + +-- | Fetch big-endian uint32 value at a certain index in a `DataView`. +getUint32be :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint32be = getBE (Proxy :: Proxy Uint32) + +-- | Fetch little-endian uint32 value at a certain index in a `DataView`. +getUint32le :: DataView -> ByteOffset -> Effect (Maybe UInt) +getUint32le = getLE (Proxy :: Proxy Uint32) + +-- | Fetch big-endian float32 value at a certain index in a `DataView`. +getFloat32be :: DataView -> ByteOffset -> Effect (Maybe F.Float32) +getFloat32be = getBE (Proxy :: Proxy Float32) + +-- | Fetch little-endian float32 value at a certain index in a `DataView`. +getFloat32le :: DataView -> ByteOffset -> Effect (Maybe F.Float32) +getFloat32le = getLE (Proxy :: Proxy Float32) + +-- | Fetch big-endian float64 value at a certain index in a `DataView`. +getFloat64be :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat64be = getBE (Proxy :: Proxy Float64) + +-- | Fetch little-endian float64 value at a certain index in a `DataView`. +getFloat64le :: DataView -> ByteOffset -> Effect (Maybe Number) +getFloat64le = getLE (Proxy :: Proxy Float64) + +-- | Store big-endian value at a certain index in a `DataView`. +setBE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +setBE = set BE + +-- | Store little-endian value at a certain index in a `DataView`. +setLE + :: forall a name t + . BinaryValue a t + => BytesPerType a + => ShowArrayViewType a name + => IsSymbol name + => Proxy a + -> DataView + -> ByteOffset + -> t + -> Effect Boolean +setLE = set LE + +-- | Store int8 value at a certain index in a `DataView`. +setInt8 :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt8 = setLE (Proxy :: Proxy Int8) + +-- | Store big-endian int16 value at a certain index in a `DataView`. +setInt16be :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt16be = setBE (Proxy :: Proxy Int16) + +-- | Store little-endian int16 value at a certain index in a `DataView`. +setInt16le :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt16le = setLE (Proxy :: Proxy Int16) + +-- | Store big-endian int32 value at a certain index in a `DataView`. +setInt32be :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt32be = setBE (Proxy :: Proxy Int32) + +-- | Store little-endian int32 value at a certain index in a `DataView`. +setInt32le :: DataView -> ByteOffset -> Int -> Effect Boolean +setInt32le = setLE (Proxy :: Proxy Int32) + +-- | Store uint8 value at a certain index in a `DataView`. +setUint8 :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint8 = setLE (Proxy :: Proxy Uint8) + +-- | Store big-endian uint16 value at a certain index in a `DataView`. +setUint16be :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint16be = setBE (Proxy :: Proxy Uint16) + +-- | Store little-endian uint16 value at a certain index in a `DataView`. +setUint16le :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint16le = setLE (Proxy :: Proxy Uint16) + +-- | Store big-endian uint32 value at a certain index in a `DataView`. +setUint32be :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint32be = setBE (Proxy :: Proxy Uint32) + +-- | Store little-endian uint32 value at a certain index in a `DataView`. +setUint32le :: DataView -> ByteOffset -> UInt -> Effect Boolean +setUint32le = setLE (Proxy :: Proxy Uint32) + +-- | Store big-endian float32 value at a certain index in a `DataView`. +setFloat32be :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean +setFloat32be = setBE (Proxy :: Proxy Float32) + +-- | Store little-endian float32 value at a certain index in a `DataView`. +setFloat32le :: DataView -> ByteOffset -> F.Float32 -> Effect Boolean +setFloat32le = setLE (Proxy :: Proxy Float32) + +-- | Store big-endian float64 value at a certain index in a `DataView`. +setFloat64be :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat64be = setBE (Proxy :: Proxy Float64) + +-- | Store little-endian float64 value at a certain index in a `DataView`. +setFloat64le :: DataView -> ByteOffset -> Number -> Effect Boolean +setFloat64le = setLE (Proxy :: Proxy Float64) diff --git a/src/Data/ArrayBuffer/DataView/Gen.purs b/src/Data/ArrayBuffer/DataView/Gen.purs index 46ba9ed..cd1c974 100644 --- a/src/Data/ArrayBuffer/DataView/Gen.purs +++ b/src/Data/ArrayBuffer/DataView/Gen.purs @@ -1,51 +1,37 @@ module Data.ArrayBuffer.DataView.Gen where -import Prelude - import Control.Monad.Gen (suchThat) import Control.Monad.Gen.Class (class MonadGen, chooseInt) import Control.Monad.Rec.Class (class MonadRec) import Data.ArrayBuffer.ArrayBuffer.Gen (genArrayBuffer) -import Data.ArrayBuffer.DataView (whole, byteLength, class DataView) -import Data.ArrayBuffer.Types (DataView, ByteOffset, kind ArrayViewType) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.Maybe (Maybe(Just)) -import Data.Typelevel.Num (class Nat, toInt') +import Data.ArrayBuffer.DataView (whole, byteLength) +import Data.ArrayBuffer.Types (DataView, ByteOffset, ArrayViewType) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, byteWidth) import Data.Unfoldable (replicateA) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec -import Partial.Unsafe (unsafePartial) +import Prelude ((<$>), bind, (<=), (-), pure) import Type.Proxy (Proxy(..)) - -genDataView :: forall m - . MonadGen m - => m DataView +genDataView :: forall m. MonadGen m => m DataView genDataView = whole <$> genArrayBuffer - - -- | For generating some set of offsets residing inside the generated array, with some computable value -data WithOffsetAndValue n (a :: ArrayViewType) t = - WithOffsetAndValue (Vec n ByteOffset) t DataView - -genWithOffsetAndValue :: forall m n a b t - . MonadGen m - => MonadRec m - => Nat n - => BytesPerValue a b - => DataView a t - => Nat b - => m DataView -- ^ Assumes generated length is at least the minimum length of one value - -> m t - -> m (WithOffsetAndValue n a t) -genWithOffsetAndValue gen genT = do - let n = toInt' (Proxy :: Proxy n) - b = toInt' (Proxy :: Proxy b) +data WithOffsetAndValue (a :: ArrayViewType) t = + WithOffsetAndValue (Array ByteOffset) t DataView + +genWithOffsetAndValue + :: forall m a t + . MonadGen m + => MonadRec m + => BytesPerType a + => BinaryValue a t + => Int -- generated length + -> m DataView -- ^ Assumes generated length is at least the minimum length of one value + -> m t + -> m (WithOffsetAndValue a t) +genWithOffsetAndValue n gen genT = do + let b = byteWidth (Proxy :: Proxy a) xs <- gen `suchThat` \xs -> b <= byteLength xs let l = byteLength xs - mos <- replicateA n (chooseInt 0 (l - b)) - let os = unsafePartial $ case Vec.fromArray mos of - Just q -> q + os <- replicateA n (chooseInt 0 (l - b)) t <- genT pure (WithOffsetAndValue os t xs) diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index fb56808..fc19771 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,36 +1,18 @@ -"use strict"; - - -exports.polyFill = function polyFill () { - var typedArrayTypes = - [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array - , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array - ]; - - for (var k in typedArrayTypes) { - for (var v in Array.prototype) { - if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) - typedArrayTypes[k].prototype[v] = Array.prototype[v]; - } - } -}; - - // module Data.ArrayBuffer.Typed -exports.buffer = function buffer (v) { +export function buffer(v) { return v.buffer; -}; +} -exports.byteOffset = function byteOffset (v) { +export function byteOffset(v) { return v.byteOffset; -}; +} -exports.byteLength = function byteLength (v) { +export function byteLength(v) { return v.byteLength; -}; +} -exports.lengthImpl = function lemgthImpl (v) { +export function lengthImpl(v) { return v.length; }; @@ -52,131 +34,126 @@ function newArray (f) { }; } -exports.newUint8ClampedArray = newArray(Uint8ClampedArray); -exports.newUint32Array = newArray(Uint32Array); -exports.newUint16Array = newArray(Uint16Array); -exports.newUint8Array = newArray(Uint8Array); -exports.newInt32Array = newArray(Int32Array); -exports.newInt16Array = newArray(Int16Array); -exports.newInt8Array = newArray(Int8Array); -exports.newFloat32Array = newArray(Float32Array); -exports.newFloat64Array = newArray(Float64Array); - +export const newUint8ClampedArray = newArray(Uint8ClampedArray); +export const newUint32Array = newArray(Uint32Array); +export const newUint16Array = newArray(Uint16Array); +export const newUint8Array = newArray(Uint8Array); +export const newInt32Array = newArray(Int32Array); +export const newInt16Array = newArray(Int16Array); +export const newInt8Array = newArray(Int8Array); +export const newFloat32Array = newArray(Float32Array); +export const newFloat64Array = newArray(Float64Array); // ------ -exports.everyImpl = function everyImpl (a,p) { +export function everyImpl(a, p) { return a.every(p); -}; -exports.someImpl = function someImpl (a,p) { - return a.some(p); -}; - +} -exports.fillImpl = function fillImpl (a,x,ms,me) { - return me === null ? (ms === null ? a.fill(x) : a.fill(x,ms)) : a.fill(x,ms,me); -}; +export function someImpl(a, p) { + return a.some(p); +} +export function fillImpl(x, s, e, a) { + return a.fill(x,s,e); +} -exports.mapImpl = function mapImpl (a,f) { +export function mapImpl(a, f) { return a.map(f); -}; +} -exports.forEachImpl = function forEachImpl (a,f) { +export function forEachImpl(a, f) { a.forEach(f); -}; +} -exports.filterImpl = function filterImpl (a,p) { +export function filterImpl(a, p) { return a.filter(p); -}; +} -exports.includesImpl = function includesImpl (a,x,mo) { +export function includesImpl(a, x, mo) { return mo === null ? a.includes(x) : a.includes(x,mo); -}; +} -exports.reduceImpl = function reduceImpl (a,f,i) { +export function reduceImpl(a, f, i) { return a.reduce(f,i); -}; -exports.reduce1Impl = function reduce1Impl (a,f) { +} + +export function reduce1Impl(a, f) { return a.reduce(f); -}; -exports.reduceRightImpl = function reduceRightImpl (a,f,i) { +} + +export function reduceRightImpl(a, f, i) { return a.reduceRight(f,i); -}; -exports.reduceRight1Impl = function reduceRight1Impl (a,f) { +} + +export function reduceRight1Impl(a, f) { return a.reduceRight(f); -}; +} -exports.findImpl = function findImpl (a,f) { - var x = a.find(f); - return (x === undefined) ? null : x; -}; -exports.findIndexImpl = function findIndexImpl (a,f) { - var x = a.findIndex(f); - return (x === -1) ? null : x; -}; -exports.indexOfImpl = function indexOfImpl (a,x,mo) { +export function findImpl(a, f) { + return a.find(f); +} + +export function findIndexImpl(a, f) { + var r = a.findIndex(f); + return r === -1 ? null : r; +} + +export function indexOfImpl(a, x, mo) { var r = mo === null ? a.indexOf(x) : a.indexOf(x,mo); return r === -1 ? null : r; -}; -exports.lastIndexOfImpl = function lastIndexOfImpl (a,x,mo) { +} + +export function lastIndexOfImpl(a, x, mo) { var r = mo === null ? a.lastIndexOf(x) : a.lastIndexOf(x,mo); return r === -1 ? null : r; -}; - - +} -exports.copyWithinImpl = function copyWithinImpl (a,t,s,me) { +export function copyWithinImpl(a, t, s, me) { if (me === null) { a.copyWithin(t,s); } else { a.copyWithin(t,s,me); } -}; - +} -exports.reverseImpl = function reverseImpl (a) { +export function reverseImpl(a) { a.reverse(); -}; - +} -exports.setImpl = function setImpl (a, off, b) { +export function setImpl(a, off, b) { a.set(b,off); -}; - - -exports.sliceImpl = function sliceImpl (a,ms,me) { - return me === null ? (ms === null ? a.slice() : a.slice(ms)) : a.slice(ms,me); -}; +} +export function sliceImpl(a, s, e) { + return a.slice(s,e); +} -exports.sortImpl = function sortImpl (a) { +export function sortImpl(a) { a.sort(); -}; - - -exports.subArrayImpl = function subArrayImpl (a,ms,me) { - return me === null ? (ms === null ? a.subarray() : a.subarray(ms)) : a.subarray(ms,me); -}; +} +export function subArrayImpl(a, s, e) { + return a.subarray(s, e); +} -exports.toString = function toString (a) { +export function toStringImpl(a) { return a.toString(); -}; +} -exports.joinImpl = function joinImpl (a,s) { +export function joinImpl(a, s) { return a.join(s); -}; +} -exports.unsafeAtImpl = function(a, i) { +export function unsafeAtImpl(a, i) { return a[i]; } -exports.hasIndexImpl = function(a, i) { +export function hasIndexImpl(a, i) { return i in a; } -exports.toArrayImpl = function(a) { +export function toArrayImpl(a) { var l = a.length; var ret = new Array(l); for (var i = 0; i < l; i++) diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index 98df98c..e46132d 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -1,47 +1,109 @@ -- | This module represents the functional bindings to JavaScript's `TypedArray` and other -- | objects. See [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for details. +-- | +-- | #### Creation +-- | +-- | - `whole`, `remainder`, and `part` are functions for building a typed array accessible interface +-- | on top of an existing `ArrayBuffer` +-- | - `empty` and `fromArray` are functions for creating pure typed arrays +-- | +-- | #### Modification +-- | +-- | - `fill`, `set`, and `setTyped` are functions for assigning values from external sources +-- | - `map` and `traverse` allow you to create a new array from the existing values in another +-- | - `copyWithin` allows you to set values to the array that exist in other parts of the array +-- | - `filter` creates a new array without the values that don't pass a predicate +-- | - `reverse` modifies an existing array in-place, with all values reversed +-- | - `sort` modifies an existing array in-place, with all values sorted +-- | +-- | #### Access +-- | +-- | - `elem`, `all`, and `any` are functions for testing the contents of an array +-- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an index +-- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array +-- | - `find` and `findIndex` are searching functions via a predicate +-- | - `indexOf` and `lastIndexOf` are searching functions via equality +-- | - `slice` returns a new typed array with a new copied underlying `ArrayBuffer` +-- | - `subArray` returns a new typed array view of the same `ArrayBuffer` +-- | - `toString` prints to a CSV, `join` allows you to supply the delimiter +-- | - `toArray` returns an array of numeric values module Data.ArrayBuffer.Typed - ( polyFill - , Offset, Length, Range - , buffer, byteOffset, byteLength, length + ( Index + , Length + , buffer + , byteOffset + , byteLength + , length + , compare + , eq , class TypedArray - , create, whole, remainder, part, empty, fromArray - , fill, set, setTyped, copyWithin - , map, traverse, traverse_, filter - , mapWithIndex, traverseWithIndex, traverseWithIndex_, filterWithIndex - , sort, reverse + , create + , whole + , remainder + , part + , empty + , fromArray + , fill + , set + , setTyped + , copyWithin + , map + , traverse + , traverse_ + , filter + , mapWithIndex + , traverseWithIndex + , traverseWithIndex_ + , filterWithIndex + , sort + , reverse , elem - , all, any - , allWithIndex, anyWithIndex - , unsafeAt, hasIndex, at, (!) - , foldlM, foldl1M, foldl, foldl1, foldrM, foldr1M, foldr, foldr1 - , find, findIndex, indexOf, lastIndexOf - , slice, subArray - , toString, toString', toArray + , all + , any + , allWithIndex + , anyWithIndex + , unsafeAt + , hasIndex + , at + , (!) + , reduce + , reduce1 + , foldl + , foldl1 + , reduceRight + , reduceRight1 + , foldr + , foldr1 + , foldlWithIndex + , foldrWithIndex + , find + , findIndex + , findWithIndex + , indexOf + , lastIndexOf + , slice + , subArray + , toString + , join + , toArray ) where -import Prelude - import Data.Array (length) as A -import Data.ArrayBuffer.Types (ArrayView, kind ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerValue) +import Data.ArrayBuffer.Types (ArrayView, ArrayViewType, ArrayBuffer, ByteOffset, ByteLength, Float64Array, Float32Array, Uint8ClampedArray, Uint32Array, Uint16Array, Uint8Array, Int32Array, Int16Array, Int8Array, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, byteWidth) +import Data.Float32 (Float32) as F import Data.Function.Uncurried (Fn2, Fn3, mkFn2, runFn2, runFn3) -import Data.Maybe (Maybe(..), fromMaybe) +import Data.Maybe (Maybe, fromMaybe) import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable) -import Data.Tuple (Tuple(..)) -import Data.Typelevel.Num (class Nat, toInt') import Data.UInt (UInt) import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4) import Effect.Unsafe (unsafePerformEffect) -import Partial.Unsafe (unsafePartial) +import Prelude (class Eq, class Ord, Ordering, Unit, flip, pure, ($), (&&), (*), (*>), (-), (<$>), (<*>), (<<<), (<=), (>=)) +import Prelude as Prelude import Type.Proxy (Proxy(..)) - --- | Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -foreign import polyFill :: Effect Unit - -- | `ArrayBuffer` being mapped by the typed array. foreign import buffer :: forall a. ArrayView a -> ArrayBuffer @@ -51,381 +113,364 @@ foreign import byteOffset :: forall a. ArrayView a -> ByteOffset -- | Represents the length of this typed array, in bytes. foreign import byteLength :: forall a. ArrayView a -> ByteLength -foreign import lengthImpl :: forall a. ArrayView a -> Length - +-- | Represents the number of elements in this typed array. length :: forall a. ArrayView a -> Length length = lengthImpl +foreign import lengthImpl :: forall a. ArrayView a -> Length -- object creator implementations for each typed array -foreign import newUint8ClampedArray :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray -foreign import newUint32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array -foreign import newUint16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array -foreign import newUint8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array -foreign import newInt32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array -foreign import newInt16Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array -foreign import newInt8Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array -foreign import newFloat32Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array -foreign import newFloat64Array :: forall a. Fn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array - - --- ---- - -foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean -foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean - -foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b (Nullable Offset) (Nullable Offset) Unit - -foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a) -foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit -foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a) -foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean -foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c -foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b -foreign import findImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b) -foreign import findIndexImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset) -foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) -foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset) - - --- | Value-oriented array offset -type Offset = Int --- | Value-oriented array length +foreign import newUint8ClampedArray :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8ClampedArray +foreign import newUint32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint32Array +foreign import newUint16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint16Array +foreign import newUint8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Uint8Array +foreign import newInt32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int32Array +foreign import newInt16Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int16Array +foreign import newInt8Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Int8Array +foreign import newFloat32Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float32Array +foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (Nullable ByteLength) Float64Array + +-- | Value-oriented array index. +type Index = Int +-- | Value-oriented array length. type Length = Int --- | Represents a range of indices, where if omitted, it represents the whole span. --- | If only the second argument is omitted, then it represents the remainder of the span after the first index. -type Range = Maybe (Tuple Offset (Maybe Offset)) - - --- TODO use purescript-quotient --- | Typeclass that associates a measured user-level type with a typed array. --- | --- | #### Creation --- | --- | - `whole`, `remainder`, and `part` are methods for building a typed array accessible interface --- | on top of an existing `ArrayBuffer` - Note, `part` and `remainder` may behave unintuitively - --- | when the operation is isomorphic to `whole`, the new TypedArray uses the same buffer as the input, --- | but not when the portion is a sub-array of the original buffer, a new one is made with --- | `Data.ArrayBuffer.ArrayBuffer.slice`. --- | - `empty` and `fromArray` are methods for creating pure typed arrays --- | --- | #### Modification --- | --- | - `fill`, `set`, and `setTyped` are methods for assigning values from external sources --- | - `map` and `traverse` allow you to create a new array from the existing values in another --- | - `copyWithin` allows you to set values to the array that exist in other parts of the array --- | - `filter` creates a new array without the values that don't pass a predicate --- | - `reverse` modifies an existing array in-place, with all values reversed --- | - `sort` modifies an existing array in-place, with all values sorted --- | --- | #### Access --- | --- | - `elem`, `all`, and `any` are functions for testing the contents of an array --- | - `unsafeAt`, `hasIndex`, and `at` are used to get values from an array, with an offset --- | - `foldr`, `foldrM`, `foldr1`, `foldr1M`, `foldl`, `foldlM`, `foldl1`, `foldl1M` all can reduce an array --- | - `find` and `findIndex` are searching functions via a predicate --- | - `indexOf` and `lastIndexOf` are searching functions via equality --- | - `slice` returns a new typed array on the same array buffer content as the input --- | - `subArray` returns a new typed array with a separate array buffer --- | - `toString` prints to a CSV, `toString'` allows you to supply the delimiter --- | - `toArray` returns an array of numeric values - class BinaryValue a t <= TypedArray (a :: ArrayViewType) (t :: Type) | a -> t where - create :: forall x. Fn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) + create :: forall x. EffectFn3 x (Nullable ByteOffset) (Nullable ByteLength) (ArrayView a) instance typedArrayUint8Clamped :: TypedArray Uint8Clamped UInt where create = newUint8ClampedArray + instance typedArrayUint32 :: TypedArray Uint32 UInt where create = newUint32Array + instance typedArrayUint16 :: TypedArray Uint16 UInt where create = newUint16Array + instance typedArrayUint8 :: TypedArray Uint8 UInt where create = newUint8Array + instance typedArrayInt32 :: TypedArray Int32 Int where create = newInt32Array + instance typedArrayInt16 :: TypedArray Int16 Int where create = newInt16Array + instance typedArrayInt8 :: TypedArray Int8 Int where create = newInt8Array -instance typedArrayFloat32 :: TypedArray Float32 Number where + +instance typedArrayFloat32 :: TypedArray Float32 F.Float32 where create = newFloat32Array + instance typedArrayFloat64 :: TypedArray Float64 Number where create = newFloat64Array -- | View mapping the whole `ArrayBuffer`. -whole :: forall a t. TypedArray a t => ArrayBuffer -> ArrayView a -whole a = runFn3 create a null null +whole :: forall a t. TypedArray a t => ArrayBuffer -> Effect (ArrayView a) +whole a = runEffectFn3 create a null null -- | View mapping the rest of an `ArrayBuffer` after an index. -remainder :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> ArrayView a +remainder :: forall a b t. TypedArray a t => BytesPerType b => ArrayBuffer -> Index -> Effect (ArrayView a) remainder a x = remainder' a o - where o = x * toInt' (Proxy :: Proxy b) + where + o = x * byteWidth (Proxy :: Proxy b) -remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> ArrayView a -remainder' a x = runFn3 create a (notNull x) null +remainder' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Effect (ArrayView a) +remainder' a x = runEffectFn3 create a (notNull x) null -- | View mapping a region of the `ArrayBuffer`. -part :: forall a b t. TypedArray a t => Nat b => BytesPerValue a b => ArrayBuffer -> Offset -> Length -> ArrayView a +part :: forall a t. TypedArray a t => BytesPerType a => ArrayBuffer -> Index -> Length -> Effect (ArrayView a) part a x y = part' a o y - where o = x * toInt' (Proxy :: Proxy b) + where + o = x * byteWidth (Proxy :: Proxy a) -part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> ArrayView a -part' a x y = runFn3 create a (notNull x) (notNull y) +-- | The ByteOffset must be aligned. +-- | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#byteoffset_must_be_aligned +part' :: forall a t. TypedArray a t => ArrayBuffer -> ByteOffset -> Length -> Effect (ArrayView a) +part' a x y = runEffectFn3 create a (notNull x) (notNull y) --- | Creates an empty typed array, where each value is assigned 0 -empty :: forall a t. TypedArray a t => Length -> ArrayView a -empty n = runFn3 create n null null +-- | Creates an empty typed array, where each value is assigned 0. +empty :: forall a t. TypedArray a t => Length -> Effect (ArrayView a) +empty n = runEffectFn3 create n null null --- | Creates a typed array from an input array of values, to be binary serialized -fromArray :: forall a t. TypedArray a t => Array t -> ArrayView a -fromArray a = runFn3 create a null null +-- | Creates a typed array from an input array of values, to be binary serialized. +fromArray :: forall a t. TypedArray a t => Array t -> Effect (ArrayView a) +fromArray a = runEffectFn3 create a null null --- | Fill the array with a value -fill :: forall a t. TypedArray a t => ArrayView a -> t -> Range -> Effect Unit -fill a x mz = case mz of - Nothing -> runEffectFn4 fillImpl a x null null - Just (Tuple s me) -> runEffectFn4 fillImpl a x (notNull s) (toNullable me) +-- | Fill the array with a value. +fill :: forall a t. TypedArray a t => t -> Index -> Index -> ArrayView a -> Effect Unit +fill x s e a = runEffectFn4 fillImpl x s e a --- | Stores multiple values into the typed array -set :: forall a t. TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean +foreign import fillImpl :: forall a b. EffectFn4 b Index Index (ArrayView a) Unit + +-- | Stores multiple values into the typed array. +set :: forall a t. TypedArray a t => ArrayView a -> Maybe Index -> Array t -> Effect Boolean set = setInternal A.length ap1 :: forall a b c. (a -> c) -> (a -> b -> c) ap1 f = \x _ -> f x - --- | Maps a new value over the typed array, creating a new buffer and +-- | Maps a new value over the typed array, creating a new `ArrayBuffer` and -- | typed array as well. map :: forall a t. TypedArray a t => (t -> t) -> ArrayView a -> ArrayView a map = mapWithIndex' <<< ap1 -- | Apply a function to each element in an array, supplying a -- | generated zero-based index integer along with the element, --- | creating a typed array with the new elements -mapWithIndex :: forall a t. TypedArray a t => (Offset -> t -> t) -> ArrayView a -> ArrayView a +-- | creating a typed array with the new elements. +mapWithIndex :: forall a t. TypedArray a t => (Index -> t -> t) -> ArrayView a -> ArrayView a mapWithIndex = mapWithIndex' <<< flip -mapWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> t) -> ArrayView a -> ArrayView a -mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o)))) +mapWithIndex' :: forall a t. TypedArray a t => (t -> Index -> t) -> ArrayView a -> ArrayView a +mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 \x o -> pure (f x o))) --- | Traverses over each value, returning a new one +foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index b) (ArrayView a) + +-- | Traverses over each value, returning a new one. traverse :: forall a t. TypedArray a t => (t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverse = traverseWithIndex' <<< ap1 --- | Traverses over each value, returning a new one -traverseWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) +-- | Traverses over each value, returning a new one. +traverseWithIndex :: forall a t. TypedArray a t => (Index -> t -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex = traverseWithIndex' <<< flip -traverseWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Effect t) -> ArrayView a -> Effect (ArrayView a) +traverseWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Effect t) -> ArrayView a -> Effect (ArrayView a) traverseWithIndex' f a = runEffectFn2 mapImpl a (mkEffectFn2 f) --- | Traverses over each value +-- | Traverses over each value. traverse_ :: forall a t. TypedArray a t => (t -> Effect Unit) -> ArrayView a -> Effect Unit traverse_ = traverseWithIndex_' <<< ap1 --- | Traverses over each value -traverseWithIndex_ :: forall a t. TypedArray a t => (Offset -> t -> Effect Unit) -> ArrayView a -> Effect Unit +-- | Traverses over each value. +traverseWithIndex_ :: forall a t. TypedArray a t => (Index -> t -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_ = traverseWithIndex_' <<< flip -traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit) -> ArrayView a -> Effect Unit +traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Index -> Effect Unit) -> ArrayView a -> Effect Unit traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f) --- | Test a predicate to pass on all values -all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Index Unit) Unit + +-- | Test a predicate to pass on all values. +all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean all = every <<< ap1 -allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +-- | Test a predicate to pass on all values. The predicate function +-- | receives the index and the element. +allWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect Boolean allWithIndex = every <<< flip -every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -every p a = runFn2 everyImpl a (mkFn2 p) +every :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean +every p a = runEffectFn2 everyImpl a (mkFn2 p) + +foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean --- | Test a predicate to pass on any value -any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean +-- | Test a predicate to pass on any value. +any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean any = some <<< ap1 -anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean +-- | Test a predicate (that receives also an index) to pass on any value. +anyWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect Boolean anyWithIndex = some <<< flip -some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean -some p a = runFn2 someImpl a (mkFn2 p) +some :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect Boolean +some p a = runEffectFn2 someImpl a (mkFn2 p) --- | Returns a new typed array with all values that pass the predicate -filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> ArrayView a +foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) Boolean + +-- | Returns a new typed array with all values that pass the predicate. +filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filter = filterWithIndex' <<< ap1 -filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> ArrayView a +-- | Returns a new typed array with all values that pass the +-- | predicate. The predicate function receives the index and the +-- | element. +filterWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a) filterWithIndex = filterWithIndex' <<< flip -filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a -filterWithIndex' p a = runFn2 filterImpl a (mkFn2 p) +filterWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (ArrayView a) +filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p) + +foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (ArrayView a) --- | Tests if a value is an element of the typed array -elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean -elem x mo a = runFn3 includesImpl a x (toNullable mo) +-- | Tests if a value is an element of the typed array. +elem :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect Boolean +elem x mo a = runEffectFn3 includesImpl a x (toNullable mo) + +foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) Boolean -- | Fetch element at index. -unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> t -unsafeAt a o = runFn2 unsafeAtImpl a o +unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Index -> Effect t +unsafeAt a o = runEffectFn2 unsafeAtImpl a o + +-- | Folding from the left. +reduce :: forall a t b. TypedArray a t => (b -> t -> Index -> Effect b) -> b -> ArrayView a -> Effect b +reduce f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i + +foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c + +-- | Folding from the left. Assumes the typed array is non-empty. +reduce1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t +reduce1 f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) + +foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b --- | Folding from the left -foldlM :: forall a t b. TypedArray a t => (b -> t -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -foldlM f i a = runEffectFn3 reduceImpl a (mkEffectFn3 f) i +-- | Folding from the right. +reduceRight :: forall a t b. TypedArray a t => (t -> b -> Index -> Effect b) -> b -> ArrayView a -> Effect b +reduceRight f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 \acc x o -> f x acc o) i --- | Assumes the typed array is non-empty -foldl1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -foldl1M f a = runEffectFn2 reduce1Impl a (mkEffectFn3 f) +foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Index c) c c --- | Folding from the right -foldrM :: forall a t b. TypedArray a t => (t -> b -> Offset -> Effect b) -> b -> ArrayView a -> Effect b -foldrM f i a = runEffectFn3 reduceRightImpl a (mkEffectFn3 (\acc x o -> f x acc o)) i +-- | Folding from the right. Assumes the typed array is non-empty. +reduceRight1 :: forall a t. Partial => TypedArray a t => (t -> t -> Index -> Effect t) -> ArrayView a -> Effect t +reduceRight1 f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 \acc x o -> f x acc o) --- | Assumes the typed array is non-empty -foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> ArrayView a -> Effect t -foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o)) +foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Index b) b --- | Returns the first value satisfying the predicate -find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Maybe t +-- | Returns the first value satisfying the predicate. +find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t) find = findWithIndex' <<< ap1 -findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Maybe t +-- | Returns the first value satisfying the predicate. The predicate +-- | receives the index and the element at that index. +findWithIndex :: forall a t. TypedArray a t => (Index -> t -> Boolean) -> ArrayView a -> Effect (Maybe t) findWithIndex = findWithIndex' <<< flip -findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t -findWithIndex' f a = toMaybe (runFn2 findImpl a (mkFn2 f)) +findWithIndex' :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe t) +findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f) --- | Returns the first index of the value satisfying the predicate -findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset -findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f)) +foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable b) --- | Returns the first index of the element, if it exists, from the left -indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset -indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo)) +-- | Returns the first index of the value satisfying the predicate. +findIndex :: forall a t. TypedArray a t => (t -> Index -> Boolean) -> ArrayView a -> Effect (Maybe Index) +findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f) --- | Returns the first index of the element, if it exists, from the right -lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset -lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo)) +foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Index Boolean) (Nullable Index) -foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> b -foldl f = foldlWithIndex' (\a x _ -> f a x) +-- | Returns the first index of the element, if it exists, from the left. +indexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) +indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo) -foldlWithIndex :: forall a b t. TypedArray a t => (Offset -> b -> t -> b) -> b -> ArrayView a -> b -foldlWithIndex f = foldlWithIndex' (\a x o -> f o a x) +foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) -foldlWithIndex' :: forall a b t. TypedArray a t => (b -> t -> Offset -> b) -> b -> ArrayView a -> b -foldlWithIndex' f i = unsafePerformEffect <<< foldlM (\a x o -> pure (f a x o)) i +-- | Returns the first index of the element, if it exists, from the right. +lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Index -> ArrayView a -> Effect (Maybe Index) +lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo) -foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> b -foldr f = foldrWithIndex' (\a x _ -> f a x) +foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Index) (Nullable Index) -foldrWithIndex :: forall a b t. TypedArray a t => (Offset -> t -> b -> b) -> b -> ArrayView a -> b -foldrWithIndex f = foldrWithIndex' (\a x o -> f o a x) +-- | Fold a list from the left, accumulating the result using the +-- | specified function. +foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> Effect b +foldl f = reduce \a x _ -> pure $ f a x -foldrWithIndex' :: forall a b t. TypedArray a t => (t -> b -> Offset -> b) -> b -> ArrayView a -> b -foldrWithIndex' f i = unsafePerformEffect <<< foldrM (\x a o -> pure (f x a o)) i +-- | Fold a list from the left, accumulating the result using the +-- | supplied function. The accumulating function receives the index, +-- | the accumulated value and the element. +foldlWithIndex :: forall a b t. TypedArray a t => (Index -> b -> t -> b) -> b -> ArrayView a -> Effect b +foldlWithIndex f = reduce \a x o -> pure $ f o a x -foldl1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t -foldl1 f = foldl1WithIndex (\_ a x -> f a x) +-- | Fold a list from the right, accumulating the result using the +-- | specified function. +foldr :: forall a b t. TypedArray a t => (t -> b -> b) -> b -> ArrayView a -> Effect b +foldr f = reduceRight \a x _ -> pure $ f a x -foldl1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t -foldl1WithIndex f = unsafePerformEffect <<< foldl1M (\acc x o -> pure (f o acc x)) +-- | Fold a list from the right, accumulating the result using the +-- | supplied function. The accumulating function receives the index, +-- | the element and the accumulated value. +foldrWithIndex :: forall a b t. TypedArray a t => (Index -> t -> b -> b) -> b -> ArrayView a -> Effect b +foldrWithIndex f = reduceRight \a x o -> pure $ f o a x -foldr1 :: forall a t. TypedArray a t => (t -> t -> t) -> ArrayView a -> t -foldr1 f = foldr1WithIndex (\_ a x -> f a x) +-- | Folding from the left. Assumes the typed array is non-empty. +foldl1 :: forall a t. Partial => TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t +foldl1 f = reduce1 \a x _ -> pure $ f a x -foldr1WithIndex :: forall a t. TypedArray a t => (Offset -> t -> t -> t) -> ArrayView a -> t -foldr1WithIndex f = unsafePerformEffect <<< foldr1M (\x a o -> pure (f o x a)) - -foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Offset Offset (Nullable Offset) Unit +-- | Folding from the right. Assumes the typed array is non-empty. +foldr1 :: forall a t. Partial => TypedArray a t => (t -> t -> t) -> ArrayView a -> Effect t +foldr1 f = reduceRight1 \x a _ -> pure $ f a x -- | Internally copy values - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/copyWithin) for details. -copyWithin :: forall a. ArrayView a -> Offset -> Offset -> Maybe Offset -> Effect Unit +copyWithin :: forall a. ArrayView a -> Index -> Index -> Maybe Index -> Effect Unit copyWithin a t s me = runEffectFn4 copyWithinImpl a t s (toNullable me) -foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit +foreign import copyWithinImpl :: forall a. EffectFn4 (ArrayView a) Index Index (Nullable Index) Unit -- | Reverses a typed array in-place. reverse :: forall a. ArrayView a -> Effect Unit reverse a = runEffectFn1 reverseImpl a -foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Offset b Unit +foreign import reverseImpl :: forall a. EffectFn1 (ArrayView a) Unit -setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Offset -> b -> Effect Boolean -setInternal lenfn a mo b = +setInternal :: forall a b. (b -> Length) -> ArrayView a -> Maybe Index -> b -> Effect Boolean +setInternal lenfn a mo b = do let o = fromMaybe 0 mo - in if o >= 0 && lenfn b <= length a - o - then runEffectFn3 setImpl a o b *> pure true - else pure false + if o >= 0 && lenfn b <= length a - o then + runEffectFn3 setImpl a o b *> pure true + else + pure false +foreign import setImpl :: forall a b. EffectFn3 (ArrayView a) Index b Unit -- | Stores multiple values in the typed array, reading input values from the second typed array. -setTyped :: forall a. ArrayView a -> Maybe Offset -> ArrayView a -> Effect Boolean +setTyped :: forall a. ArrayView a -> Maybe Index -> ArrayView a -> Effect Boolean setTyped = setInternal length +-- | Copy part of the contents of a typed array into a new `ArrayBuffer`, +-- | between the start and end indices. +slice :: forall a. Index -> Index -> ArrayView a -> Effect (ArrayView a) +slice s e a = runEffectFn3 sliceImpl a s e --- | Copy the entire contents of the typed array into a new buffer. -foreign import sliceImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) - --- | Copy part of the contents of a typed array into a new buffer, between some start and end indices. -slice :: forall a. ArrayView a -> Range -> ArrayView a -slice a mz = case mz of - Nothing -> runFn3 sliceImpl a null null - Just (Tuple s me) -> runFn3 sliceImpl a (notNull s) (toNullable me) - -foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit +foreign import sliceImpl :: forall a. EffectFn3 (ArrayView a) Index Index (ArrayView a) --- | Sorts the values in-place +-- | Sorts the values in-place. sort :: forall a. ArrayView a -> Effect Unit sort a = runEffectFn1 sortImpl a +foreign import sortImpl :: forall a. EffectFn1 (ArrayView a) Unit -foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) (Nullable Offset) (Nullable Offset) (ArrayView a) +-- | Returns a new typed array view of the same `ArrayBuffer`, beginning at +-- | the index and ending at the second. +subArray :: forall a. Index -> Index -> ArrayView a -> ArrayView a +subArray s e a = runFn3 subArrayImpl a s e --- | Returns a new typed array view of the same buffer, beginning at the index and ending at the second. --- | --- | **Note**: there is really peculiar behavior with `subArray` - if the first offset argument is omitted, or --- | is `0`, and likewise if the second argument is the length of the array, then the "sub-array" is actually a --- | mutable replica of the original array - the sub-array reference reflects mutations to the original array. --- | However, when the sub-array is is actually a smaller contiguous portion of the array, then it behaves --- | purely, because JavaScript interally calls `Data.ArrayBuffer.ArrayBuffer.slice`. -subArray :: forall a. ArrayView a -> Range -> ArrayView a -subArray a mz = case mz of - Nothing -> runFn3 subArrayImpl a null null - Just (Tuple s me) -> runFn3 subArrayImpl a (notNull s) (toNullable me) +foreign import subArrayImpl :: forall a. Fn3 (ArrayView a) Index Index (ArrayView a) -- | Prints array to a comma-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toString) for details. -foreign import toString :: forall a. ArrayView a -> String +toString :: forall a. ArrayView a -> Effect String +toString a = runEffectFn1 toStringImpl a -foreign import joinImpl :: forall a. Fn2 (ArrayView a) String String +foreign import toStringImpl :: forall a. EffectFn1 (ArrayView a) String -- | Prints array to a delimiter-separated string - see [MDN's spec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/join) for details. -toString' :: forall a. ArrayView a -> String -> String -toString' a s = runFn2 joinImpl a s - - -foreign import unsafeAtImpl :: forall a b. Fn2 (ArrayView a) Offset b +join :: forall a. String -> ArrayView a -> Effect String +join s a = runEffectFn2 joinImpl a s -foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Offset Boolean +foreign import joinImpl :: forall a. EffectFn2 (ArrayView a) String String -- | Determine if a certain index is valid. -hasIndex :: forall a. ArrayView a -> Offset -> Boolean +hasIndex :: forall a. ArrayView a -> Index -> Boolean hasIndex a o = runFn2 hasIndexImpl a o +foreign import hasIndexImpl :: forall a. Fn2 (ArrayView a) Index Boolean + -- | Fetch element at index. -at :: forall a t. TypedArray a t => ArrayView a -> Offset -> Maybe t -at a n = if a `hasIndex` n - then Just (unsafePartial (unsafeAt a n)) - else Nothing +at :: forall a t. TypedArray a t => ArrayView a -> Index -> Effect (Maybe t) +at a n = toMaybe <$> runEffectFn2 unsafeAtImpl a n + +foreign import unsafeAtImpl :: forall a b. EffectFn2 (ArrayView a) Index b infixl 3 at as ! +-- | Turn typed array into an array. +toArray :: forall a t. TypedArray a t => ArrayView a -> Effect (Array t) +toArray a = runEffectFn1 toArrayImpl a -foreign import toArrayImpl :: forall a b. ArrayView a -> Array b +foreign import toArrayImpl :: forall a b. EffectFn1 (ArrayView a) (Array b) --- | Turn typed array into an array. -toArray :: forall a t. TypedArray a t => ArrayView a -> Array t -toArray = toArrayImpl +-- | Compare 2 typed arrays. +compare :: forall a t. TypedArray a t => Ord t => ArrayView a -> ArrayView a -> Effect Ordering +compare a b = Prelude.compare <$> toArray a <*> toArray b + +-- | Equality test for typed arrays. +eq :: forall a t. TypedArray a t => Eq t => ArrayView a -> ArrayView a -> Effect Boolean +eq a b = Prelude.eq <$> toArray a <*> toArray b diff --git a/src/Data/ArrayBuffer/Typed/Gen.js b/src/Data/ArrayBuffer/Typed/Gen.js deleted file mode 100644 index a592186..0000000 --- a/src/Data/ArrayBuffer/Typed/Gen.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -// module Data.ArrayBuffer.Typed.Gen - -exports.toFloat32 = function toFloat32(s) { - var r = new Float32Array(1); - r[0] = s; - return r[0]; -} diff --git a/src/Data/ArrayBuffer/Typed/Gen.purs b/src/Data/ArrayBuffer/Typed/Gen.purs index e617f96..b2091ff 100644 --- a/src/Data/ArrayBuffer/Typed/Gen.purs +++ b/src/Data/ArrayBuffer/Typed/Gen.purs @@ -1,35 +1,29 @@ -- | Functions for generating typed arrays and values. - module Data.ArrayBuffer.Typed.Gen where -import Prelude - import Control.Monad.Gen.Class (class MonadGen, sized, chooseInt, chooseFloat) import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA import Data.ArrayBuffer.Types (ArrayView) -import Data.Generic.Rep (class Generic) -import Data.Maybe (Maybe(..)) -import Data.Typelevel.Num (class Nat, toInt') +import Data.ArrayBuffer.ValueMapping (class BytesPerType) +import Data.Float32 (Float32, fromNumber') as F import Data.UInt (UInt) import Data.UInt (fromInt) as UInt import Data.UInt.Gen (genUInt) as UInt import Data.Unfoldable (replicateA) -import Data.Vec (Vec) -import Data.Vec (fromArray) as Vec -import Partial.Unsafe (unsafePartial) -import Type.Proxy (Proxy(..)) - - -genTypedArray :: forall m a t - . MonadGen m - => TypedArray a t - => m t - -> m (ArrayView a) +import Effect.Unsafe (unsafePerformEffect) +import Prelude (bind, bottom, negate, pure, top, ($), (-), (/), (<$>)) + +genTypedArray + :: forall m a t + . MonadGen m + => TypedArray a t + => m t + -> m (ArrayView a) genTypedArray gen = sized \s -> do n <- chooseInt 0 s a <- replicateA n gen - pure $ TA.fromArray a + pure (unsafePerformEffect $ TA.fromArray a) genUint8 :: forall m. MonadGen m => m UInt genUint8 = UInt.fromInt <$> chooseInt 0 255 @@ -49,29 +43,26 @@ genUint32 = UInt.genUInt bottom top genInt32 :: forall m. MonadGen m => m Int genInt32 = chooseInt bottom top -foreign import toFloat32 :: Number -> Number - -genFloat32 :: forall m. MonadGen m => m Number -genFloat32 = toFloat32 <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 +genFloat32 :: forall m. MonadGen m => m F.Float32 +genFloat32 = F.fromNumber' <$> chooseFloat (-3.40282347e+38) 3.40282347e+38 genFloat64 :: forall m. MonadGen m => m Number -genFloat64 = chooseFloat ((-1.7976931348623157e+308)/div) (1.7976931348623157e+308/div) - where div = 4.0 +genFloat64 = chooseFloat ((-1.7976931348623157e+308) / div) (1.7976931348623157e+308 / div) + where + div = 4.0 -- | For generating some set of offsets residing inside the generated array -data WithOffset n a = WithOffset (Vec n TA.Offset) (ArrayView a) -derive instance genericWithOffset :: Generic (ArrayView a) a' => Generic (WithOffset n a) _ - -genWithOffset :: forall m n a - . MonadGen m - => Nat n - => m (ArrayView a) - -> m (WithOffset n a) -genWithOffset gen = do - let n = toInt' (Proxy :: Proxy n) +data WithIndices a = WithIndices (Array TA.Index) (ArrayView a) + +genWithIndices + :: forall m a + . MonadGen m + => BytesPerType a + => Int -- Number of offsets residing inside the generated array + -> m (ArrayView a) + -> m (WithIndices a) +genWithIndices n gen = do xs <- gen let l = TA.length xs - mos <- replicateA n (chooseInt 0 (l - 1)) - let os = unsafePartial $ case Vec.fromArray mos of - Just q -> q - pure (WithOffset os xs) + os <- replicateA n (chooseInt 0 (l - 1)) + pure (WithIndices os xs) diff --git a/src/Data/ArrayBuffer/ValueMapping.purs b/src/Data/ArrayBuffer/ValueMapping.purs index 0ed8fb9..8694a19 100644 --- a/src/Data/ArrayBuffer/ValueMapping.purs +++ b/src/Data/ArrayBuffer/ValueMapping.purs @@ -1,28 +1,51 @@ -- | This module represents type-level mappings between `ArrayViewType`s -- | and meaningful data. +module Data.ArrayBuffer.ValueMapping + ( class BytesPerType + , byteWidth + , class BinaryValue + , class ShowArrayViewType + ) where -module Data.ArrayBuffer.ValueMapping where - -import Data.ArrayBuffer.Types (kind ArrayViewType, Float64, Float32, Uint8Clamped, Uint32, Uint16, Uint8, Int32, Int16, Int8) -import Data.Typelevel.Num (D1, D2, D4, D8) +import Data.ArrayBuffer.Types (ArrayViewType, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped) +import Data.Float32 (Float32) as F import Data.UInt (UInt) +import Type.Proxy (Proxy) + +-- | Type-level map of each `ArrayViewType` to the number of bytes of storage +-- | it requires. +class BytesPerType (a :: ArrayViewType) where + byteWidth :: (Proxy a) -> Int + +instance bytesPerTypeInt8 :: BytesPerType Int8 where + byteWidth _ = 1 + +instance bytesPerTypeInt16 :: BytesPerType Int16 where + byteWidth _ = 2 + +instance bytesPerTypeInt32 :: BytesPerType Int32 where + byteWidth _ = 4 +instance bytesPerTypeUint8 :: BytesPerType Uint8 where + byteWidth _ = 1 --- | Maps a `TypedArray`'s binary casted value, to the space occupied by that value, in bytes. -class BytesPerValue (a :: ArrayViewType) (b :: Type) | a -> b +instance bytesPerTypeUint16 :: BytesPerType Uint16 where + byteWidth _ = 2 -instance bytesPerValueUint8Clamped :: BytesPerValue Uint8Clamped D1 -instance bytesPerValueUint32 :: BytesPerValue Uint32 D4 -instance bytesPerValueUint16 :: BytesPerValue Uint16 D2 -instance bytesPerValueUint8 :: BytesPerValue Uint8 D1 -instance bytesPerValueInt32 :: BytesPerValue Int32 D4 -instance bytesPerValueInt16 :: BytesPerValue Int16 D2 -instance bytesPerValueInt8 :: BytesPerValue Int8 D1 -instance bytesPerValueFloat32 :: BytesPerValue Float32 D4 -instance bytesPerValueFloat64 :: BytesPerValue Float64 D8 +instance bytesPerTypeUint32 :: BytesPerType Uint32 where + byteWidth _ = 4 +instance bytesPerTypeUint8Clamped :: BytesPerType Uint8Clamped where + byteWidth _ = 1 --- | Maps a `TypedArray`'s binary casted value, to its computable representation in JavaScript. +instance bytesPerTypeFloat32 :: BytesPerType Float32 where + byteWidth _ = 4 + +instance bytesPerTypeFloat64 :: BytesPerType Float64 where + byteWidth _ = 8 + +-- | Type-level map of `TypedArray`’s binary casted value to its +-- | representation in JavaScript. class BinaryValue (a :: ArrayViewType) (t :: Type) | a -> t instance binaryValueUint8Clamped :: BinaryValue Uint8Clamped UInt @@ -32,5 +55,18 @@ instance binaryValueUint8 :: BinaryValue Uint8 UInt instance binaryValueInt32 :: BinaryValue Int32 Int instance binaryValueInt16 :: BinaryValue Int16 Int instance binaryValueInt8 :: BinaryValue Int8 Int -instance binaryValueFloat32 :: BinaryValue Float32 Number +instance binaryValueFloat32 :: BinaryValue Float32 F.Float32 instance binaryValueFloat64 :: BinaryValue Float64 Number + +-- | Type-level map of `TypedArray` to its element type name. +class ShowArrayViewType (a :: ArrayViewType) (name :: Symbol) | a -> name + +instance showArrayViewTypeUint8Clamped :: ShowArrayViewType Uint8Clamped "Uint8Clamped" +instance showArrayViewTypeViewUint32 :: ShowArrayViewType Uint32 "Uint32" +instance showArrayViewTypeViewUint16 :: ShowArrayViewType Uint16 "Uint16" +instance showArrayViewTypeViewUint8 :: ShowArrayViewType Uint8 "Uint8" +instance showArrayViewTypeViewInt32 :: ShowArrayViewType Int32 "Int32" +instance showArrayViewTypeViewInt16 :: ShowArrayViewType Int16 "Int16" +instance showArrayViewTypeViewInt8 :: ShowArrayViewType Int8 "Int8" +instance showArrayViewTypeViewFloat32 :: ShowArrayViewType Float32 "Float32" +instance showArrayViewTypeViewFloat64 :: ShowArrayViewType Float64 "Float64" diff --git a/test/Main.purs b/test/Main.purs index 40066b3..4ad0759 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -8,6 +8,5 @@ import Test.Properties (propertiesTests) main :: Effect Unit main = do - log "Starting tests..." propertiesTests diff --git a/test/Properties.purs b/test/Properties.purs index 77f0d14..bb529fc 100644 --- a/test/Properties.purs +++ b/test/Properties.purs @@ -6,7 +6,7 @@ import Effect.Ref (new, read) as Ref import Prelude (Unit, bind, discard, ($), (<>), (*), show) import Test.Properties.DataView (dataViewTests) import Test.Properties.TypedArray (typedArrayTests) - +import Test.Properties.Typed.Laws (typedArrayLaws) propertiesTests :: Effect Unit propertiesTests = do @@ -14,6 +14,7 @@ propertiesTests = do count <- Ref.new 0 log " - TypedArray Tests:" typedArrayTests count + typedArrayLaws count c <- Ref.read count log $ " - Verified " <> show c <> " properties, generating " <> show (c * 9 * 100) <> " test cases." diff --git a/test/Properties/ArrayBuffer.purs b/test/Properties/ArrayBuffer.purs index 3be4160..8ab6fe4 100644 --- a/test/Properties/ArrayBuffer.purs +++ b/test/Properties/ArrayBuffer.purs @@ -1,2 +1 @@ module Test.Properties.ArrayBuffer where - diff --git a/test/Properties/DataView.purs b/test/Properties/DataView.purs index d399e1b..e7199bf 100644 --- a/test/Properties/DataView.purs +++ b/test/Properties/DataView.purs @@ -1,117 +1,127 @@ module Test.Properties.DataView where - import Prelude +import Data.Array.Partial (head) as Array import Data.ArrayBuffer.DataView as DV import Data.ArrayBuffer.DataView.Gen (genDataView, genWithOffsetAndValue, WithOffsetAndValue(..)) import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genUint16, genUint32, genUint8) -import Data.ArrayBuffer.Types (Uint32, Uint16, Uint8, Int32, Int16, Int8, Float32, Float64) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) +import Data.ArrayBuffer.Types (Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8) +import Data.ArrayBuffer.ValueMapping (class BinaryValue, class BytesPerType, class ShowArrayViewType) +import Data.Float32 (Float32) as F import Data.Maybe (Maybe(..)) -import Data.Typelevel.Num (class Nat, D1, D2, D4, D8) +import Data.Symbol (class IsSymbol) import Data.UInt (UInt) -import Data.Vec (head) as Vec import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) +import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, quickCheckGen, Result, (===)) - - +import Type.Proxy (Proxy(..)) dataViewTests :: Ref Int -> Effect Unit dataViewTests count = do log " - setBE x o => getBE o === Just x" - placingAValueIsThereTestsBE count + placingAValueIsThereTests DV.BE count log " - setLE x o => getLE o === Just x" - placingAValueIsThereTestsLE count - + placingAValueIsThereTests DV.LE count -type TestableViewF a b n t q = - Show t +type TestableViewF a name t q = + Show t => Eq t => Ord t => Semiring t - => BytesPerValue a b - => Nat b - => DV.DataView a t - => WithOffsetAndValue n a t + => BytesPerType a + => BinaryValue a t + => ShowArrayViewType a name + => IsSymbol name + => WithOffsetAndValue a t -> q - -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableViewF a b n t q) -> Effect Unit +overAll + :: forall q + . Testable q + => Ref Int + -> (forall a name t. TestableViewF a name t q) + -> Effect Unit overAll count f = do - void (Ref.modify (\x -> x + 1) count) + void (Ref.modify (_ + 1) count) log " - Uint32" - quickCheckGen $ - let f' :: TestableViewF Uint32 D4 n UInt q - f' = f - in f' <$> genWithOffsetAndValue genDataView genUint32 + quickCheckGen do + let + f' :: TestableViewF Uint32 "Uint32" UInt q + f' = f + + f' <$> genWithOffsetAndValue 4 genDataView genUint32 log " - Uint16" - quickCheckGen $ - let f' :: TestableViewF Uint16 D2 n UInt q - f' = f - in f' <$> genWithOffsetAndValue genDataView genUint16 + quickCheckGen do + let + f' :: TestableViewF Uint16 "Uint16" UInt q + f' = f + + f' <$> genWithOffsetAndValue 2 genDataView genUint16 log " - Uint8" - quickCheckGen $ - let f' :: TestableViewF Uint8 D1 n UInt q - f' = f - in f' <$> genWithOffsetAndValue genDataView genUint8 + quickCheckGen do + let + f' :: TestableViewF Uint8 "Uint8" UInt q + f' = f + + f' <$> genWithOffsetAndValue 1 genDataView genUint8 log " - Int32" - quickCheckGen $ - let f' :: TestableViewF Int32 D4 n Int q - f' = f - in f' <$> genWithOffsetAndValue genDataView genInt32 + quickCheckGen do + let + f' :: TestableViewF Int32 "Int32" Int q + f' = f + + f' <$> genWithOffsetAndValue 4 genDataView genInt32 log " - Int16" - quickCheckGen $ - let f' :: TestableViewF Int16 D2 n Int q - f' = f - in f' <$> genWithOffsetAndValue genDataView genInt16 + quickCheckGen do + let + f' :: TestableViewF Int16 "Int16" Int q + f' = f + + f' <$> genWithOffsetAndValue 2 genDataView genInt16 log " - Int8" - quickCheckGen $ - let f' :: TestableViewF Int8 D1 n Int q - f' = f - in f' <$> genWithOffsetAndValue genDataView genInt8 + quickCheckGen do + let + f' :: TestableViewF Int8 "Int8" Int q + f' = f - log " - Float32" - quickCheckGen $ - let f' :: TestableViewF Float32 D4 n Number q - f' = f - in f' <$> genWithOffsetAndValue genDataView genFloat32 + f' <$> genWithOffsetAndValue 1 genDataView genInt8 - log " - Float64" - quickCheckGen $ - let f' :: TestableViewF Float64 D8 n Number q - f' = f - in f' <$> genWithOffsetAndValue genDataView genFloat64 + log " - Float32" + quickCheckGen do + let + f' :: TestableViewF Float32 "Float32" F.Float32 q + f' = f + f' <$> genWithOffsetAndValue 4 genDataView genFloat32 -placingAValueIsThereTestsBE :: Ref Int -> Effect Unit -placingAValueIsThereTestsBE count = overAll count placingAValueIsThere - where - placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result - placingAValueIsThere (WithOffsetAndValue os t xs) = - let o = Vec.head os - in unsafePerformEffect do - _ <- DV.setBE (DV.AProxy :: DV.AProxy a) xs t o - my <- DV.getBE (DV.AProxy :: DV.AProxy a) xs o - pure (my === Just t) + log " - Float64" + quickCheckGen do + let + f' :: TestableViewF Float64 "Float64" Number q + f' = f + f' <$> genWithOffsetAndValue 8 genDataView genFloat64 -placingAValueIsThereTestsLE :: Ref Int -> Effect Unit -placingAValueIsThereTestsLE count = overAll count placingAValueIsThere +placingAValueIsThereTests :: DV.Endian -> Ref Int -> Effect Unit +placingAValueIsThereTests endian count = overAll count placingAValueIsThere where - placingAValueIsThere :: forall a b t. TestableViewF a b D1 t Result - placingAValueIsThere (WithOffsetAndValue os t xs) = - let o = Vec.head os - in unsafePerformEffect do - _ <- DV.setLE (DV.AProxy :: DV.AProxy a) xs t o - my <- DV.getLE (DV.AProxy :: DV.AProxy a) xs o - pure (my === Just t) + placingAValueIsThere :: forall a name t. TestableViewF a name t Result + placingAValueIsThere (WithOffsetAndValue os t xs) = do + let + o = unsafePartial $ Array.head os + prx = Proxy :: Proxy a + + unsafePerformEffect do + _ <- DV.set endian prx xs o t + my <- DV.get endian prx xs o + pure (my === Just t) diff --git a/test/Properties/Typed/Laws.purs b/test/Properties/Typed/Laws.purs new file mode 100644 index 0000000..21ac0dd --- /dev/null +++ b/test/Properties/Typed/Laws.purs @@ -0,0 +1,149 @@ +module Test.Properties.Typed.Laws where + +import Prelude +import Data.ArrayBuffer.Typed (class TypedArray, toString) +import Data.ArrayBuffer.Typed.Gen (genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8) +import Data.ArrayBuffer.Types (ArrayView, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, Uint8Clamped, ArrayViewType) +import Data.Float32 as F +import Data.UInt (UInt) +import Effect (Effect) +import Effect.Ref (Ref) +import Effect.Ref as Ref +import Test.QuickCheck (class Arbitrary, arbitrary) +import Test.QuickCheck.Gen (Gen) +import Test.QuickCheck.Laws.Data (checkEq, checkMonoid, checkOrd, checkSemigroup) +import Type.Proxy (Proxy(..)) +import Data.ArrayBuffer.Typed as TA +import Data.Generic.Rep (class Generic) +import Data.Maybe (Maybe(..)) +import Effect.Unsafe (unsafePerformEffect) + +newtype A a = A a + +foreign import data ArrayElt :: ArrayViewType -> Type -> Type + +class ArrayEl (a :: ArrayViewType) (t :: Type) where + arb :: Proxy (ArrayView a) -> Gen t + +instance arrayElUint8Clamped :: ArrayEl Uint8Clamped UInt where + arb _ = genUint8 + +instance arrayElUint32 :: ArrayEl Uint32 UInt where + arb _ = genUint32 + +instance arrayElUint16 :: ArrayEl Uint16 UInt where + arb _ = genUint16 + +instance arrayElUint8 :: ArrayEl Uint8 UInt where + arb _ = genUint8 + +instance arrayElInt32 :: ArrayEl Int32 Int where + arb _ = genInt32 + +instance arrayElInt16 :: ArrayEl Int16 Int where + arb _ = genInt16 + +instance arrayElInt8 :: ArrayEl Int8 Int where + arb _ = genInt8 + +instance arrayElFloat32 :: ArrayEl Float32 F.Float32 where + arb _ = genFloat32 + +instance arrayElFloat64 :: ArrayEl Float64 Number where + arb _ = genFloat64 + +instance arbitraryAAV :: (TypedArray a t, ArrayEl a t) => Arbitrary (A (AV a t)) where + arbitrary = (A <<< AV) <$> genTypedArray (arb (Proxy :: Proxy (ArrayView a))) + +derive newtype instance eqA :: Eq t => Eq (A t) +derive newtype instance ordA :: Ord t => Ord (A t) +derive newtype instance semigroupA :: Semigroup t => Semigroup (A t) +derive newtype instance monoidA :: Monoid t => Monoid (A t) + +typedArrayLaws :: Ref Int -> Effect Unit +typedArrayLaws count = do + do + let f = checkEq + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkOrd + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkSemigroup + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + + do + let f = checkMonoid + void $ Ref.modify (_ + 1) count + f (Proxy :: Proxy (A (AV Float32 F.Float32))) + f (Proxy :: Proxy (A (AV Float64 Number))) + f (Proxy :: Proxy (A (AV Int16 Int))) + f (Proxy :: Proxy (A (AV Int32 Int))) + f (Proxy :: Proxy (A (AV Int8 Int))) + f (Proxy :: Proxy (A (AV Uint16 UInt))) + f (Proxy :: Proxy (A (AV Uint32 UInt))) + f (Proxy :: Proxy (A (AV Uint8 UInt))) + f (Proxy :: Proxy (A (AV Uint8Clamped UInt))) + +newtype AV :: forall k. ArrayViewType -> k -> Type +newtype AV a t = AV (ArrayView a) + +derive instance genericAV :: Generic (AV a t) _ + +instance ordArrayView :: (TypedArray a t, Ord t) => Ord (AV a t) where + compare (AV a) (AV b) = unsafePerformEffect $ TA.compare a b + +instance eqArrayView :: (TypedArray a t, Eq t) => Eq (AV a t) where + eq (AV a) (AV b) = unsafePerformEffect $ TA.eq a b + +instance showArrayView :: (TypedArray a t, Show t) => Show (AV a t) where + show (AV a) = "T[" <> s <> "]" + where + s = unsafePerformEffect $ toString a + +instance semigroupArrayView :: TypedArray a t => Semigroup (AV a t) where + append (AV a) (AV b) = unsafePerformEffect do + let + la = TA.length a + lb = TA.length b + r <- TA.empty $ la + lb + void $ TA.setTyped r (Just 0) a + void $ TA.setTyped r (Just la) b + pure $ AV r + +instance monoidArrayView :: TypedArray a t => Monoid (AV a t) where + mempty = AV $ unsafePerformEffect $ TA.empty 0 + +instance arbitraryArrayView :: (TypedArray a t, Arbitrary t) => Arbitrary (AV a t) where + arbitrary = do + xs <- arbitrary + pure $ unsafePerformEffect $ AV <$> TA.fromArray xs diff --git a/test/Properties/TypedArray.purs b/test/Properties/TypedArray.purs index e570952..36dfc8c 100644 --- a/test/Properties/TypedArray.purs +++ b/test/Properties/TypedArray.purs @@ -1,20 +1,18 @@ module Test.Properties.TypedArray where - import Prelude import Control.Monad.Gen (suchThat) -import Data.Array (drop, take) -import Data.Array as Array +import Data.Array (all, cons, drop, length, reverse, slice, snoc, sort, take, unsafeIndex) as Array +import Data.Array.Partial (head) as Array import Data.ArrayBuffer.Typed (class TypedArray) import Data.ArrayBuffer.Typed as TA -import Data.ArrayBuffer.Typed.Gen (WithOffset(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithOffset) -import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint8Array, Uint8ClampedArray, Uint32Array) -import Data.ArrayBuffer.ValueMapping (class BytesPerValue) -import Data.Maybe (Maybe(..)) +import Data.ArrayBuffer.Typed.Gen (WithIndices(..), genFloat32, genFloat64, genInt16, genInt32, genInt8, genTypedArray, genUint16, genUint32, genUint8, genWithIndices) +import Data.ArrayBuffer.Types (ArrayView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray) +import Data.ArrayBuffer.ValueMapping (class BytesPerType, byteWidth) +import Data.Maybe (Maybe(..), fromMaybe, isJust) +import Data.Traversable (traverse) import Data.Tuple (Tuple(..)) -import Data.Typelevel.Num (class Nat, D0, D1, D5, toInt') -import Data.Vec (head) as Vec import Effect (Effect) import Effect.Console (log) import Effect.Ref (Ref) @@ -22,15 +20,17 @@ import Effect.Ref as Ref import Effect.Unsafe (unsafePerformEffect) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Testable, Result(..), quickCheckGen, (/==), (), (===)) -import Test.QuickCheck.Combinators ((==>), (|=|)) import Test.QuickCheck.Gen (Gen) import Type.Proxy (Proxy(..)) - typedArrayTests :: Ref Int -> Effect Unit typedArrayTests count = do + log " - subarrayBehavesLikeArraySlice" + subarrayBehavesLikeArraySliceTests count + log " - sliceBehavesLikeArraySlice" + sliceBehavesLikeArraySliceTests count log " - partBehavesLikeTakeDrop" - partBehavesLikeTakeDrop count + partBehavesLikeTakeDropTests count log " - byteLength x / bytesPerValue === length x" byteLengthDivBytesPerValueTests count log " - fromArray (toArray x) === x" @@ -48,9 +48,9 @@ typedArrayTests count = do log " - filter p (filter p x) == filter p x" filterIsIdempotentTests count log " - forall os `in` xs. all (\\o -> hasIndex o xs)" - withOffsetHasIndexTests count + withIndicesHasIndexTests count log " - forall os `in` xs. all (\\o -> elem (at o xs) xs)" - withOffsetElemTests count + withIndicesElemTests count log " - any p x => p (find p x)" anyImpliesFindTests count log " - p (at x (findIndex p x))" @@ -75,7 +75,7 @@ typedArrayTests count = do sortIsIdempotentTests count log " - toArray (sort x) == Array.sort (toArray x)" sortIsArraySortTests count - log " - toString' \",\" x == toString x" + log " - join \",\" x == toString x" toStringIsJoinWithCommaTests count log " - setTyped x (subArray x) == x" setTypedOfSubArrayIsIdentityTests count @@ -92,9 +92,7 @@ typedArrayTests count = do log " - let z' = subArray 0 (length x) x; q = toArray x; mutate z'; pure q /= toArray x" modifyingSubArrayMutatesOriginalAllTests count log " - let z' = subArray o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPartTests count - log " - let z' = subArray 0 o x; q = toArray z'; mutate x; pure q == toArray z'" - modifyingOriginalDoesntMutateSubArrayPart2Tests count + modifyingOriginalMutatesSubArrayPartTests count log " - let z' = slice x; q = toArray z'; mutate x; pure q == toArray z'" modifyingOriginalDoesntMutateSliceTests count log " - let z' = slice o x; q = toArray z'; mutate x; pure q == toArray z'" @@ -108,537 +106,617 @@ typedArrayTests count = do log " - copyWithin o x == setTyped x (slice o x)" copyWithinViaSetTypedTests count - - -type TestableArrayF a b n t q = - Show t +type TestableArrayF a t q = + Show t => Eq t => Ord t => Semiring t => TypedArray a t - => BytesPerValue a b - => Nat b - => WithOffset n a - -> q - - -overAll' :: forall q n. Testable q => Nat n => Int -> Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll' mn count f = do - void (Ref.modify (\x -> x + 1) count) - - let chk :: forall a b t. Show t => Eq t => Ord t => Semiring t => Nat b => BytesPerValue a b => TypedArray a t => String -> Proxy (ArrayView a) -> Gen t -> Effect Unit - chk s _ gen = do - log $ " - " <> s - quickCheckGen $ f <$> genWithOffset arr - where arr :: Gen (ArrayView a) - arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs - - chk "Uint8ClampedArray" (Proxy :: Proxy Uint8ClampedArray) genUint8 - chk "Uint32Array" (Proxy :: Proxy Uint32Array) genUint32 - chk "Uint16Array" (Proxy :: Proxy Uint16Array) genUint16 - chk "Uint8Array" (Proxy :: Proxy Uint8Array) genUint8 - chk "Int32Array" (Proxy :: Proxy Int32Array) genInt32 - chk "Int16Array" (Proxy :: Proxy Int16Array) genInt16 - chk "Int8Array" (Proxy :: Proxy Int8Array) genInt8 - chk "Float32Array" (Proxy :: Proxy Float32Array) genFloat32 - chk "Float64Array" (Proxy :: Proxy Float64Array) genFloat64 - - -overAll :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll count f = overAll' 0 count f - -overAll1 :: forall q n. Testable q => Nat n => Ref Int -> (forall a b t. TestableArrayF a b n t q) -> Effect Unit -overAll1 count f = overAll' 1 count f - -partBehavesLikeTakeDrop :: Ref Int -> Effect Unit -partBehavesLikeTakeDrop count = overAll count f - where - f :: forall a b t. TestableArrayF a b D0 t Result - f (WithOffset _ a) = - let n = 2 - na = TA.toArray a - ba = TA.buffer a - pa :: ArrayView a - pa = TA.part ba n n - in take n (drop n na) === TA.toArray pa + => BytesPerType a + => WithIndices a + -> Effect q + +overAll' + :: forall q + . Testable q + => Int -- n + -> Int -- “minimum n”? + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit +overAll' n mn count f = do + void (Ref.modify (_ + 1) count) + + let + chk + :: forall a t + . Show t + => Eq t + => Ord t + => Semiring t + => BytesPerType a + => TypedArray a t + => String + -> Int + -> Proxy (ArrayView a) + -> Gen t + -> Effect Unit + chk s n' _ gen = do + log $ " - " <> s + quickCheckGen $ unsafePerformEffect <<< f <$> genWithIndices n' arr + where + arr :: Gen (ArrayView a) + arr = genTypedArray gen `suchThat` \xs -> mn <= TA.length xs + + chk "Uint8ClampedArray" n (Proxy :: Proxy Uint8ClampedArray) genUint8 + chk "Uint32Array" n (Proxy :: Proxy Uint32Array) genUint32 + chk "Uint16Array" n (Proxy :: Proxy Uint16Array) genUint16 + chk "Uint8Array" n (Proxy :: Proxy Uint8Array) genUint8 + chk "Int32Array" n (Proxy :: Proxy Int32Array) genInt32 + chk "Int16Array" n (Proxy :: Proxy Int16Array) genInt16 + chk "Int8Array" n (Proxy :: Proxy Int8Array) genInt8 + chk "Float32Array" n (Proxy :: Proxy Float32Array) genFloat32 + chk "Float64Array" n (Proxy :: Proxy Float64Array) genFloat64 + +overAll + :: forall q + . Testable q + => Int + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit +overAll n count f = overAll' n 0 count f + +overAll1 + :: forall q + . Testable q + => Int + -> Ref Int + -> (forall a t. TestableArrayF a t q) + -> Effect Unit +overAll1 n count f = overAll' n 1 count f + +subarrayBehavesLikeArraySliceTests :: Ref Int -> Effect Unit +subarrayBehavesLikeArraySliceTests count = overAll 2 count f + where + f :: forall a t. TestableArrayF a t Result + f (WithIndices os xs) = do + let + s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 + axs <- TA.toArray xs + let sxs = TA.subArray s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a + +sliceBehavesLikeArraySliceTests :: Ref Int -> Effect Unit +sliceBehavesLikeArraySliceTests count = overAll 2 count f + where + f :: forall a t. TestableArrayF a t Result + f (WithIndices os xs) = do + let + s = unsafePartial $ os `Array.unsafeIndex` 0 + e = unsafePartial $ os `Array.unsafeIndex` 1 + axs <- TA.toArray xs + sxs <- TA.slice s e xs + a <- TA.toArray sxs + pure $ Array.slice s e axs === a + +partBehavesLikeTakeDropTests :: Ref Int -> Effect Unit +partBehavesLikeTakeDropTests count = overAll 0 count f + where + f :: forall a t. TestableArrayF a t Result + f (WithIndices _ xs) = do + let n = 2 + axs <- TA.toArray xs + pxs <- TA.part (TA.buffer xs) n n :: Effect (ArrayView a) + aps <- TA.toArray pxs + pure $ Array.take n (Array.drop n axs) === aps byteLengthDivBytesPerValueTests :: Ref Int -> Effect Unit -byteLengthDivBytesPerValueTests count = overAll count byteLengthDivBytesPerValueEqLength +byteLengthDivBytesPerValueTests count = overAll 0 count byteLengthDivBytesPerValueEqLength where - byteLengthDivBytesPerValueEqLength :: forall a b t. TestableArrayF a b D0 t Result - byteLengthDivBytesPerValueEqLength (WithOffset _ a) = - let b = toInt' (Proxy :: Proxy b) - in TA.length a === (TA.byteLength a `div` b) + byteLengthDivBytesPerValueEqLength :: forall a t. TestableArrayF a t Result + byteLengthDivBytesPerValueEqLength (WithIndices _ xs) = do + let b = byteWidth (Proxy :: Proxy a) + pure $ TA.length xs === (TA.byteLength xs `div` b) fromArrayToArrayIsoTests :: Ref Int -> Effect Unit -fromArrayToArrayIsoTests count = overAll count fromArrayToArrayIso +fromArrayToArrayIsoTests count = overAll 0 count fromArrayToArrayIso where - fromArrayToArrayIso :: forall a b t. TestableArrayF a b D0 t Result - fromArrayToArrayIso (WithOffset _ x) = - TA.toArray (TA.fromArray (TA.toArray x) :: ArrayView a) === TA.toArray x - + fromArrayToArrayIso :: forall a t. TestableArrayF a t Result + fromArrayToArrayIso (WithIndices _ xs) = do + axs <- TA.toArray xs + xs' <- TA.fromArray axs :: Effect (ArrayView a) + axs' <- TA.toArray xs' + pure $ axs' === axs allAreFilledTests :: Ref Int -> Effect Unit -allAreFilledTests count = overAll count allAreFilled - where - allAreFilled :: forall a b t. TestableArrayF a b D0 t Result - allAreFilled (WithOffset _ xs) = unsafePerformEffect do - let x = case TA.at xs 0 of - Nothing -> zero - Just y -> y - TA.fill xs x Nothing - let b = TA.all (\y -> y == x) xs - pure (b "All aren't the filled value") - +allAreFilledTests count = overAll 0 count allAreFilled + where + allAreFilled :: forall a t. TestableArrayF a t Result + allAreFilled (WithIndices _ xs) = do + e <- TA.at xs 0 + let + x = fromMaybe zero e + l = TA.length xs + TA.fill x 0 l xs + b <- TA.all (_ == x) xs + pure (b "All aren't the filled value") setSingletonIsEqTests :: Ref Int -> Effect Unit -setSingletonIsEqTests count = overAll count setSingletonIsEq - where - setSingletonIsEq :: forall a b t. TestableArrayF a b D1 t Result - setSingletonIsEq (WithOffset os xs) = unsafePerformEffect do - case TA.at xs 0 of - Nothing -> pure Success - Just x -> do - _ <- TA.set xs (Just (Vec.head os)) [x] - pure (TA.at xs (Vec.head os) === Just x) - +setSingletonIsEqTests count = overAll 1 count setSingletonIsEq + where + setSingletonIsEq :: forall a t. TestableArrayF a t Result + setSingletonIsEq (WithIndices os xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just x -> do + let o = unsafePartial $ Array.head os + _ <- TA.set xs (Just o) [ x ] + e' <- TA.at xs o + pure $ e' === Just x -- | Should work with any arbitrary predicate, but we can't generate them allImpliesAnyTests :: Ref Int -> Effect Unit -allImpliesAnyTests count = overAll count allImpliesAny +allImpliesAnyTests count = overAll 0 count allImpliesAny where - allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result - allImpliesAny (WithOffset _ xs) = - let pred x = x /= zero - all' = TA.all pred xs "All don't satisfy the predicate" - any' = TA.any pred xs "None satisfy the predicate" - in (TA.length xs === zero) |=| all' ==> any' - + allImpliesAny :: forall a t. TestableArrayF a t Result + allImpliesAny (WithIndices _ xs) = do + let pred x = x /= zero + all'' <- TA.all pred xs + let all' = all'' "All don't satisfy the predicate" + any'' <- TA.any pred xs + let any' = any'' "None satisfy the predicate" + pure $ (TA.length xs === zero) `xor` all' `implies` any' -- | Should work with any arbitrary predicate, but we can't generate them filterImpliesAllTests :: Ref Int -> Effect Unit -filterImpliesAllTests count = overAll count filterImpliesAll +filterImpliesAllTests count = overAll 0 count filterImpliesAll where - filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result - filterImpliesAll (WithOffset _ xs) = - let pred x = x /= zero - ys = TA.filter pred xs - all' = TA.all pred ys - in all' "Filter doesn't imply all" - + filterImpliesAll :: forall a t. TestableArrayF a t Result + filterImpliesAll (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + all' <- TA.all pred ys + pure $ all' "Filter doesn't imply all" -- | Should work with any arbitrary predicate, but we can't generate them filterIsTotalTests :: Ref Int -> Effect Unit -filterIsTotalTests count = overAll count filterIsTotal +filterIsTotalTests count = overAll 0 count filterIsTotal where - filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result - filterIsTotal (WithOffset _ xs) = - let pred x = x /= zero - ys = TA.filter pred xs - zs = TA.filter (\x -> not pred x) ys - in TA.toArray zs === [] - + filterIsTotal :: forall a t. TestableArrayF a t Result + filterIsTotal (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + zs <- TA.filter (not pred) ys + azs <- TA.toArray zs + pure $ azs === [] -- | Should work with any arbitrary predicate, but we can't generate them filterIsIdempotentTests :: Ref Int -> Effect Unit -filterIsIdempotentTests count = overAll count filterIsIdempotent +filterIsIdempotentTests count = overAll 0 count filterIsIdempotent where - filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - filterIsIdempotent (WithOffset _ xs) = - let pred x = x /= zero - ys = TA.filter pred xs - zs = TA.filter pred ys - in TA.toArray zs === TA.toArray ys - + filterIsIdempotent :: forall a t. TestableArrayF a t Result + filterIsIdempotent (WithIndices _ xs) = do + let pred x = x /= zero + ys <- TA.filter pred xs + zs <- TA.filter pred ys + azs <- TA.toArray zs + ays <- TA.toArray ys + pure $ azs === ays -withOffsetHasIndexTests :: Ref Int -> Effect Unit -withOffsetHasIndexTests count = overAll1 count withOffsetHasIndex +withIndicesHasIndexTests :: Ref Int -> Effect Unit +withIndicesHasIndexTests count = overAll1 5 count withIndicesHasIndex where - withOffsetHasIndex :: forall a b t. TestableArrayF a b D5 t Result - withOffsetHasIndex (WithOffset os xs) = - Array.all (\o -> TA.hasIndex xs o) os "All doesn't have index of itself" - + withIndicesHasIndex :: forall a t. TestableArrayF a t Result + withIndicesHasIndex (WithIndices os xs) = pure $ + Array.all (TA.hasIndex xs) os "All doesn't have index of itself" -withOffsetElemTests :: Ref Int -> Effect Unit -withOffsetElemTests count = overAll1 count withOffsetElem +withIndicesElemTests :: Ref Int -> Effect Unit +withIndicesElemTests count = overAll1 5 count withIndicesElem where - withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result - withOffsetElem (WithOffset os xs) = - Array.all (\o -> TA.elem (unsafePartial (TA.unsafeAt xs o)) Nothing xs) os - "All doesn't have an elem of itself" - + withIndicesElem :: forall a t. TestableArrayF a t Result + withIndicesElem (WithIndices os xs) = do + let fetch o = TA.at xs o + exs <- traverse fetch os + pure $ Array.all isJust exs "All doesn't have an elem of itself" -- | Should work with any arbitrary predicate, but we can't generate them anyImpliesFindTests :: Ref Int -> Effect Unit -anyImpliesFindTests count = overAll count anyImpliesFind - where - anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result - anyImpliesFind (WithOffset _ xs) = - let pred x = x /= zero - p = TA.any pred xs "All don't satisfy the predicate" - q = - case TA.find pred xs of - Nothing -> Failed "Doesn't have a value satisfying the predicate" - Just z -> if pred z - then Success - else Failed "Found value doesn't satisfy the predicate" - in p ==> q - +anyImpliesFindTests count = overAll 0 count anyImpliesFind + where + anyImpliesFind :: forall a t. TestableArrayF a t Result + anyImpliesFind (WithIndices _ xs) = do + let pred x = x /= zero + a <- TA.any pred xs + let p = a "All don't satisfy the predicate" + idx <- TA.find pred xs + let + q = case idx of + Nothing -> Failed "Doesn't have a value satisfying the predicate" + Just z -> + if pred z then Success + else Failed "Found value doesn't satisfy the predicate" + pure $ p `implies` q -- | Should work with any arbitrary predicate, but we can't generate them findIndexImpliesAtTests :: Ref Int -> Effect Unit -findIndexImpliesAtTests count = overAll count findIndexImpliesAt - where - findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - findIndexImpliesAt (WithOffset _ xs) = - let pred x o = x /= zero - mo = TA.findIndex pred xs - in case mo of - Nothing -> Success - Just o -> case TA.at xs o of - Nothing -> Failed "No value at found index" - Just x -> pred x o "Find index implies at" - - +findIndexImpliesAtTests count = overAll 0 count findIndexImpliesAt + where + findIndexImpliesAt :: forall a t. TestableArrayF a t Result + findIndexImpliesAt (WithIndices _ xs) = do + let pred x _ = x /= zero + mo <- TA.findIndex pred xs + case mo of + Nothing -> pure Success + Just o -> do + e <- TA.at xs o + case e of + Nothing -> pure $ Failed "No value at found index" + Just x -> pure $ pred x o "Find index implies at" indexOfImpliesAtTests :: Ref Int -> Effect Unit -indexOfImpliesAtTests count = overAll count indexOfImpliesAt - where - indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - indexOfImpliesAt (WithOffset _ xs) = - case TA.at xs 0 of - Nothing -> Success - Just y -> case TA.indexOf y Nothing xs of - Nothing -> Failed "no index of" - Just o -> TA.at xs o === Just y - +indexOfImpliesAtTests count = overAll 1 count indexOfImpliesAt + where + indexOfImpliesAt :: forall a t. TestableArrayF a t Result + indexOfImpliesAt (WithIndices _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just y -> do + idx <- TA.indexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no index of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit -lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt - where - lastIndexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result - lastIndexOfImpliesAt (WithOffset _ xs) = - case TA.at xs 0 of - Nothing -> Success - Just y -> case TA.lastIndexOf y Nothing xs of - Nothing -> Failed "no lastIndex of" - Just o -> TA.at xs o === Just y - +lastIndexOfImpliesAtTests count = overAll 0 count lastIndexOfImpliesAt + where + lastIndexOfImpliesAt :: forall a t. TestableArrayF a t Result + lastIndexOfImpliesAt (WithIndices _ xs) = do + e <- TA.at xs 0 + case e of + Nothing -> pure Success + Just y -> do + idx <- TA.lastIndexOf y Nothing xs + case idx of + Nothing -> pure $ Failed "no lastIndex of" + Just o -> do + e' <- TA.at xs o + pure $ e' === Just y foldrConsIsToArrayTests :: Ref Int -> Effect Unit -foldrConsIsToArrayTests count = overAll count foldrConsIsToArray +foldrConsIsToArrayTests count = overAll 0 count foldrConsIsToArray where - foldrConsIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldrConsIsToArray (WithOffset _ xs) = - TA.foldr (\x acc -> Array.cons x acc) [] xs === TA.toArray xs - + foldrConsIsToArray :: forall a t. TestableArrayF a t Result + foldrConsIsToArray (WithIndices _ xs) = do + axs <- TA.toArray xs + rxs <- TA.foldr Array.cons [] xs + pure $ rxs === axs foldlSnocIsToArrayTests :: Ref Int -> Effect Unit -foldlSnocIsToArrayTests count = overAll count foldlSnocIsToArray +foldlSnocIsToArrayTests count = overAll 0 count foldlSnocIsToArray where - foldlSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - foldlSnocIsToArray (WithOffset _ xs) = - TA.foldl (\acc x -> Array.snoc acc x) [] xs === TA.toArray xs - + foldlSnocIsToArray :: forall a t. TestableArrayF a t Result + foldlSnocIsToArray (WithIndices _ xs) = do + axs <- TA.toArray xs + rxs <- TA.foldl Array.snoc [] xs + pure $ rxs === axs mapIdentityIsIdentityTests :: Ref Int -> Effect Unit -mapIdentityIsIdentityTests count = overAll count mapIdentityIsIdentity +mapIdentityIsIdentityTests count = overAll 0 count mapIdentityIsIdentity where - mapIdentityIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - mapIdentityIsIdentity (WithOffset _ xs) = - TA.toArray (TA.map identity xs) === TA.toArray xs - + mapIdentityIsIdentity :: forall a t. TestableArrayF a t Result + mapIdentityIsIdentity (WithIndices _ xs) = do + axs <- TA.toArray xs + mxs <- TA.toArray (TA.map identity xs) + pure $ axs === mxs traverseSnocIsToArrayTests :: Ref Int -> Effect Unit -traverseSnocIsToArrayTests count = overAll count traverseSnocIsToArray +traverseSnocIsToArrayTests count = overAll 0 count traverseSnocIsToArray where - traverseSnocIsToArray :: forall a b t. TestableArrayF a b D0 t Result - traverseSnocIsToArray (WithOffset _ xs) = - let ys = unsafePerformEffect do - ref <- Ref.new [] - TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs - Ref.read ref - in TA.toArray xs === ys - + traverseSnocIsToArray :: forall a t. TestableArrayF a t Result + traverseSnocIsToArray (WithIndices _ xs) = do + ref <- Ref.new [] + TA.traverse_ (\x -> void (Ref.modify (\xs' -> Array.snoc xs' x) ref)) xs + ys <- Ref.read ref + axs <- TA.toArray xs + pure $ axs === ys doubleReverseIsIdentityTests :: Ref Int -> Effect Unit -doubleReverseIsIdentityTests count = overAll count doubleReverseIsIdentity +doubleReverseIsIdentityTests count = overAll 0 count doubleReverseIsIdentity where - doubleReverseIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - doubleReverseIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - _ = unsafePerformEffect do - TA.reverse xs - TA.reverse xs - in TA.toArray xs === ys - + doubleReverseIsIdentity :: forall a t. TestableArrayF a t Result + doubleReverseIsIdentity (WithIndices _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + TA.reverse xs + axs' <- TA.toArray xs + pure $ axs === axs' reverseIsArrayReverseTests :: Ref Int -> Effect Unit -reverseIsArrayReverseTests count = overAll count reverseIsArrayReverse +reverseIsArrayReverseTests count = overAll 0 count reverseIsArrayReverse where - reverseIsArrayReverse :: forall a b t. TestableArrayF a b D0 t Result - reverseIsArrayReverse (WithOffset _ xs) = - let ys = Array.reverse (TA.toArray xs) - _ = unsafePerformEffect do - TA.reverse xs - in TA.toArray xs === ys - + reverseIsArrayReverse :: forall a t. TestableArrayF a t Result + reverseIsArrayReverse (WithIndices _ xs) = do + axs <- TA.toArray xs + TA.reverse xs + rxs <- TA.toArray xs + pure $ Array.reverse axs === rxs sortIsIdempotentTests :: Ref Int -> Effect Unit -sortIsIdempotentTests count = overAll count sortIsIdempotent +sortIsIdempotentTests count = overAll 0 count sortIsIdempotent where - sortIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result - sortIsIdempotent (WithOffset _ xs) = - let ys = unsafePerformEffect do - TA.sort xs - pure (TA.toArray xs) - zs = unsafePerformEffect do - TA.sort xs - pure (TA.toArray xs) - in zs === ys - + sortIsIdempotent :: forall a t. TestableArrayF a t Result + sortIsIdempotent (WithIndices _ xs) = do + TA.sort xs + ys <- TA.toArray xs + TA.sort xs + zs <- TA.toArray xs + pure $ zs === ys sortIsArraySortTests :: Ref Int -> Effect Unit -sortIsArraySortTests count = overAll count sortIsArraySort +sortIsArraySortTests count = overAll 0 count sortIsArraySort where - sortIsArraySort :: forall a b t. TestableArrayF a b D0 t Result - sortIsArraySort (WithOffset _ xs) = - let ys = Array.sort (TA.toArray xs) - _ = unsafePerformEffect do - TA.sort xs - in TA.toArray xs === ys - + sortIsArraySort :: forall a t. TestableArrayF a t Result + sortIsArraySort (WithIndices _ xs) = do + axs <- TA.toArray xs + let ys = Array.sort axs + TA.sort xs + sxs <- TA.toArray xs + pure $ sxs === ys toStringIsJoinWithCommaTests :: Ref Int -> Effect Unit -toStringIsJoinWithCommaTests count = overAll count toStringIsJoinWithComma +toStringIsJoinWithCommaTests count = overAll 0 count toStringIsJoinWithComma where - toStringIsJoinWithComma :: forall a b t. TestableArrayF a b D0 t Result - toStringIsJoinWithComma (WithOffset _ xs) = - TA.toString' xs "," === TA.toString xs - + toStringIsJoinWithComma :: forall a t. TestableArrayF a t Result + toStringIsJoinWithComma (WithIndices _ xs) = do + s1 <- TA.join "," xs + s2 <- TA.toString xs + pure $ s1 === s2 setTypedOfSubArrayIsIdentityTests :: Ref Int -> Effect Unit -setTypedOfSubArrayIsIdentityTests count = overAll count setTypedOfSubArrayIsIdentity - where - setTypedOfSubArrayIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - setTypedOfSubArrayIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - zsSub = TA.subArray xs Nothing - zs = unsafePerformEffect do - _ <- TA.setTyped xs Nothing zsSub - pure (TA.toArray xs) - in zs === ys - +setTypedOfSubArrayIsIdentityTests count = overAll 0 count setTypedOfSubArrayIsIdentity + where + setTypedOfSubArrayIsIdentity :: forall a t. TestableArrayF a t Result + setTypedOfSubArrayIsIdentity (WithIndices _ xs) = do + ys <- TA.toArray xs + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + _ <- TA.setTyped xs Nothing zsSub + zs <- TA.toArray xs + pure $ zs === ys modifyingOriginalMutatesSubArrayTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayTests count = overAll count modifyingOriginalMutatesSubArray - where - modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs Nothing - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys - +modifyingOriginalMutatesSubArrayTests count = overAll 0 count modifyingOriginalMutatesSubArray + where + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalTests count = overAll count modifyingOriginalMutatesSubArray - where - modifyingOriginalMutatesSubArray :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArray (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs Nothing - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys - +modifyingSubArrayMutatesOriginalTests count = overAll 0 count modifyingOriginalMutatesSubArray + where + modifyingOriginalMutatesSubArray :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArray (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayZeroTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayZeroTests count = overAll count modifyingOriginalMutatesSubArrayZero - where - modifyingOriginalMutatesSubArrayZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayZero (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys - +modifyingOriginalMutatesSubArrayZeroTests count = overAll 0 count modifyingOriginalMutatesSubArrayZero + where + modifyingOriginalMutatesSubArrayZero :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayZero (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalZeroTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalZeroTests count = overAll count modifyingSubArrayMutatesOriginalZero - where - modifyingSubArrayMutatesOriginalZero :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalZero (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs (Just (Tuple 0 Nothing)) - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys - +modifyingSubArrayMutatesOriginalZeroTests count = overAll 0 count modifyingSubArrayMutatesOriginalZero + where + modifyingSubArrayMutatesOriginalZero :: forall a t. TestableArrayF a t Result + modifyingSubArrayMutatesOriginalZero (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys modifyingOriginalMutatesSubArrayAllTests :: Ref Int -> Effect Unit -modifyingOriginalMutatesSubArrayAllTests count = overAll count modifyingOriginalMutatesSubArrayAll - where - modifyingOriginalMutatesSubArrayAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalMutatesSubArrayAll (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs /== ys - +modifyingOriginalMutatesSubArrayAllTests count = overAll 0 count modifyingOriginalMutatesSubArrayAll + where + modifyingOriginalMutatesSubArrayAll :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayAll (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingSubArrayMutatesOriginalAllTests :: Ref Int -> Effect Unit -modifyingSubArrayMutatesOriginalAllTests count = overAll count modifyingSubArrayMutatesOriginalAll - where - modifyingSubArrayMutatesOriginalAll :: forall a b t. TestableArrayF a b D0 t Result - modifyingSubArrayMutatesOriginalAll (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.subArray xs (Just (Tuple 0 (Just (TA.length xs)))) - zs = TA.toArray xs - ys = unsafePerformEffect do - TA.fill zsSub zero Nothing - pure (TA.toArray xs) - in zs /== ys - - -modifyingOriginalDoesntMutateSubArrayPartTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSubArrayPartTests count = overAll count modifyingOriginalMutatesSubArrayPart - where - modifyingOriginalMutatesSubArrayPart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalMutatesSubArrayPart (WithOffset os xs) - | Vec.head os == 0 = Success - | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.subArray xs (Just (Tuple o Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - - -modifyingOriginalDoesntMutateSubArrayPart2Tests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSubArrayPart2Tests count = overAll count modifyingOriginalMutatesSubArrayPart2 - where - modifyingOriginalMutatesSubArrayPart2 :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalMutatesSubArrayPart2 (WithOffset os xs) - | Vec.head os == 0 = Success - | Array.all (eq zero) (TA.toArray (TA.subArray xs Nothing)) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.subArray xs (Just (Tuple 0 (Just o))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - +modifyingSubArrayMutatesOriginalAllTests count = overAll 0 count modifyingSubArrayMutatesOriginalAll + where + modifyingSubArrayMutatesOriginalAll :: forall a t. TestableArrayF a t Result + modifyingSubArrayMutatesOriginalAll (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray xs + TA.fill zero 0 l zsSub + ys <- TA.toArray xs + pure $ zs /== ys + +modifyingOriginalMutatesSubArrayPartTests :: Ref Int -> Effect Unit +modifyingOriginalMutatesSubArrayPartTests count = overAll 1 count modifyingOriginalMutatesSubArrayPart + where + modifyingOriginalMutatesSubArrayPart :: forall a t. TestableArrayF a t Result + modifyingOriginalMutatesSubArrayPart (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + zsSub = TA.subArray 0 l xs + zs <- TA.toArray zsSub + if o == 0 || Array.all (eq zero) zs then pure Success + else do + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs /== ys modifyingOriginalDoesntMutateSliceTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSliceTests count = overAll count modifyingOriginalDoesntMutateSlice - where - modifyingOriginalDoesntMutateSlice :: forall a b t. TestableArrayF a b D0 t Result - modifyingOriginalDoesntMutateSlice (WithOffset _ xs) - | Array.all (eq zero) (TA.toArray xs) = Success - | otherwise = - let zsSub = TA.slice xs Nothing - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - +modifyingOriginalDoesntMutateSliceTests count = overAll 0 count modifyingOriginalDoesntMutateSlice + where + modifyingOriginalDoesntMutateSlice :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlice (WithIndices _ xs) = do + axs <- TA.toArray xs + if Array.all (eq zero) axs then pure Success + else do + let l = TA.length xs + zsSub <- TA.slice 0 l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePartTests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSlicePartTests count = overAll count modifyingOriginalDoesntMutateSlicePart - where - modifyingOriginalDoesntMutateSlicePart :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart (WithOffset os xs) - | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.slice xs (Just (Tuple o Nothing)) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - +modifyingOriginalDoesntMutateSlicePartTests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart + where + modifyingOriginalDoesntMutateSlicePart :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlicePart (WithIndices os xs) = do + let l = TA.length xs + axs <- TA.toArray =<< TA.slice 0 l xs + let o = unsafePartial $ Array.head os + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success + else do + zsSub <- TA.slice o l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys modifyingOriginalDoesntMutateSlicePart2Tests :: Ref Int -> Effect Unit -modifyingOriginalDoesntMutateSlicePart2Tests count = overAll count modifyingOriginalDoesntMutateSlicePart2 - where - modifyingOriginalDoesntMutateSlicePart2 :: forall a b t. TestableArrayF a b D1 t Result - modifyingOriginalDoesntMutateSlicePart2 (WithOffset os xs) - | Array.all (eq zero) (TA.toArray (TA.slice xs (Just (Tuple (Vec.head os) Nothing)))) = Success - | TA.at xs (Vec.head os) == Just zero = Success - | otherwise = - let o = Vec.head os - zsSub = TA.slice xs (Just (Tuple 0 (Just o))) - zs = TA.toArray zsSub - ys = unsafePerformEffect do - TA.fill xs zero Nothing - pure (TA.toArray zsSub) - in zs === ys - +modifyingOriginalDoesntMutateSlicePart2Tests count = overAll 1 count modifyingOriginalDoesntMutateSlicePart2 + where + modifyingOriginalDoesntMutateSlicePart2 :: forall a t. TestableArrayF a t Result + modifyingOriginalDoesntMutateSlicePart2 (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + axs <- TA.toArray =<< TA.slice o l xs + e <- TA.at xs o + if Array.all (eq zero) axs || e == Just zero then pure Success + else do + zsSub <- TA.slice o l xs + zs <- TA.toArray zsSub + TA.fill zero 0 l xs + ys <- TA.toArray zsSub + pure $ zs === ys copyWithinSelfIsIdentityTests :: Ref Int -> Effect Unit -copyWithinSelfIsIdentityTests count = overAll count copyWithinSelfIsIdentity +copyWithinSelfIsIdentityTests count = overAll 0 count copyWithinSelfIsIdentity where - copyWithinSelfIsIdentity :: forall a b t. TestableArrayF a b D0 t Result - copyWithinSelfIsIdentity (WithOffset _ xs) = - let ys = TA.toArray xs - zs = unsafePerformEffect do - TA.copyWithin xs 0 0 (Just (TA.length xs)) - pure (TA.toArray xs) - in zs === ys - + copyWithinSelfIsIdentity :: forall a t. TestableArrayF a t Result + copyWithinSelfIsIdentity (WithIndices _ xs) = do + ys <- TA.toArray xs + TA.copyWithin xs 0 0 (Just (TA.length xs)) + zs <- TA.toArray xs + pure $ zs === ys copyWithinIsSliceTests :: Ref Int -> Effect Unit -copyWithinIsSliceTests count = overAll count copyWithinIsSlice - where - copyWithinIsSlice :: forall a b t. TestableArrayF a b D1 t Result - copyWithinIsSlice (WithOffset os xs) = - let o = Vec.head os - ys = TA.toArray (TA.slice xs (Just (Tuple o Nothing))) - zs = unsafePerformEffect do - TA.copyWithin xs 0 o Nothing - pure $ Array.drop (Array.length ys) $ TA.toArray xs - in TA.toArray xs === ys <> zs - +copyWithinIsSliceTests count = overAll 1 count copyWithinIsSlice + where + copyWithinIsSlice :: forall a t. TestableArrayF a t Result + copyWithinIsSlice (WithIndices os xs) = do + let + o = unsafePartial $ Array.head os + l = TA.length xs + ys <- TA.toArray =<< TA.slice o l xs + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + zs <- pure $ Array.drop (Array.length ys) axs + pure $ axs === ys <> zs copyWithinViaSetTypedTests :: Ref Int -> Effect Unit -copyWithinViaSetTypedTests count = overAll count copyWithinViaSetTyped - where - copyWithinViaSetTyped :: forall a b t. TestableArrayF a b D1 t Result - copyWithinViaSetTyped (WithOffset os xs) = - let o = Vec.head os - xs' = TA.fromArray (TA.toArray xs) :: ArrayView a - _ = unsafePerformEffect do - let ys = TA.slice xs' (Just (Tuple o Nothing)) - _ <- TA.setTyped xs' Nothing ys - TA.copyWithin xs 0 o Nothing - in TA.toArray xs === TA.toArray xs' +copyWithinViaSetTypedTests count = overAll 1 count copyWithinViaSetTyped + where + copyWithinViaSetTyped :: forall a t. TestableArrayF a t Result + copyWithinViaSetTyped (WithIndices os xs) = do + let o = unsafePartial $ Array.head os + txs <- TA.toArray xs + xs' <- TA.fromArray txs :: Effect (ArrayView a) + let l = TA.length xs' + ys <- TA.slice o l xs' + _ <- TA.setTyped xs' Nothing ys + TA.copyWithin xs 0 o Nothing + axs <- TA.toArray xs + axs' <- TA.toArray xs' + pure $ axs === axs' + +-- | Uses the second failure message as the result failure message +-- | https://github.com/athanclark/purescript-quickcheck-combinators/blob/293e5af07ae47b61d4eae5defef4c0f472bfa9ca/src/Test/QuickCheck/Combinators.purs#L62 +implies :: Result -> Result -> Result +implies x y = case y of + Failed y' -> case x of + Success -> Failed ("Implied failure: " <> y') + _ -> Success + _ -> Success + +-- | Combine two results with "Exclusive Or" logic, and with `", xor "` as the failure message separator, and "XOR" as the failure message if they are both `Success` +-- | https://github.com/athanclark/purescript-quickcheck-combinators/blob/293e5af07ae47b61d4eae5defef4c0f472bfa9ca/src/Test/QuickCheck/Combinators.purs#L44 +xor :: Result -> Result -> Result +xor = xor' ", xor " "XOR" + where + -- Combine two results with "Exclusive Or" logic, and with a failure message separator and failure message if they are both `Success` + xor' + :: String -- ^ Separator + -> String -- ^ Success failure message + -> Result + -> Result + -> Result + xor' m s x y = case Tuple x y of + Tuple (Failed x') (Failed y') -> Failed (x' <> m <> y') + Tuple Success Success -> Failed s + Tuple Success y' -> y' + Tuple x' Success -> x'