Open alisajadih opened 2 years ago
EDIT2: Nvm, just saw this https://github.com/MostlyAdequate/mostly-adequate-guide/pull/252, and realized that I needed to use the "pumped up" version of curry
, as defined in exercises/support.js
:
// NOTE A slightly pumped up version of `curry` which also keeps track of
// whether a function was called partially or with all its arguments at once.
// This is useful to provide insights during validation of exercises.
function curry(fn) {
assert(
typeof fn === 'function',
typeMismatch('function -> ?', [getType(fn), '?'].join(' -> '), 'curry'),
);
const arity = fn.length;
return namedAs(fn.name, function $curry(...args) {
$curry.partially = this && this.partially;
if (args.length < arity) {
return namedAs(fn.name, $curry.bind({ partially: true }, ...args));
}
return fn.call(this || { partially: false }, ...args);
});
}
EDIT: So I finally got it working in javascript, but liftA2(concat)
needed an additional curry, for reasons I do not understand.
As we can see below, I had to liftA2(curry(liftA2(concat)), x, y)
in order for lifting twice to work,
otherwise there is an error in liftA2
when calling .ap
.
#+begin_src js :noweb no-export :results code
<<js maybe applicative>>
<<js task applicative>>
<<js compose>>
<<js liftA2>>
<<js identity>>
<<js concat>>
const tOfM = compose(Task.of, Maybe.of)
const x = tOfM("Hello ")
const y = tOfM("World")
const curriedAndLiftedConcat = curry(liftA2(concat))
const z = liftA2(curriedAndLiftedConcat, x, y)
////// ERROR //////
// NOTE this curry-less function throws an error, why???
const onlyLiftedConcat = liftA2(concat)
const a = liftA2(onlyLiftedConcat, x, y)
let afork
try {
afork = a.fork(id, id)
} catch (error) {
afork = error.message
}
////// ERROR //////
return {
// x,
// xfork: x.fork(id, id),
// y,
// yfork: y.fork(id, id),
z,
zfork: z.fork(id, id),
a,
afork
}
#+end_src
#+RESULTS:
#+begin_src js
{
z: Task { fork: [Function (anonymous)] },
zfork: Maybe { val: 'Hello World' },
a: Task { fork: [Function (anonymous)] },
afork: "Cannot read properties of undefined (reading 'map')"
}
#+end_src
Dependencies implementations:
In chapter 10 on part laws. there is a code that demonstrates "applicatives are close under composition".
I think we could remove liftA2 wrapper for
concat
function, It's useless.liftA2(concat, tOfM('Rainy Days and Mondays'), tOfM(' always get me down'));