News for hackers. Stuff that matters.
Re:Embracing Functional Programming in Ruby (2017) by Toutouxc on Saturday February 20, 2021 @03:07AM (#26202490)
Dressing a pure function as an object sounds a bit OOP cargo cultish, like promoting weird service "objects" at all costs instead of domain objects that would actually behave like objects (e.g. a ReserveStuffService instead of a StuffReservation).

My solution in this case is similar. Instead of a PayrollCalculator returning a PayrollResult value-object I'd rather have an immutable CalculatedPayroll or PayrollCalculation or something that lazy-calculates and memoizes the necessary values internally, exposing only the taxes, debits etc. variables, and maybe a single method called "precalculate" that would trigger the process eagerly.

I could do new kinds of calculations (California, New York) with simple inheritance: CaliforniaPayrollCalculation, overriding some of the private methods.

Is there a problem with my implementation that will bite me in the ass later?

Re:Embracing Functional Programming in Ruby (2017) by blunte on Saturday February 20, 2021 @06:36AM (#26203410)
I don’t see how this approach is better than just using modules with pure functions that operate on arrays and hashes.
Re:Embracing Functional Programming in Ruby (2017) by leecommamichael on Saturday February 20, 2021 @02:11AM (#26202264)
It seems to be the safe bet to write dynamic languages in a functional way.
Re:Embracing Functional Programming in Ruby (2017) by cutler on Saturday February 20, 2021 @06:36AM (#26203411)
Why not just use a functional language? Elixir and Clojure are dynamic like Ruby.
Re:Embracing Functional Programming in Ruby (2017) by fulafel on Saturday February 20, 2021 @06:45AM (#26203446)
That would need a rewrite from scratch instead of the described refactoring and introducing more functional concepts.
Re:Embracing Functional Programming in Ruby (2017) by blunte on Saturday February 20, 2021 @07:22AM (#26203627)
I wonder what the possibilities would be if using jruby to interoperate with clojure.
Re:Embracing Functional Programming in Ruby (2017) by adenozine on Saturday February 20, 2021 @07:57AM (#26203779)
Oooo, yeah that's an interesting one. Especially with clojure's macros, surely there's some cool possibilities
Re:Embracing Functional Programming in Ruby (2017) by eyelidlessness on Saturday February 20, 2021 @04:07PM (#26207986)
There are lots of reasons to choose an imperative language and write functional code in it. Ecosystem, familiarity, quality of documentation, likelihood of finding helpful solutions on the web, hiring pool. I could go on.

I spent a few years working in Clojure, and I’m glad I did. It taught me to think in FP. Now I use those same FP techniques in a language I vastly prefer overall (TypeScript).

There’s really very little reason not to write functional or mostly-functional code in any language.

Re:Embracing Functional Programming in Ruby (2017) by cutler on Sunday February 21, 2021 @10:41AM (#26214238)
Why would you prefer Typescript over, say, Clojurescript if functional is your priority? It's the immutability that matters in functional. Typescript is just a better way to manage mutable code.
Re:Embracing Functional Programming in Ruby (2017) by eyelidlessness on Sunday February 21, 2021 @05:05PM (#26217722)
> Why would you prefer Typescript over, say, Clojurescript if functional is your priority?

Because the benefits of a powerful static type system (type checking, documentation, refactoring) are also a priority.

I’ve used ClojureScript extensively, and shipped cljs projects in production. But one of the problems I encountered frequently was completely inexplicable behavior when I mistakenly passed incompatible values to core functions. Absolutely bonkers errors or silent failures I had to track deep into the inner recesses of cljs.core etc, generally having to reverse engineer the advanced compilation to understand what had happened.

When I filed bugs on cases like this, eg asking for a meaningful runtime error, they were without exception closed as WONTFIX with snooty notes like “just don’t pass incompatible values to core functions”.

While I’m quite a fan of TDD, even my extensive coverage would sometimes fail to catch issues like that. But TypeScript routinely catches them for me.

> It's the immutability that matters in functional.

I believe the ML family of languages would strongly disagree...

> Typescript is just a better way to manage mutable code.

TypeScript is a better way to catch type errors in JavaScript, mutable and immutable alike. But it does help enforce immutability if you use `readonly`/`as const` extensively, which I do.

Sure, I’d vastly prefer immutability was the default. But it’s a small price to pay for such huge productivity and safety improvements.

Re:Embracing Functional Programming in Ruby (2017) by hkazemi on Saturday February 20, 2021 @03:40AM (#26202659)
Timeless piece