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

Assign matched expressions to variables in generated JavaScript ⚡ #114

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

jmackie
Copy link
Member

@jmackie jmackie commented Dec 29, 2022

Updates the JavaScript code generator to assign expressions being matched against. This avoids potentially expensive re-evaluations.

Given the following module:

module Example exports (..);


some = fn (a) -> a;

long = fn (a) -> a;

convoluted = fn (a) -> a;

function = fn (a) -> a;

type ABC =
    | A(Int)
    | B(Int)
    | C(Int);

example = fn (abc) ->
    match abc
    |> some
    |> long
    |> convoluted
    |> function with
    | A(i) -> i
    | B(i) -> i
    | C(i) -> i
    end;

Before this PR would generate:

function example(abc) {
  if ($function(convoluted(long(some(abc))))[0] === "A") {
    const i = $function(convoluted(long(some(abc))))[1];
    return i;
  }
  if ($function(convoluted(long(some(abc))))[0] === "B") {
    const i = $function(convoluted(long(some(abc))))[1];
    return i;
  }
  if ($function(convoluted(long(some(abc))))[0] === "C") {
    const i = $function(convoluted(long(some(abc))))[1];
    return i;
  }
  throw new Error("Pattern match error");
}
export { convoluted, example, $function, long, some, A, B, C };

But now generates:

function example(abc) {
  const $0 = $function(convoluted(long(some(abc))));
  if ($0[0] === "A") {
    const i = $0[1];
    return i;
  }
  if ($0[0] === "B") {
    const i = $0[1];
    return i;
  }
  if ($0[0] === "C") {
    const i = $0[1];
    return i;
  }
  throw new Error("Pattern match error");
}

@codecov
Copy link

codecov bot commented Dec 29, 2022

Codecov Report

Merging #114 (8975da7) into main (8ac4c06) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #114      +/-   ##
==========================================
+ Coverage   84.34%   84.37%   +0.02%     
==========================================
  Files          83       83              
  Lines       15214    15242      +28     
==========================================
+ Hits        12833    12861      +28     
  Misses       2381     2381              
Impacted Files Coverage Δ
crates/ditto-codegen-js/src/convert.rs 93.94% <100.00%> (+0.24%) ⬆️
crates/ditto-codegen-js/src/optimize/mod.rs 95.47% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jmackie jmackie merged commit 43e5eae into main Dec 29, 2022
@jmackie jmackie deleted the cleverer-match-expressions branch December 29, 2022 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant