2 ms·
let updatePersonBalance = ({id, currentBalance}, txns) => { let balChange = txns .filter(i => id === i.personID) .map(i => i.amt)
by Scarbutt 6y ago
let updatePersonBalance = ({id, currentBalance}, txns) => {
let balChange = txns
.filter(i => id === i.personID)
.map(i => i.amt)
.reduce((i,j) => i + j);
return (balChange + currentBalance);
}
Defaulting to immutability it's not as useful in single-threaded environments like nodejs or the browser. The above function is pure though and it's an exact copy of yours but could make it smaller and still be readable.
Picking Clojure over JS today is a very hard sell. Both are dynamic and use the same information model, objects/arrays vs maps/vectors but with the difference that JS has a big enabling ecosystem of libraries. And there's Typescript when you need it.
- mbil 6y agoMy point wasn't to show that Clojure can produce a concise final function -- it was to show that each step in the process of developing that function can be evaluated within the editor (sorry, I tried to communicate this by including each form's output; not an easy process to show in a static comment), without copying and pasting to/from a REPL, without even leaving your current buffer. If you can do that with JS I would genuinely love to know (since I write JS frequently). > Picking Clojure over JS today is a very hard sell. I'm not sure I would choose ClojureScript over JS on the frontend. I haven't worked with ClojureScript much nor thought much about the trade-offs. > Defaulting to immutability it's not as useful in single-threaded environments like nodejs or the browser. It might not be as useful, but I think it's still useful. With immutability, you know that inside a scope you can do whatever you want with any data (ignoring atoms for a moment) and you're not going to step on somebody else's state. > with the difference that JS has a big enabling ecosystem of libraries ClojureScript has interoperability with all the JS libraries, no? And if you need strong typing, then I'd agree Clojure is not a good fit.
- beders 6y agoThis might be pure if you can guarantee no one is changing txns - for example - while updatePersonBalance runs. Throw in a async fn in there and all bets are off. Looking at state mgmt solutions for JS today, many will require or recommend immutable data - for very good reasons.