Skip to content

Instantly share code, notes, and snippets.

@tuchk4

tuchk4/test.re Secret

Last active July 6, 2021 20:42
Show Gist options
  • Save tuchk4/42a98693fd0d664eea04bd54ad95549e to your computer and use it in GitHub Desktop.
Save tuchk4/42a98693fd0d664eea04bd54ad95549e to your computer and use it in GitHub Desktop.
type part_item = {mutable symbols: string};
type data_item = {symbol: string, next: bool};
let process data => {
let parts: list part_item = [];
let current = {symbols: ""};
List.iter
(
fun p =>
if p.next {
/* ? push current to parts ? */
current.symbols = ""
} else {
current.symbols = current.symbols ^ p.symbol
}
)
data;
parts
};
let data: list data_item = [
{symbol: "a", next: false},
{symbol: "b", next: false},
{symbol: "c", next: true},
{symbol: "d", next: false}
];
let parts = process data;
@idkjs
Copy link

idkjs commented Jun 9, 2021

Modern ReasonML syntax

type data_item = {
  symbol: string,
  next: bool,
};

/* Convenience function for making data items. */
let make_data_item = (symbol, next) => {symbol, next};

let process = (data: list(data_item)) => {
  /*
   We track the current symbol as well as the result list in the folding function.
   */
  let fold_func = ((current, result), {symbol, next}) =>
    if (next) {
      (symbol, [current, ...result]);
    } else {
      (current ++ symbol, result);
    };

  let (current, result) = List.fold_left(fold_func, ("", []), data);
  /*
   We need to reverse the result list because `[el, ...els]` above actually
   builds it up in the _opposite_ order to the original input list.
   */
  (current, List.rev(result));
};

let result =
  process([
    make_data_item("a", false),
    make_data_item("b", false),
    make_data_item("c", true),
    make_data_item("d", false),
  ]);

Thanks, @yawaramin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment