Skip to content
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

Add Array.joinWith to Belt #4748

Merged
merged 2 commits into from
Oct 15, 2020
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
6 changes: 3 additions & 3 deletions jscomp/main/builtin_cmi_datasets.ml

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions jscomp/main/builtin_cmj_datasets.ml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions jscomp/others/belt_Array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,14 @@ let unzip a =
a2.!(i) <- v2
done;
(a1, a2)

let joinWithU a sep toString =
match length a with
| 0 -> ""
| l ->
let lastIndex = l - 1 in
let rec aux i res =
if i = lastIndex then res ^ toString a.!(i) [@bs]
else aux (i + 1) (res ^ toString a.!(i) [@bs] ^ sep) in
aux 0 ""
let joinWith a sep toString = joinWithU a sep (fun [@bs] x -> toString x)
34 changes: 26 additions & 8 deletions jscomp/others/belt_Array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ val map: 'a array -> ('a -> 'b ) -> 'b array
the beginning to end

@example {[
map [|1;2|] (fun x-> x + 1) = [|3;4|]
map [|1;2|] (fun x-> x + 10) = [|11;12|]
]}

*)
Expand Down Expand Up @@ -393,7 +393,7 @@ val getIndexBy: 'a array -> ('a -> bool) -> int option
val keepU: 'a array -> ('a -> bool [@bs]) -> 'a array
val keep: 'a array -> ('a -> bool ) -> 'a array
(** [keep xs p ]
@return a new array that keep all elements satisfy [p]
@return a new array that keeps all elements satisfying [p]

@example {[
keep [|1;2;3|] (fun x -> x mod 2 = 0) = [|2|]
Expand All @@ -403,7 +403,9 @@ val keep: 'a array -> ('a -> bool ) -> 'a array
val keepWithIndexU: 'a array -> ('a -> int -> bool [@bs]) -> 'a array
val keepWithIndex: 'a array -> ('a -> int -> bool ) -> 'a array
(** [keepWithIndex xs p ]
@return a new array that keep all elements satisfy [p]
@return a new array that keeps all elements satisfying [p].
The predicate [p] takes two arguments:
the element from [xs] and the index starting from 0.

@example {[
keepWithIndex [|1;2;3|] (fun _x i -> i = 1) = [|2|]
Expand All @@ -413,10 +415,10 @@ val keepWithIndex: 'a array -> ('a -> int -> bool ) -> 'a array
val keepMapU: 'a array -> ('a -> 'b option [@bs]) -> 'b array
val keepMap: 'a array -> ('a -> 'b option) -> 'b array
(** [keepMap xs p]
@return a new array that keep all elements that return a non-None applied [p]
@return a new array that keeps all elements that return a non-None when applied to [p]

@example {[
keepMap [|1;2;3|] (fun x -> if x mod 2 then Some x else None)
keepMap [|1;2;3|] (fun x -> if x mod 2 = 0 then Some x else None)
= [| 2 |]
]}
*)
Expand All @@ -425,16 +427,16 @@ val forEachWithIndexU: 'a array -> (int -> 'a -> unit [@bs]) -> unit
val forEachWithIndex: 'a array -> (int -> 'a -> unit ) -> unit
(** [forEachWithIndex xs f]

The same as {!forEach};except that [f] is supplied two arguments:
The same as {!forEach}; except that [f] is supplied with two arguments:
the index starting from 0 and the element from [xs]

@example {[

forEach [|"a";"b";"c"|] (fun i x -> Js.log("Item " ^ (string_of_int i) ^ " is " ^ x));;
forEachWithIndex [|"a";"b";"c"|] (fun i x -> Js.log("Item " ^ (string_of_int i) ^ " is " ^ x));;
(* prints:
Item 0 is a
Item 1 is b
Item 2 is cc
Item 2 is c
*)

let total = ref 0 ;;
Expand Down Expand Up @@ -522,6 +524,22 @@ val reduceWithIndex: 'a array -> 'b -> ('b -> 'a -> int -> 'b) -> 'b
]}
*)

val joinWithU: 'a array -> string -> ('a -> string [@bs]) -> string
val joinWith: 'a array -> string -> ('a -> string) -> string
(** [joinWith xs sep toString]

Concatenates all the elements of [xs] converted to string with [toString], each separated by [sep], the string
given as the second argument, into a single string.
If the array has only one element, then that element will be returned
without using the separator.
If the array is empty, the empty string will be returned.
@example{[
joinWith [|0; 1|] ", " string_of_int = "0, 1"
joinWith [||] " " string_of_int = ""
joinWith [|1|] " " string_of_int = "1"
]}
*)

val someU: 'a array -> ('a -> bool [@bs]) -> bool
val some: 'a array -> ('a -> bool) -> bool
(** [some xs p]
Expand Down
38 changes: 19 additions & 19 deletions lib/4.06.1/unstable/js_compiler.ml

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions lib/4.06.1/unstable/js_refmt_compiler.ml

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions lib/4.06.1/whole_compiler.ml

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions lib/es6/belt_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,30 @@ function unzip(a) {
];
}

function joinWithU(a, sep, toString) {
var l = a.length;
if (l === 0) {
return "";
}
var lastIndex = l - 1 | 0;
var _i = 0;
var _res = "";
while(true) {
var res = _res;
var i = _i;
if (i === lastIndex) {
return res + toString(a[i]);
}
_res = res + (toString(a[i]) + sep);
_i = i + 1 | 0;
continue ;
};
}

function joinWith(a, sep, toString) {
return joinWithU(a, sep, Curry.__1(toString));
}

export {
get ,
getExn ,
Expand Down Expand Up @@ -707,6 +731,8 @@ export {
reduceReverse2 ,
reduceWithIndexU ,
reduceWithIndex ,
joinWithU ,
joinWith ,
someU ,
some ,
everyU ,
Expand Down
26 changes: 26 additions & 0 deletions lib/js/belt_Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,30 @@ function unzip(a) {
];
}

function joinWithU(a, sep, toString) {
var l = a.length;
if (l === 0) {
return "";
}
var lastIndex = l - 1 | 0;
var _i = 0;
var _res = "";
while(true) {
var res = _res;
var i = _i;
if (i === lastIndex) {
return res + toString(a[i]);
}
_res = res + (toString(a[i]) + sep);
_i = i + 1 | 0;
continue ;
};
}

function joinWith(a, sep, toString) {
return joinWithU(a, sep, Curry.__1(toString));
}

exports.get = get;
exports.getExn = getExn;
exports.set = set;
Expand Down Expand Up @@ -706,6 +730,8 @@ exports.reduceReverse2U = reduceReverse2U;
exports.reduceReverse2 = reduceReverse2;
exports.reduceWithIndexU = reduceWithIndexU;
exports.reduceWithIndex = reduceWithIndex;
exports.joinWithU = joinWithU;
exports.joinWith = joinWith;
exports.someU = someU;
exports.some = some;
exports.everyU = everyU;
Expand Down