DEV Community

Discussion on: What are your thoughts on functional programming? In PHP?

Collapse
 
ohryan profile image
Ryan

It’s missing some of the niceties in syntax of a functional language though.

Such as?

Collapse
 
itsjzt profile image
Saurabh Sharma

These are some nice to have features in functional programming languages.

  • Function can be passed to other functions and returned as values. In short functions should be treated as values.

  • closures the inner function can use the variables of outer function regardless of where it is running.

  • Map, filter, reduce like functions.

  • pipeline operator.

Thread Thread
 
ohryan profile image
Ryan

Unless I'm misunderstanding the terms, I believe PHP has all of these features except a pipeline operator.

Thread Thread
 
underscorefunk profile image
John Funk • Edited

It's a different language, but check out the ramda library for examples of really awesome functional programming. The documentation is great. Chris Okahravi has a superb video tutorial series on every single function in ramda too. —> youtube.com/playlist?list=PLrhzvIc...

If you REALLY want to get into it, check out Prof. Frisby's Mostly Adequate Guide to Functional Programming github.com/MostlyAdequate/mostly-a...

Thread Thread
 
drewknab profile image
Drew Knab

+1 for Frisby’s

Collapse
 
drewknab profile image
Drew Knab • Edited

Pipeline and pattern matching. There’s probably some more syntactic sugar I’m not thinking of right now.

Basically it just results in “uglier” code.

You could probably approximate pipeline with some method chaining if you didn’t want to nest array functions as arguments to other array functions.

Thread Thread
 
underscorefunk profile image
John Funk

You can simulate pipeline type stuff with array reduce and unary functions.

What I really miss is built in currying/partial application, less verbose anonymous function syntax, and auto closures.

Oh, and auto return statements.

When you get used to writing JS and other FP stuff like (x,y)=> x + y and you can do (x) => (y) => x + y, it's really nice.

Thread Thread
 
drewknab profile image
Drew Knab • Edited

Pretty sure you can curry in PHP?

Edit: never mind, read your post wrong.

Thread Thread
 
underscorefunk profile image
John Funk

All good. :D I'm no FP expert and welcome corrections. Who knows... maybe my comments will yield some answers and it'll turn out that what I didn't think you could do easily, you can.