What's the Best Way to Approach Functional Programming as a Beginner?
Asked by
CodeExplorer42
I'm coming from a six-year background in imperative and object-oriented programming, and I want to expand my skills into functional programming. I've spent a little time trying to implement `Math.max()` in a functional style by using concepts like immutability and pure functions, but I'm still pretty new to this.
I've written a few approaches in JavaScript and would love your feedback on how I'm doing so far. I'm interested in knowing what works well, what doesn't, and how my approaches fit in with established functional programming concepts. I'm also open to seeing different methods, even if they're considered antipatterns, as it might help with my learning curve.
Answers
Approach #2 does seem like the best option overall, as itโs simple and expressive! That first method with recursion is a nice attempt at applying core concepts, but you'd normally want to avoid that kind of deep branching in functional languages. The third method is quite uniqueโฆ perhaps too unique? It's creative, but I think you might find there's a simpler route that gets to the same end result without complicating things.
Reply to this answer
If you're really looking to dive into functional programming, consider trying out Haskell. It's often seen as a pinnacle of functional programming. Your approach #2 is definitely the most idiomatic in JavaScript, while approach #1 is a valid way to think through it, though pattern matching would simplify it a lot. Iโm not totally sure about that chunking idea; it feels a bit convoluted for finding a maximum. Stick with the simpler functional concepts to start with!
Reply to this answer
I think your second approach using `reduce` is the most idiomatic way to do this in functional programming. It's concise, expressive, and gets the job done in a clean way! Your first approach with recursion is interesting but could be cleaner with pattern matching methods like in Haskell instead of if-else checks. It would make the code more readable. By the way, that third approach with chunking seems overcomplicated for this problem, but itโs great to see you experimenting!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions