How to resolve the algorithm Monads/Writer monad step by step in the Jsish programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Monads/Writer monad step by step in the Jsish programming language
Table of Contents
Problem Statement
The Writer monad is a programming design pattern which makes it possible to compose functions which return their result values paired with a log string. The final result of a composed function yields both a value, and a concatenation of the logs from each component function application. Demonstrate in your programming language the following:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Monads/Writer monad step by step in the Jsish programming language
Source code in the jsish programming language
'use strict';
/* writer monad, in Jsish */
function writerMonad() {
// START WITH THREE SIMPLE FUNCTIONS
// Square root of a number more than 0
function root(x) {
return Math.sqrt(x);
}
// Add 1
function addOne(x) {
return x + 1;
}
// Divide by 2
function half(x) {
return x / 2;
}
// DERIVE LOGGING VERSIONS OF EACH FUNCTION
function loggingVersion(f, strLog) {
return function (v) {
return {
value: f(v),
log: strLog
};
};
}
var log_root = loggingVersion(root, "obtained square root"),
log_addOne = loggingVersion(addOne, "added 1"),
log_half = loggingVersion(half, "divided by 2");
// UNIT/RETURN and BIND for the the WRITER MONAD
// The Unit / Return function for the Writer monad:
// 'Lifts' a raw value into the wrapped form
// a -> Writer a
function writerUnit(a) {
return {
value: a,
log: "Initial value: " + JSON.stringify(a)
};
}
// The Bind function for the Writer monad:
// applies a logging version of a function
// to the contents of a wrapped value
// and return a wrapped result (with extended log)
// Writer a -> (a -> Writer b) -> Writer b
function writerBind(w, f) {
var writerB = f(w.value),
v = writerB.value;
return {
value: v,
log: w.log + '\n' + writerB.log + ' -> ' + JSON.stringify(v)
};
}
// USING UNIT AND BIND TO COMPOSE LOGGING FUNCTIONS
// We can compose a chain of Writer functions (of any length) with a simple foldr/reduceRight
// which starts by 'lifting' the initial value into a Writer wrapping,
// and then nests function applications (working from right to left)
function logCompose(lstFunctions, value) {
return lstFunctions.reduceRight(
writerBind,
writerUnit(value)
);
}
var half_of_addOne_of_root = function (v) {
return logCompose(
[log_half, log_addOne, log_root], v
);
};
return half_of_addOne_of_root(5);
}
var writer = writerMonad();
;writer.value;
;writer.log;
/*
=!EXPECTSTART!=
writer.value ==> 1.61803398874989
writer.log ==> Initial value: 5
obtained square root -> 2.23606797749979
added 1 -> 3.23606797749979
divided by 2 -> 1.61803398874989
=!EXPECTEND!=
*/
You may also check:How to resolve the algorithm Palindrome detection step by step in the Retro programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the J programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the PL/I programming language
You may also check:How to resolve the algorithm Pi step by step in the bc programming language
You may also check:How to resolve the algorithm Even or odd step by step in the ALGOL 68 programming language