Skip to content

Commit

Permalink
⚡️ Tail-call optimize function captures
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi authored Apr 26, 2024
1 parent 23a5899 commit 2d86bf5
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/glam/doc.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -594,28 +594,32 @@ fn do_to_string(
|> do_to_string(max_width, indent, rest)
}

ForceBreak(doc) ->
[#(indent, ForceBroken, doc), ..rest]
|> do_to_string(acc, max_width, current_width, _)
ForceBreak(doc) -> {
let docs = [#(indent, ForceBroken, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}

Concat(docs) ->
list.map(docs, fn(doc) { #(indent, mode, doc) })
|> list.append(rest)
|> do_to_string(acc, max_width, current_width, _)
Concat(docs) -> {
let docs =
list.map(docs, fn(doc) { #(indent, mode, doc) })
|> list.append(rest)
do_to_string(acc, max_width, current_width, docs)
}

Group(doc) -> {
let fits = fits([#(indent, Unbroken, doc)], max_width, current_width)
let new_mode = case fits {
True -> Unbroken
False -> Broken
}
[#(indent, new_mode, doc), ..rest]
|> do_to_string(acc, max_width, current_width, _)
let docs = [#(indent, new_mode, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}

Nest(doc, i) ->
[#(indent + i, mode, doc), ..rest]
|> do_to_string(acc, max_width, current_width, _)
Nest(doc, i) -> {
let docs = [#(indent + i, mode, doc), ..rest]
do_to_string(acc, max_width, current_width, docs)
}

Text(text: text, length: length) ->
do_to_string(acc <> text, max_width, current_width + length, rest)
Expand Down

0 comments on commit 2d86bf5

Please sign in to comment.