Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Monad (functional programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Writer monad (JavaScript) <span id="Writer monad"></span> === Another common situation is keeping a [[log file]] or otherwise reporting a program's progress. Sometimes, a programmer may want to log even more specific, technical data for later [[profiling (computer programming)|profiling]] or [[debugging]]. The '''Writer monad''' can handle these tasks by generating auxiliary output that accumulates step-by-step. To show how the monad pattern is not restricted to primarily functional languages, this example implements a <code>Writer</code> monad in [[JavaScript]]. First, an array (with nested tails) allows constructing the <code>Writer</code> type as a [[linked list]]. The underlying output value will live in position 0 of the array, and position 1 will implicitly hold a chain of auxiliary notes: <syntaxhighlight lang="Javascript">const writer = value => [value, []];</syntaxhighlight> Defining {{mvar|unit}} is also very simple: <syntaxhighlight lang="Javascript">const unit = value => [value, []];</syntaxhighlight> Only {{mvar|unit}} is needed to define simple functions that output <code>Writer</code> objects with debugging notes: <syntaxhighlight lang="Javascript"> const squared = x => [x * x, [`${x} was squared.`]]; const halved = x => [x / 2, [`${x} was halved.`]]; </syntaxhighlight> A true monad still requires {{mvar|bind}}, but for <code>Writer</code>, this amounts simply to concatenating a function's output to the monad's linked list: <syntaxhighlight lang="Javascript"> const bind = (writer, transform) => { const [value, log] = writer; const [result, updates] = transform(value); return [result, log.concat(updates)]; }; </syntaxhighlight> The sample functions can now be chained together using {{mvar|bind}}, but defining a version of monadic composition (called <code>pipelog</code> here) allows applying these functions even more succinctly: <syntaxhighlight lang="Javascript"> const pipelog = (writer, ...transforms) => transforms.reduce(bind, writer); </syntaxhighlight> The final result is a clean separation of concerns between stepping through computations and logging them to audit later: <syntaxhighlight lang="Javascript"> pipelog(unit(4), squared, halved); // Resulting writer object = [8, ['4 was squared.', '16 was halved.']] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)