Skip to content

feat: Create immutable.res #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
25 changes: 25 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "rescript-immutable-data",
"sources": [
{
"dir": "src",
"subdirs": true
},
{
"dir": "examples",
"subdirs": true,
"mode": "dev"
}
],
"package-specs": [
{
"module": "commonjs",
"in-source": true
}
],
"suffix": ".bs.js",
"bs-dependencies": [
"@rescript/core"
],
"bsc-flags": ["-open RescriptCore"]
}
33 changes: 33 additions & 0 deletions examples/one.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/one.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let t1 = [1]

let t2 = t1->Immutable.Array.fromArray->Immutable.Array.head
// let t3 = t1->Immutable.Array.head
let t4 = t1->Immutable.Array.fromArray->Immutable.Array.tail

let t5 = Dict.make()
let t6 = t5->Immutable.Dict.make->Immutable.Dict.set("t", 1)

let t10 = Dict.fromArray([("foo", "bar")])->Immutable.Dict.make

// let t11 = t1->Immutable.Array.make->Array.map(n => n + 1)

let t11 = t1->Immutable.Array.fromArray
let t12 = t11->Immutable.Array.at(1)
100 changes: 100 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@jvlk/rescript-immutable-data",
"version": "1.0.0-alpha.1",
"description": "Aplha attempt at immmutable data for ReScript Core",
"scripts": {
"build": "rescript",
"format": "prettier . --write",
"res:build": "rescript build -with-deps",
"res:dev": "rescript build -w -with-deps"
},
"keywords": [],
"author": "Josh Derocher",
"license": "MIT",
"devDependencies": {
},
"dependencies": {
"@rescript/core": "^0.3.1",
"@rescript/react": "^0.11.0",
"rescript": "^10.1.4"
}
}
128 changes: 128 additions & 0 deletions src/immutable.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/immutable.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Array = {
// new to Immutable
type t<'a> = array<'a>
let fromArray = arr => arr->Array.copy
let toArray = fromArray

// different from array
let set = (t, i, v) => {
let c = t->Array.copy
c->Array.set(i, v)
c
}

// same as Array

let at = Array.at

let concatMany = Array.concatMany
let concat = (t, a) => [a]->Array.concat(t)
let copy = Array.copy
let copyAlllWithin = Array.copyAllWithin
let copyWithin = Array.copyWithin
let copyWithinToEnd = Array.copyWithinToEnd

let get = Array.get
let map = Array.map

// not sure we want to add these
let append = (t, a) => t->Array.concat([a])
let isEmpty = t => t->Array.length == 0
let head = t => t->Array.get(0)
let tail = t => isEmpty(t) ? [] : t->Array.slice(~start=1, ~end=t->Array.length)
}


// Dict seems like another good candidate for being Immutable
module Dict = {
type t<'a> = Dict.t<'a>
let make = Dict.copy
let set = (o, k, v) => {
let c = o->Dict.copy
c->Dict.set(k, v)
c
}
let get = Dict.get
let fromArray = Dict.fromArray
let fromIterator = Dict.fromIterator
let toArray = Dict.toArray
}
Loading