git.iko.soy/rowtype-yoga/purescript-unique
on master help sign in
25 files changed+459−110
since splitsnapshots
unifiedsplit
.editorconfig+13 .envrc+9 .eslintrc.json−28 .github/ISSUE_TEMPLATE/bug-report.md+23 .github/ISSUE_TEMPLATE/change-request.md+24 .github/ISSUE_TEMPLATE/config.yml+8 .github/PULL_REQUEST_TEMPLATE.md+12 .github/workflows/ci.yml+57 .gitignore+9−7 .tidyrc.json+10 .travis.yml−18 CHANGELOG.md+80 CONTRIBUTING.md+50 LICENSE+1 README.md+39−4 bower.json+5−4 docs/README.md+3 packages.dhall+5 purs.json+6−5 shell.nix+28 spago.dhall+5 src/Data/Unique.js+23−21 src/Data/Unique.purs+34−16 test.dhall+6 test/Main.purs+9−7
.editorconfignew file+13
@@ -0,0 +1,13 @@
1+# https://editorconfig.org
2+root = true
3+
4+[*]
5+indent_style = space
6+indent_size = 2
7+end_of_line = lf
8+charset = utf-8
9+trim_trailing_whitespace = true
10+insert_final_newline = true
11+
12+[*.md]
13+trim_trailing_whitespace = false
.envrcnew file+9
@@ -0,0 +1,9 @@
1+#!/usr/bin/env bash
2+
3+if command -v lorri;
4+then
5+ eval "$(lorri direnv)"
6+elif command -v nix-shell
7+then
8+ use nix
9+fi
.eslintrc.jsondeleted−28
@@ -1,28 +0,0 @@
1−{
2− "parserOptions": {
3− "ecmaVersion": 5
4− },
5− "extends": "eslint:recommended",
6− "env": {
7− "commonjs": true
8− },
9− "rules": {
10− "strict": [2, "global"],
11− "block-scoped-var": 2,
12− "consistent-return": 2,
13− "eqeqeq": [2, "smart"],
14− "guard-for-in": 2,
15− "no-caller": 2,
16− "no-extend-native": 2,
17− "no-loop-func": 2,
18− "no-new": 2,
19− "no-param-reassign": 2,
20− "no-return-assign": 2,
21− "no-unused-expressions": 2,
22− "no-use-before-define": 2,
23− "radix": [2, "always"],
24− "indent": [2, 2, { "SwitchCase": 1 }],
25− "quotes": [2, "double"],
26− "semi": [2, "always"]
27− }
28−}
.github/ISSUE_TEMPLATE/bug-report.mdnew file+23
@@ -0,0 +1,23 @@
1+---
2+name: Bug report
3+about: Report an issue
4+title: ""
5+labels: bug
6+assignees: ""
7+---
8+
9+**Describe the bug**
10+
11+A clear and concise description of the bug.
12+
13+**To Reproduce**
14+
15+A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue.
16+
17+**Expected behavior**
18+
19+A clear and concise description of what you expected to happen.
20+
21+**Additional context**
22+
23+Add any other context about the problem here.
.github/ISSUE_TEMPLATE/change-request.mdnew file+24
@@ -0,0 +1,24 @@
1+---
2+name: Change request
3+about: Propose an improvement to this library
4+title: ""
5+labels: ""
6+assignees: ""
7+---
8+
9+**Is your change request related to a problem? Please describe.**
10+
11+A clear and concise description of the problem.
12+
13+Examples:
14+
15+- It's frustrating to have to [...]
16+- I was looking for a function to [...]
17+
18+**Describe the solution you'd like**
19+
20+A clear and concise description of what a good solution to you looks like, including any solutions you've already considered.
21+
22+**Additional context**
23+
24+Add any other context about the change request here.
.github/ISSUE_TEMPLATE/config.ymlnew file+8
@@ -0,0 +1,8 @@
1+blank_issues_enabled: false
2+contact_links:
3+ - name: PureScript Discourse
4+ url: https://discourse.purescript.org/
5+ about: Ask and answer questions on the PureScript discussion forum.
6+ - name: PureScript Discord
7+ url: https://purescript.org/chat
8+ about: Ask and answer questions on the PureScript chat.
.github/PULL_REQUEST_TEMPLATE.mdnew file+12
@@ -0,0 +1,12 @@
1+**Description of the change**
2+
3+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.
4+
5+---
6+
7+**Checklist:**
8+
9+- [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username
10+- [ ] Linked any existing issues or proposals that this pull request should close
11+- [ ] Updated or added relevant documentation in the README and/or documentation directory
12+- [ ] Added a test for the contribution (if applicable)
.github/workflows/ci.ymlnew file+57
@@ -0,0 +1,57 @@
1+name: CI
2+
3+on:
4+ push:
5+ branches: [master]
6+ paths:
7+ - 'src/**/*.purs'
8+ - 'src/**/*.js'
9+ - 'test/**/*.purs'
10+ - 'test/**/*.js'
11+ - '*.dhall'
12+ - '*.json'
13+ - '.github/workflows/ci.yml'
14+ pull_request:
15+ branches: [master]
16+ paths:
17+ - 'src/**/*.purs'
18+ - 'src/**/*.js'
19+ - 'test/**/*.purs'
20+ - 'test/**/*.js'
21+ - '*.dhall'
22+ - '*.json'
23+ - '.github/workflows/ci.yml'
24+
25+jobs:
26+ build:
27+ runs-on: ubuntu-latest
28+
29+ steps:
30+ - uses: actions/checkout@v3
31+
32+ - uses: purescript-contrib/setup-purescript@main
33+ with:
34+ purescript: "unstable"
35+ purs-tidy: "latest"
36+
37+ - uses: actions/cache@v2
38+ with:
39+ key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
40+ path: |
41+ .spago
42+ output
43+
44+ - run: spago install
45+
46+ - run: spago build --no-install --purs-args '--censor-lib --strict'
47+
48+ - run: spago -x test.dhall test
49+
50+ - run: purs-tidy check src test
51+
52+ - name: Verify Bower & Pulp
53+ run: |
54+ npm install bower pulp@16.0.0-0
55+ npx bower install
56+ npx pulp build # -- --censor-lib --strict
57+ npx pulp test
.gitignore+9−7
@@ -1,7 +1,9 @@
1−/.*
2−!/.gitignore
3−!/.eslintrc.json
4−!/.travis.yml
5−/bower_components/
6−/node_modules/
7−/output/
1+.*
2+!.gitignore
3+!.github
4+!.editorconfig
5+!.tidyrc.json
6+
7+output
8+generated-docs
9+bower_components
.tidyrc.jsonnew file+10
@@ -0,0 +1,10 @@
1+{
2+ "importSort": "source",
3+ "importWrap": "source",
4+ "indent": 2,
5+ "operatorsFile": null,
6+ "ribbon": 1,
7+ "typeArrowPlacement": "first",
8+ "unicode": "never",
9+ "width": null
10+}
.travis.ymldeleted−18
@@ -1,18 +0,0 @@
1−language: node_js
2−dist: trusty
3−sudo: required
4−node_js: stable
5−env:
6− - PATH=$HOME/purescript:$PATH
7−install:
8− - TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9− - wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10− - tar -xvf $HOME/purescript.tar.gz -C $HOME/
11− - chmod a+x $HOME/purescript
12− - npm install -g bower
13− - npm install
14−script:
15− - bower install --production
16− - npm run -s build
17− - bower install
18− - npm -s test
CHANGELOG.mdnew file+80
@@ -0,0 +1,80 @@
1+# Changelog
2+
3+Notable changes 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).
4+
5+## [Unreleased]
6+
7+Breaking changes:
8+
9+New features:
10+
11+Bugfixes:
12+
13+Other improvements:
14+- Updated the project for its new home in the Rowtype Yoga organization. ([50e18df](https://github.com/rowtype-yoga/purescript-unique/commit/50e18dfe14082186abc103b3c969343cf998cad0) by @nsaunders)
15+
16+## [0.6.1] - 2023-03-18
17+
18+New features:
19+- Added `hashUnique` function. (#4 by @nsaunders)
20+
21+Other improvements:
22+- Changed `Unique`'s runtime representation to a [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). (#4 by @nsaunders)
23+- Updated the project to the standards of the [purescript-contrib](https://github.com/purescript-contrib) organization. ([8ec352b](https://github.com/rowtype-yoga/purescript-unique/commit/8ec352ba1f7076c57c776872c2bc8ce7eb530e44) by @nsaunders)
24+
25+## [0.6.0] - 2023-03-02
26+
27+Breaking changes:
28+- Updated to v0.15 of the compiler, dropping support for previous versions. ([7999c18](https://github.com/rowtype-yoga/purescript-unique/commit/7999c1815adeb9011124c7d54902b55f18c8f295) by @nsaunders)
29+
30+## [0.5.1] - 2023-03-02
31+
32+Other improvements:
33+- Added `purs.json` file to facilitate package name transfer. ([4c40891](https://github.com/rowtype-yoga/purescript-unique/commit/4c40891f563c3e5f212fe3b0cef4c0c28dd917b8) by @nsaunders)
34+- Added `LICENSE` file, determined from `bower.json`. ([2f3305c](https://github.com/rowtype-yoga/purescript-unique/commit/2f3305cb511e6c270c25d53e6ebaaf3c193646ae) by @nsaunders)
35+
36+## [0.5.0] - 2017-04-26
37+
38+Breaking changes:
39+- Updated to v0.11 of the compiler, dropping support for previous versions. ([33214fa](https://github.com/rowtype-yoga/purescript-unique/commit/33214faf821843aad95a019ad0a4a28d2b305189) by @mechairoi)
40+
41+## [0.4.0] - 2017-01-06
42+
43+Other improvements:
44+- Updated dependencies. ([03a24af](https://github.com/rowtype-yoga/purescript-unique/commit/03a24af23dd9a64e08ff8a9f8d119437d2d9cf44) by @mechairoi)
45+
46+## [0.3.0] - 2016-06-28
47+
48+Breaking changes:
49+- Updated to v0.9 of the compiler, dropping support for previous versions. ([3a58d82](https://github.com/rowtype-yoga/purescript-unique/commit/3a58d82f1d9b5942cc29f3a31979a10978797204) by @mechairoi)
50+
51+## [0.2.3] - 2016-05-04
52+
53+Breaking changes:
54+- Updated to v0.8 of the compiler, dropping support for previous versions. ([42fecfe](https://github.com/rowtype-yoga/purescript-unique/commit/42fecfef16b3f54e7bf52ba8e882202f5f73fe6b) by @mechairoi)
55+
56+## [0.2.2] - 2016-02-12
57+
58+Bugfixes:
59+- Fixed `bower.json`. ([5048080](https://github.com/rowtype-yoga/purescript-unique/commit/5048080a43a552e5a4786aff4bd4ae93816ade0f) by @mechairoi)
60+
61+Other improvements:
62+- Updated to use Pursuit for docs. ([de69519](https://github.com/rowtype-yoga/purescript-unique/commit/de695190c6bf43d827f0e63c0c99a6e080b262cc) by @mechairoi)
63+
64+## [0.2.1] - 2016-02-12
65+
66+No significant changes in this release.
67+
68+## [0.2.0] - 2016-02-12
69+
70+Breaking changes:
71+- Renamed `Uniq` effect to `UNIQ`. ([4eeac9f](https://github.com/rowtype-yoga/purescript-unique/commit/4eeac9f11f79b85bf8086d869ffa8fa56d9a9ec0) by @mechairoi)
72+
73+## [0.1.0] - 2015-08-09
74+
75+Breaking changes:
76+- Updated to v0.7 of the compiler, dropping support for previous versions. ([d2b2873](https://github.com/rowtype-yoga/purescript-unique/commit/d2b2873559a8c1bfe7dd3194a1614a77eac1a01e) by @mechairoi)
77+
78+## [0.0.1] - 2015-08-09
79+
80+Initial release
CONTRIBUTING.mdnew file+50
@@ -0,0 +1,50 @@
1+Contributing
2+============
3+
4+Welcome, and thanks for your interest in Unique. Whether you are new to PureScript or here to teach us new tricks, we're glad to have you and look forward to your contributions. Listed below are a few ways you can help.
5+
6+Asking questions
7+----------------
8+If you have a question, please [open an issue](https://github.com/rowtype-yoga/purescript-unique/issues/new?labels=question) to discuss it. This might help us to find a defect; reveal an opportunity to improve the documentation or developer experience; or help someone facing a similar issue in the future.
9+
10+Reporting bugs
11+--------------
12+Please [open an issue](https://github.com/rowtype-yoga/purescript-unique/issues/new?labels=bug&template=bug-report.md) to discuss any defects you find.
13+
14+Enhancements
15+------------
16+Please [open an issue](https://github.com/rowtype-yoga/purescript-unique/issues/new?labels=enhancement&template=change-request.md) to discuss potential enhancements to the library. When submitting design ideas, please note that, for the most part, this library is intended as a direct port of the Haskell [`Data.Unique`](https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Unique.html) module.
17+
18+Improving documentation
19+-----------------------
20+Please share any suggestions for improving or adding to the documentation by [submitting a pull request](https://github.com/rowtype-yoga/purescript-unique/compare) or [opening an issue](https://github.com/rowtype-yoga/purescript-unique/issues/new?labels=documentation).
21+
22+Sharing resources
23+-----------------
24+If you have created a tutorial, auxiliary library, interesting code example, or other resource related to Unique, please [share it with the PureScript community](https://discourse.purescript.org). Increasing awareness will bring more users (and potential contributors) to the project.
25+
26+Submitting code
27+---------------
28+Pull requests are welcome, but we ask that you [open an issue](https://github.com/rowtype-yoga/purescript-unique/issues/new) to discuss your plans before making any significant investment of your valuable time.
29+
30+### Development environment
31+
32+You can use [Nix](https://github.com/NixOS/nix) to create a development environment with all required tooling. Simply run `nix-shell` in the root project directory, and you'll have everything you need to get started.
33+
34+### Making changes
35+
36+* After making any changes, run `purs-tidy format-in-place src test` to format the code.
37+* Unit test(s) should accompany each change.
38+
39+### Preparing your submission
40+
41+Please verify your changes before submitting a pull request using the following commands:
42+1. `purs-tidy check src test`, which ensures that modules in the `src` and `test` directories conform to the project's formatting standards; and
43+1. `spago -x test.dhall test`, which runs the unit tests.
44+
45+### Submitting a pull request
46+
47+When you are ready to [submit a pull request](https://github.com/rowtype-yoga/purescript-unique/compare), please make sure to include:
48+1. a brief description of the change;
49+1. a link to the related issue, if applicable; and
50+1. any design considerations you feel are important.
LICENSE+1
@@ -1,6 +1,7 @@
11MIT License
22
33Copyright (c) 2015-2017 mechairoi
4+Copyright (c) The University of Glasgow (Documentation)
45
56Permission is hereby granted, free of charge, to any person obtaining a copy
67of this software and associated documentation files (the "Software"), to deal
README.md+39−4
@@ -1,8 +1,43 @@
1−# purescript-unique
1+# Unique
22
3−[![Build Status](https://travis-ci.org/mechairoi/purescript-unique.svg?branch=master)](https://travis-ci.org/mechairoi/purescript-unique)
3+[![CI](https://github.com/rowtype-yoga/purescript-unique/workflows/CI/badge.svg?branch=master)](https://github.com/rowtype-yoga/purescript-unique/actions?query=workflow%3ACI+branch%3Amaster)
4+[![Release](https://img.shields.io/github/release/rowtype-yoga/purescript-unique.svg)](https://github.com/rowtype-yoga/purescript-unique/releases)
5+[![Pursuit](https://pursuit.purescript.org/packages/purescript-unique/badge)](https://pursuit.purescript.org/packages/purescript-unique)
6+[![Maintainer: nsaunders](https://img.shields.io/badge/maintainer-nsaunders-teal.svg)](https://github.com/nsaunders)
47
58Data.Unique for PureScript.
69
7−## Documentations
8−- [purescript-unique on Pursuit](https://pursuit.purescript.org/packages/purescript-unique)
10+## Installation
11+
12+Install `unique` with [Spago](https://github.com/purescript/spago):
13+
14+```sh
15+spago install unique
16+```
17+
18+## Quick start
19+
20+The quick start hasn't been written yet (contributions are welcome!). The quick start covers a common, minimal use case for the library, whereas longer examples and tutorials are kept in the [docs directory](./docs).
21+
22+## Documentation
23+
24+`unique` documentation is stored in a few places:
25+
26+1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-unique).
27+2. Written documentation is kept in the [docs directory](./docs).
28+3. Usage examples can be found in [the test suite](./test).
29+
30+If you get stuck, there are several ways to get help:
31+
32+- [Open an issue](https://github.com/rowtype-yoga/purescript-unique/issues) if you have encountered a bug or problem.
33+- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://discord.com/invite/sMqwYUbvz6) chat.
34+
35+## Contributing
36+
37+You can contribute to `unique` in several ways:
38+
39+1. If you encounter a problem or have a question, please [open an issue](https://github.com/rowtype-yoga/purescript-unique/issues). We'll do our best to work with you to resolve or answer it.
40+
41+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.
42+
43+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.
bower.json+5−4
@@ -17,11 +17,12 @@
1717 "package.json"
1818 ],
1919 "dependencies": {
20− "purescript-prelude": "^3.0.0",
21− "purescript-eff": "^3.1.0"
20+ "purescript-prelude": "^6.0.0",
21+ "purescript-effect": "^4.0.0",
22+ "purescript-refs": "^6.0.0"
2223 },
2324 "devDependencies": {
24− "purescript-console": "^3.0.0",
25− "purescript-assert": "^3.0.0"
25+ "purescript-console": "^6.0.0",
26+ "purescript-assert": "^6.0.0"
2627 }
2728}
docs/README.mdnew file+3
@@ -0,0 +1,3 @@
1+# Unique Documentation
2+
3+This directory contains documentation for `unique`. 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.
packages.dhallnew file+5
@@ -0,0 +1,5 @@
1+let upstream =
2+ https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20230105/packages.dhall
3+ sha256:3e9fbc9ba03e9a1fcfd895f65e2d50ee2f5e86c4cd273f3d5c841b655a0e1bda
4+
5+in upstream
purs.json+6−5
@@ -4,18 +4,19 @@
44 {
55 "email": "nick@saunde.rs",
66 "keytype": "ssh-ed25519",
7− "public": "AAAAC3NzaC1lZDI1NTE5AAAAICm0Ar7rHAWONOM4qA5q777/CQaN6cHvcBq3VXB7d5Yw"
7+ "public": "AAAAC3NzaC1lZDI1NTE5AAAAIONVUgzC6fBZMCob5Ua0BShtF9QlriJV1IyZVvr8m8Gd"
88 }
99 ],
10− "version": "0.5.1",
10+ "version": "0.6.1",
1111 "license": "MIT",
1212 "location": {
13− "githubOwner": "mechairoi",
13+ "githubOwner": "nsaunders",
1414 "githubRepo": "purescript-unique"
1515 },
1616 "description": "Data.Unique for PureScript",
1717 "dependencies": {
18− "prelude": ">=3.0.0 <4.0.0",
19− "eff": ">=3.1.0 <4.0.0"
18+ "prelude": ">=6.0.0 <7.0.0",
19+ "effect": ">=4.0.0 <5.0.0",
20+ "refs": ">=6.0.0 <7.0.0"
2021 }
2122}
shell.nixnew file+28
@@ -0,0 +1,28 @@
1+let
2+ pkgs = import (builtins.fetchTarball {
3+ url = "https://github.com/NixOS/nixpkgs/archive/21.11.tar.gz";
4+ }) {};
5+
6+ # To update to a newer version of easy-purescript-nix:
7+ # 1. Obtain the commit hash <rev> via `curl https://api.github.com/repos/justinwoo/easy-purescript-nix/commits/master`.
8+ # 2. Obtain the sha256 hash <sha256> via `nix-prefetch-url --unpack https://github.com/justinwoo/easy-purescript-nix/archive/<rev>.tar.gz`.
9+ # 3. Update the <rev> and <sha256> below.
10+ pursPkgs = import (pkgs.fetchFromGitHub {
11+ owner = "justinwoo";
12+ repo = "easy-purescript-nix";
13+ rev = "ee51a6d459b8fecfcb10f24ca9728e649c6a9e00";
14+ sha256 = "1w4k6mcayyg6388na0cca5qx94sm99xn3na26x2w34jlyz3bwl3m";
15+ }) {inherit pkgs;};
16+in
17+ pkgs.stdenv.mkDerivation {
18+ name = "unique";
19+ buildInputs = with pursPkgs; [
20+ purs
21+ spago
22+ pulp
23+ purs-tidy
24+
25+ pkgs.nodejs-16_x
26+ pkgs.nodePackages.bower
27+ ];
28+ }
spago.dhallnew file+5
@@ -0,0 +1,5 @@
1+{ name = "unique"
2+, dependencies = [ "effect", "prelude", "refs" ]
3+, packages = ./packages.dhall
4+, sources = [ "src/**/*.purs" ]
5+}
src/Data/Unique.js+23−21
@@ -1,27 +1,29 @@
1−/* global exports */
2−"use strict";
1+export var zero = BigInt("0");
32
4−// module Data.Unique
3+var one = BigInt("1");
54
6−var MAX_SAFE_INTEGER = 9007199254740991;
7−var uniqueSource = [];
5+export function eqImpl(a) {
6+ return function(b) {
7+ return a === b;
8+ };
9+}
810
9−function iter(i) {
10− if (uniqueSource.length === i) {
11− for (var j = 0; j < i; ++j) uniqueSource[j] = 0;
12− uniqueSource[i] = 1;
13− } else {
14− if (uniqueSource[i] === MAX_SAFE_INTEGER) {
15− uniqueSource[i] = 0;
16− iter(i + 1);
17− } else {
18− uniqueSource[i]++;
19− }
20− }
11+export function ordImpl(lt) {
12+ return function (eq) {
13+ return function (gt) {
14+ return function (x) {
15+ return function (y) {
16+ return x < y ? lt : x === y ? eq : gt;
17+ };
18+ };
19+ };
20+ };
2121}
2222
23−exports.newUnique = function () {
24− iter(0);
25− return uniqueSource.length === 1 ? uniqueSource[0] + "" : uniqueSource.join(",");
26−};
23+export function addOne(x) {
24+ return x + one;
25+}
2726
27+export function hashUnique(x) {
28+ return parseInt(BigInt.asIntN(32, x));
29+}
src/Data/Unique.purs+34−16
@@ -1,23 +1,41 @@
1−module Data.Unique
2− ( newUnique
3− , Unique()
4− , UNIQ()
5− ) where
1+module Data.Unique (Unique, hashUnique, newUnique) where
62
7−import Control.Monad.Eff (Eff, kind Effect)
8−import Prelude (class Ord, class Eq, class Show, compare, (==))
3+import Prelude hiding (zero)
94
10−foreign import data UNIQ :: Effect
5+import Effect (Effect)
6+import Effect.Ref (Ref)
7+import Effect.Ref as Ref
8+import Effect.Unsafe (unsafePerformEffect)
119
12−newtype Unique = Unique String
10+-- | An abstract unique object. Objects of type `Unique` may be compared for
11+-- | equality and ordering and hashed into `Int`.
12+foreign import data Unique :: Type
1313
14−foreign import newUnique :: forall e. Eff (uniq :: UNIQ | e) Unique
14+foreign import eqImpl :: Unique -> Unique -> Boolean
1515
16−instance showUnique :: Show Unique where
17− show (Unique a) = a
16+instance Eq Unique where
17+ eq = eqImpl
1818
19−instance eqUnique :: Eq Unique where
20− eq (Unique a) (Unique b) = a == b
19+foreign import ordImpl :: Ordering -> Ordering -> Ordering -> Unique -> Unique -> Ordering
2120
22−instance ordUnique :: Ord Unique where
23− compare (Unique a) (Unique b) = compare a b
21+instance Ord Unique where
22+ compare = ordImpl LT EQ GT
23+
24+foreign import addOne :: Unique -> Unique
25+
26+foreign import zero :: Unique
27+
28+-- | Hashes a `Unique` into an `Int`. Two `Unique`s may hash to the same value,
29+-- | although in practice this is unlikely. The `Int` returned makes a good hash
30+-- | key.
31+foreign import hashUnique :: Unique -> Int
32+
33+uniqSource :: Ref Unique
34+uniqSource = unsafePerformEffect (Ref.new zero)
35+
36+-- | Creates a new object of type `Unique`. The value returned will not compare
37+-- | equal to any other value of type `Unique` returned by previous calls to
38+-- | `newUnique`. There is no limit on the number of times `newUnique` may be
39+-- | called.
40+newUnique :: Effect Unique
41+newUnique = Ref.modify addOne uniqSource
test.dhallnew file+6
@@ -0,0 +1,6 @@
1+let conf = ./spago.dhall
2+
3+in conf // {
4+ sources = conf.sources # ["test/**/*.purs"],
5+ dependencies = conf.dependencies # ["assert", "console"]
6+}
test/Main.purs+9−7
@@ -1,15 +1,17 @@
11module Test.Main where
22
3−import Prelude ((==), ($), (/=), discard, bind, Unit)
4−import Data.Unique (newUnique, UNIQ)
5−import Test.Assert (assert, ASSERT)
6−import Control.Monad.Eff (Eff)
7−import Control.Monad.Eff.Console (log, CONSOLE)
3+import Prelude
4+import Data.Unique (hashUnique, newUnique)
5+import Test.Assert (assert)
6+import Effect (Effect)
7+import Effect.Console (log)
88
9−main :: forall e. Eff (console :: CONSOLE , uniq :: UNIQ , assert :: ASSERT | e) Unit
9+main :: Effect Unit
1010main = do
1111 log "Test Data.Unique"
1212 a <- newUnique
1313 b <- newUnique
14− assert $ a /= b
1514 assert $ a == a
15+ assert $ a < b
16+ assert $ hashUnique a == hashUnique a
17+ assert $ hashUnique a /= hashUnique b