by drawfloat · 4y ago · 0 comments
const flow = (...fns) => x0 => fns.reduce(
(x, f) => f(x),
x0
);
function slamTimTamsUsingFlow(timtams) {
return timtams
.slice(0, MAX_TIMTAMS)
.map(flow(
biteArbitraryCorner,
biteOppositeCorner,
insertInBeverage,
drawLiquid,
insertIntoMouth));
}
Just be written as: const slamTimTams = timtams =>
timtams
.slice(0, MAX_TIMTAMS)
.map(timtam => timtam.biteArbitraryCorner()
.biteOppositeCorner()
.insertInBeverage()
.drawLiquid()
.insertIntoMouth());
Feels much more concise and readable than the flow example function slamTimTam(timtam) {
timtam.biteArbitraryCorner();
timtam.biteOppositeCorner();
timtam.insertInBeverage();
timtam.drawLiquid();
timtam.insertIntoMouth();
}
function slamTimTams(timtams) {
const slammedTimTams = timtams.slice(0, MAX_TIMTAMS);
for (const timtam of slammedTimTams) {
slamTimTam(timtam);
}
return slammedTimTams;
}That doesn't look "functional" at all. The "slamTimTam" function is modifying an object it's been given. Modifying an object that you've taken as an argument should almost never happen anywhere in any codebase, much less a functional one.
Although it's not totally your fault: TFA seems completely confused about what "map" actually does, itself. I can't really tell whether they know what immutability is or not.
In fact one of my biggest pet peeves is "fluent" style code that modifies the object it's being called on. D3 in JavaScript is a perfect example of this abominable perversion of that "functional" style.
Yes... because he ditched it.
And further - there isn't really anything wrong with modifying an object. Functional programs are nice because they remove lots of extraneous state, but there are many situations where you still have to track state somewhere - doing it in a contained object that is changed is fine.
The issue (particularly in JS) is when you allow assignment by reference value, and not by copy - in that case modifying an object can be dangerous because other parts of the code may be holding onto the same reference, and you've accidentally introduced subtle shared state.
Ideally - all users would be aware, and would make sure that assignment only happens with a call to something like copy()/clone() but the language doesn't really have the tooling to enforce this (unlike many other languages ex: c++/Rust/others)
In the article, it seems like we are just looping over a set of 11 cookies and doing exactly the same thing to all of them. Not sure what I'm missing. The stuff after that regarding infinite sequences is pretty good though!
You're missing that we're not just looping over a set of 11 cookies and doing exactly the same thing to all of them. We're streaming over a lazy sequence of cookies and doing the same thing to them until we stop (in the example when they hit 5 cookies eaten instead of eating them all and, presumably, becoming ill).
Generators act like streams, and are useful in any place you might see this pattern:
value, state = next_value(state)
-- alternatively if we can mutate the state directly --
value = next_value(state)
But they permit you a greater deal of flexibility about the way "state" mutates than a typical state structure might (or at least greater ease of use).The whole point of a generator is its stack frame and its “instruction pointer”. You can implement a state machine right in regular code via cheap primitives. Otherwise the difference is just syntactical, afaiu.
function* bar() {
yield 3;
console.log("Resumed after the 3")
yield 4;
console.log("Resumed after the 4")
}
function* foo() {
yield 1;
console.log("Resumed after the 1")
yield 2;
console.log("Resumed after the 2")
yield* bar();
}
for(let n of foo()) {
console.log(n);
}
Output: 1
Resumed after the 1
2
Resumed after the 2
3
Resumed after the 3
4
Resumed after the 4The difference here is push vs pull (also called eager vs lazy). It is often easier to write pull computations that only do the work that is actually needed.