Skip to content

Commit c2149a5

Browse files
authored
feat: Create immutable.res (#1)
Starting to work on this concept: rescript-lang/rescript-core#23
1 parent af1292e commit c2149a5

9 files changed

+412
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib

bsconfig.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "rescript-immutable-data",
3+
"sources": [
4+
{
5+
"dir": "src",
6+
"subdirs": true
7+
},
8+
{
9+
"dir": "examples",
10+
"subdirs": true,
11+
"mode": "dev"
12+
}
13+
],
14+
"package-specs": [
15+
{
16+
"module": "commonjs",
17+
"in-source": true
18+
}
19+
],
20+
"suffix": ".bs.js",
21+
"bs-dependencies": [
22+
"@rescript/core"
23+
],
24+
"bsc-flags": ["-open RescriptCore"]
25+
}

examples/one.bs.js

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/one.res

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let t1 = [1]
2+
3+
let t2 = t1->Immutable.Array.fromArray->Immutable.Array.head
4+
// let t3 = t1->Immutable.Array.head
5+
let t4 = t1->Immutable.Array.fromArray->Immutable.Array.tail
6+
7+
let t5 = Dict.make()
8+
let t6 = t5->Immutable.Dict.make->Immutable.Dict.set("t", 1)
9+
10+
let t10 = Dict.fromArray([("foo", "bar")])->Immutable.Dict.make
11+
12+
// let t11 = t1->Immutable.Array.make->Array.map(n => n + 1)
13+
14+
let t11 = t1->Immutable.Array.fromArray
15+
let t12 = t11->Immutable.Array.at(1)

package-lock.json

+100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@jvlk/rescript-immutable-data",
3+
"version": "1.0.0-alpha.1",
4+
"description": "Aplha attempt at immmutable data for ReScript Core",
5+
"scripts": {
6+
"build": "rescript",
7+
"format": "prettier . --write",
8+
"res:build": "rescript build -with-deps",
9+
"res:dev": "rescript build -w -with-deps"
10+
},
11+
"keywords": [],
12+
"author": "Josh Derocher",
13+
"license": "MIT",
14+
"devDependencies": {
15+
},
16+
"dependencies": {
17+
"@rescript/core": "^0.3.1",
18+
"@rescript/react": "^0.11.0",
19+
"rescript": "^10.1.4"
20+
}
21+
}

src/immutable.bs.js

+128
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/immutable.res

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Array = {
2+
// new to Immutable
3+
type t<'a> = array<'a>
4+
let fromArray = arr => arr->Array.copy
5+
let toArray = fromArray
6+
7+
// different from array
8+
let set = (t, i, v) => {
9+
let c = t->Array.copy
10+
c->Array.set(i, v)
11+
c
12+
}
13+
14+
// same as Array
15+
16+
let at = Array.at
17+
18+
let concatMany = Array.concatMany
19+
let concat = (t, a) => [a]->Array.concat(t)
20+
let copy = Array.copy
21+
let copyAlllWithin = Array.copyAllWithin
22+
let copyWithin = Array.copyWithin
23+
let copyWithinToEnd = Array.copyWithinToEnd
24+
25+
let get = Array.get
26+
let map = Array.map
27+
28+
// not sure we want to add these
29+
let append = (t, a) => t->Array.concat([a])
30+
let isEmpty = t => t->Array.length == 0
31+
let head = t => t->Array.get(0)
32+
let tail = t => isEmpty(t) ? [] : t->Array.slice(~start=1, ~end=t->Array.length)
33+
}
34+
35+
36+
// Dict seems like another good candidate for being Immutable
37+
module Dict = {
38+
type t<'a> = Dict.t<'a>
39+
let make = Dict.copy
40+
let set = (o, k, v) => {
41+
let c = o->Dict.copy
42+
c->Dict.set(k, v)
43+
c
44+
}
45+
let get = Dict.get
46+
let fromArray = Dict.fromArray
47+
let fromIterator = Dict.fromIterator
48+
let toArray = Dict.toArray
49+
}

0 commit comments

Comments
 (0)