Skip to content
Draft
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
1 change: 1 addition & 0 deletions changes/belt-inline-helpers.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Mark small Belt functions `@inline`, so calls to them compile to the code they run: the `Belt.Option` and `Belt.Result` combinators such as `map` and `getWithDefault`, `Belt.Result.eq` and `cmp`, `Belt.Array.get` and `set`, `Belt.List.head`, `tail` and `add`, and the `size`, `isEmpty`, `peek` and `top` accessors of the hash maps and sets, `MutableQueue` and `MutableStack`.
2 changes: 2 additions & 0 deletions packages/@rescript/belt/src/Belt_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ external getUnsafe: (t<'a>, int) => 'a = "%array_unsafe_get"
external setUnsafe: (t<'a>, int, 'a) => unit = "%array_unsafe_set"

/* external get: 'a t -> int -> 'a = "%array_safe_get" */
@inline
let get = (arr, i) =>
if i >= 0 && i < length(arr) {
Some(getUnsafe(arr, i))
Expand All @@ -30,6 +31,7 @@ let getOrThrow = (arr, i) => {

let getExn = getOrThrow

@inline
let set = (arr, i, v) =>
if i >= 0 && i < length(arr) {
setUnsafe(arr, i, v)
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashMap.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type id<'a, 'id> = Belt_Id.hashable<'a, 'id>
type t<'a, 'b, 'id> = N.t<hash<'a, 'id>, eq<'a, 'id>, 'a, 'b>

let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashMapInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ let has = (h, key) => {

let make = (~hintSize) => C.make(~hintSize, ~hash=(), ~eq=())
let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashMapString.res
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ let has = (h, key) => {

let make = (~hintSize) => C.make(~hintSize, ~hash=(), ~eq=())
let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashSet.res
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ let make = (type value identity, ~hintSize, ~id: id<value, identity>) => {
}

let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashSetInt.res
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ let has = (h, key) => {
let make = (~hintSize) => C.make(~hintSize, ~hash=(), ~eq=())

let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_HashSetString.res
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ let has = (h, key) => {
let make = (~hintSize) => C.make(~hintSize, ~hash=(), ~eq=())

let clear = C.clear

@inline
let size = h => h.C.size

let forEach = N.forEach
let reduce = N.reduce
let logStats = N.logStats
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_List.res
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ external mutableCell: ('a, t<'a>) => t<'a> = "%makemutablelist"
- the cell is not empty
*/

@inline
let head = x =>
switch x {
| list{} => None
Expand All @@ -76,6 +77,7 @@ let headOrThrow = x =>

let headExn = headOrThrow

@inline
let tail = x =>
switch x {
| list{} => None
Expand All @@ -90,6 +92,7 @@ let tailOrThrow = x =>

let tailExn = tailOrThrow

@inline
let add = (xs, x) => list{x, ...xs}

/* Assume `n >=0` */
Expand Down
3 changes: 3 additions & 0 deletions packages/@rescript/belt/src/Belt_MutableQueue.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let add = (q, x) => {
}
}

@inline
let peek = q =>
switch q.first {
/* same here could be v */
Expand Down Expand Up @@ -130,8 +131,10 @@ let rec copyMapAux = (qRes, prev, cell, f) =>

let map = (q, f) => copyMapAux({length: q.length, first: None, last: None}, None, q.first, f)

@inline
let isEmpty = q => q.length == 0

@inline
let size = q => q.length

let rec iterAux = (cell, f) =>
Expand Down
2 changes: 2 additions & 0 deletions packages/@rescript/belt/src/Belt_MutableStack.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ let copy = (s: t<_>): t<_> => {root: s.root}

let push = (s, x) => s.root = Some({head: x, tail: s.root})

@inline
let top = s =>
switch s.root {
| None => None
| Some(x) => Some(x.head)
}

@inline
let isEmpty = s => s.root == None

let pop = s =>
Expand Down
9 changes: 9 additions & 0 deletions packages/@rescript/belt/src/Belt_Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* SPDX-License-Identifier: MIT
*/

@inline
let keep = (opt, p) =>
switch opt {
| Some(x) as some if p(x) => some
| _ => None
}

@inline
let forEach = (opt, f) =>
switch opt {
| Some(x) => f(x)
Expand All @@ -26,42 +28,49 @@ let getExn = getOrThrow

external getUnsafe: option<'a> => 'a = "%identity"

@inline
let mapWithDefault = (opt, default, f) =>
switch opt {
| Some(x) => f(x)
| None => default
}

@inline
let map = (opt, f) =>
switch opt {
| Some(x) => Some(f(x))
| None => None
}

@inline
let flatMap = (opt, f) =>
switch opt {
| Some(x) => f(x)
| None => None
}

@inline
let getWithDefault = (opt, default) =>
switch opt {
| Some(x) => x
| None => default
}

@inline
let orElse = (opt, other) =>
switch opt {
| Some(_) as some => some
| None => other
}

@inline
let isSome = x =>
switch x {
| Some(_) => true
| None => false
}

@inline
let isNone = x => x == None

let eq = (a, b, f) =>
Expand Down
8 changes: 8 additions & 0 deletions packages/@rescript/belt/src/Belt_Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,49 @@ let getOrThrow = x =>

let getExn = getOrThrow

@inline
let mapWithDefault = (opt, default, f) =>
switch opt {
| Ok(x) => f(x)
| Error(_) => default
}

@inline
let map = (opt, f) =>
switch opt {
| Ok(x) => Ok(f(x))
| Error(y) => Error(y)
}

@inline
let flatMap = (opt, f) =>
switch opt {
| Ok(x) => f(x)
| Error(y) => Error(y)
}

@inline
let getWithDefault = (opt, default) =>
switch opt {
| Ok(x) => x
| Error(_) => default
}

@inline
let isOk = x =>
switch x {
| Ok(_) => true
| Error(_) => false
}

@inline
let isError = x =>
switch x {
| Ok(_) => false
| Error(_) => true
}

@inline
let eq = (a, b, f) =>
switch (a, b) {
| (Ok(a), Ok(b)) => f(a, b)
Expand All @@ -60,6 +67,7 @@ let eq = (a, b, f) =>
| (Error(_), Error(_)) => true
}

@inline
let cmp = (a, b, f) =>
switch (a, b) {
| (Ok(a), Ok(b)) => f(a, b)
Expand Down
19 changes: 12 additions & 7 deletions tests/build_tests/testrepo/packages/dep02/src/Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ function includes(prim0, prim1) {
}

function head(t) {
return Belt_Array.get(t, 0);
if (0 < t.length) {
return Primitive_option.some(t[0]);
}
}

function take(t, n) {
return Belt_Array.slice(t, 0, n);
}

function last(t) {
return Belt_Array.get(t, t.length - 1 | 0);
let i = t.length - 1 | 0;
if (i >= 0 && i < t.length) {
return Primitive_option.some(t[i]);
}
}

function isEmpty(t) {
Expand Down Expand Up @@ -90,8 +95,8 @@ function eqBy(_xs, _ys, fn) {
while (true) {
let ys = _ys;
let xs = _xs;
let match = Belt_Array.get(xs, 0);
let match$1 = Belt_Array.get(ys, 0);
let match = 0 < xs.length ? Primitive_option.some(xs[0]) : undefined;
let match$1 = 0 < ys.length ? Primitive_option.some(ys[0]) : undefined;
if (match === undefined) {
return match$1 === undefined;
}
Expand Down Expand Up @@ -189,7 +194,7 @@ function indexOfBy(t, fn, value) {
let _idx = 0;
while (true) {
let idx = _idx;
let value$p = Belt_Array.get(t, idx);
let value$p = idx >= 0 && idx < t.length ? Primitive_option.some(t[idx]) : undefined;
if (value$p === undefined) {
return;
}
Expand All @@ -202,8 +207,8 @@ function indexOfBy(t, fn, value) {
}

function swapAt(t, i, j) {
let match = Belt_Array.get(t, i);
let match$1 = Belt_Array.get(t, j);
let match = i >= 0 && i < t.length ? Primitive_option.some(t[i]) : undefined;
let match$1 = j >= 0 && j < t.length ? Primitive_option.some(t[j]) : undefined;
if (match === undefined) {
return t;
}
Expand Down

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

Loading
Loading