Are you over 18 and want to see adult content?
More Annotations
A complete backup of grandappeal.org.uk
Are you over 18 and want to see adult content?
A complete backup of tahghigh-tahghigh.blogsky.com
Are you over 18 and want to see adult content?
A complete backup of alex-fotografias.blogspot.com
Are you over 18 and want to see adult content?
A complete backup of divinadelivery.com.br
Are you over 18 and want to see adult content?
A complete backup of licitaciones.com.bo
Are you over 18 and want to see adult content?
A complete backup of desguacesalcala.com
Are you over 18 and want to see adult content?
A complete backup of thomashueblonline.com
Are you over 18 and want to see adult content?
A complete backup of toxophilites.com
Are you over 18 and want to see adult content?
A complete backup of gelavasadze.livejournal.com
Are you over 18 and want to see adult content?
A complete backup of austinrelocationguide.com
Are you over 18 and want to see adult content?
Favourite Annotations
A complete backup of https://practicalmommy.com
Are you over 18 and want to see adult content?
A complete backup of https://androidguys.com
Are you over 18 and want to see adult content?
A complete backup of https://hachettego.com
Are you over 18 and want to see adult content?
A complete backup of https://practical-law.com
Are you over 18 and want to see adult content?
A complete backup of https://diecastsociety.com
Are you over 18 and want to see adult content?
A complete backup of https://houseofswitzerland.org
Are you over 18 and want to see adult content?
A complete backup of https://bisa.ac.uk
Are you over 18 and want to see adult content?
A complete backup of https://quidax.com
Are you over 18 and want to see adult content?
A complete backup of https://ostem.org
Are you over 18 and want to see adult content?
A complete backup of https://adfontesmedia.com
Are you over 18 and want to see adult content?
A complete backup of https://tiesdb.com
Are you over 18 and want to see adult content?
A complete backup of https://ebaytechblog.com
Are you over 18 and want to see adult content?
Text
HOME | GARFIELDTECH
Home | GarfieldTech. Good technical writing is hard. A few days ago, I randomly tossed this out on Twitter without context: Technical writing requires assuming the reader is simultaneously highly intelligent and utterly ignorant, without making them feel likeHOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchPRESENTER BIO
Playful bio. Larry Garfield is an aspiring blacksmith who moonlights as a Staff Engineer for the TYPO3 project. When not trying to hand-forge his own medieval armory from scratch he tries to teach developers and development managers the skills of yesteryear that the industry has forgotten. He is the author of several books on PHP,including
PROGRAMMING LANGUAGE TRADE-OFFS START GIT WITH AN EMPTY COMMIT My personal favorite is to simply start the history with a null commit. Git normally won't let you make a commit if there's nothing to do, but you can easily disable that check. My new Git projects now tend to start like so: git init. git commit --allow-empty -m "Initial commit". Boom, done. Now my repository has a single commit in it, the THE 3 PARAGRAPH PITCHTMI ABOUT ME
TMI About me. Submitted by Larry on 22 March 2017 - 3:49pm. Recently, I've become aware of a whisper campaign going around the Drupal world, discussing details about my personal life. I do not know where it started or who all has been part of it. However, it's become apparent that it's not going to settle down, and has now had a major, direct ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY ORMs vs. Query Builders: Database portability. Submitted by Larry on 1 July 2009 - 10:59pm. There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern. THE CONTAINER IS A LIE! Linux kernel specific (not GNU) Other OSes didn't add features, or did very differently; 6 namespace types added piecemeal; UTS Namespace. sethostname(), setdomainname(), and uname()HOME | GARFIELDTECH
Home | GarfieldTech. Good technical writing is hard. A few days ago, I randomly tossed this out on Twitter without context: Technical writing requires assuming the reader is simultaneously highly intelligent and utterly ignorant, without making them feel likeHOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchPRESENTER BIO
Playful bio. Larry Garfield is an aspiring blacksmith who moonlights as a Staff Engineer for the TYPO3 project. When not trying to hand-forge his own medieval armory from scratch he tries to teach developers and development managers the skills of yesteryear that the industry has forgotten. He is the author of several books on PHP,including
PROGRAMMING LANGUAGE TRADE-OFFS START GIT WITH AN EMPTY COMMIT My personal favorite is to simply start the history with a null commit. Git normally won't let you make a commit if there's nothing to do, but you can easily disable that check. My new Git projects now tend to start like so: git init. git commit --allow-empty -m "Initial commit". Boom, done. Now my repository has a single commit in it, the THE 3 PARAGRAPH PITCHTMI ABOUT ME
TMI About me. Submitted by Larry on 22 March 2017 - 3:49pm. Recently, I've become aware of a whisper campaign going around the Drupal world, discussing details about my personal life. I do not know where it started or who all has been part of it. However, it's become apparent that it's not going to settle down, and has now had a major, direct ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY ORMs vs. Query Builders: Database portability. Submitted by Larry on 1 July 2009 - 10:59pm. There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern. THE CONTAINER IS A LIE! Linux kernel specific (not GNU) Other OSes didn't add features, or did very differently; 6 namespace types added piecemeal; UTS Namespace. sethostname(), setdomainname(), and uname()HOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchARCHITECTURE
Programming paradigms, like software architecture, have trade-offs. In fact, many of the same methods for comparing architectural designs apply just as well to language design. To do that, though, we need to take a step back and look at more than just PHP-style objects.WEBSITE FEEDBACK
Technical thoughts, tutorials, and musings. Main navigation. Show — Main navigation Hide — Main navigation. Home; Presentations; Contact MAGICAL PHP: __CALL() Each magic method begins with __, and gets called automatically by PHP under certain conditions. Technically some are called overloading methods rather than magic methods, but they all effectively work by the same magic blue smoke. Today we're just going to look at one of them: __call (). If a class implements __call (), then if an object of SHORT AND SAFE ARRAY ITERATION Short and safe array iteration. One reason to follow development mailing lists is you sometimes pick up on some very neat tricks. Here's one that I spotted on the PHP Internals list recently to simplify array iteration in PHP 7. PHP's largely loose, dynamic typing has plenty of both pros and cons. ONE YEAR OF FUNCTIONAL PHP; NOW IN RUSSIAN! Mission accomplished! To celebrate the one year anniversary of the book's publication, I am happy to make two announcements. First, Thinking Functionally in PHP is now available in Russian! The translation is by Alexey Pyltsyn, who is responsible for the Russian translation of the PHP documentation as well as numerous other techbook translations.
AN OPEN LETTER TO CONFERENCE ORGANIZERS An open letter to conference organizers. Submitted by Larry on 19 July 2014 - 6:51pm. Let's be honest, I spend a lot of time at conferences. Over the past 2 years or so I've averaged more than one speaking engagement at a conference per month, including a half-dozen keynotes. I've also helped organize several conferences, mostly DrupalCamps andDON'T GO LOW
Don't go low. Submitted by Larry on 16 April 2017 - 1:09pm. This is a very uncomfortable time for Drupal. It seems there has been a great deal of concern and discomfort lurking just below the surface for a while, and recent events surrounding me have brought them all to the forefront at once. It's going to take some time to unravel it all.GARFIELDTECH
It's been an eventful couple of days, that's for sure Eventful enough that I feel it's necessary to clarify a few points. First off, I want to express my sincere thanks to everyone that has reached out, in public or in private, to express their support in this situation.HOME | GARFIELDTECH
Home | GarfieldTech. Good technical writing is hard. A few days ago, I randomly tossed this out on Twitter without context: Technical writing requires assuming the reader is simultaneously highly intelligent and utterly ignorant, without making them feel likeHOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchPRESENTER BIO
Playful bio. Larry Garfield is an aspiring blacksmith who moonlights as a Staff Engineer for the TYPO3 project. When not trying to hand-forge his own medieval armory from scratch he tries to teach developers and development managers the skills of yesteryear that the industry has forgotten. He is the author of several books on PHP,including
PROGRAMMING LANGUAGE TRADE-OFFS START GIT WITH AN EMPTY COMMIT My personal favorite is to simply start the history with a null commit. Git normally won't let you make a commit if there's nothing to do, but you can easily disable that check. My new Git projects now tend to start like so: git init. git commit --allow-empty -m "Initial commit". Boom, done. Now my repository has a single commit in it, the THE 3 PARAGRAPH PITCHTMI ABOUT ME
TMI About me. Submitted by Larry on 22 March 2017 - 3:49pm. Recently, I've become aware of a whisper campaign going around the Drupal world, discussing details about my personal life. I do not know where it started or who all has been part of it. However, it's become apparent that it's not going to settle down, and has now had a major, direct ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY ORMs vs. Query Builders: Database portability. Submitted by Larry on 1 July 2009 - 10:59pm. There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern. THE CONTAINER IS A LIE! Linux kernel specific (not GNU) Other OSes didn't add features, or did very differently; 6 namespace types added piecemeal; UTS Namespace. sethostname(), setdomainname(), and uname()HOME | GARFIELDTECH
Home | GarfieldTech. Good technical writing is hard. A few days ago, I randomly tossed this out on Twitter without context: Technical writing requires assuming the reader is simultaneously highly intelligent and utterly ignorant, without making them feel likeHOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchPRESENTER BIO
Playful bio. Larry Garfield is an aspiring blacksmith who moonlights as a Staff Engineer for the TYPO3 project. When not trying to hand-forge his own medieval armory from scratch he tries to teach developers and development managers the skills of yesteryear that the industry has forgotten. He is the author of several books on PHP,including
PROGRAMMING LANGUAGE TRADE-OFFS START GIT WITH AN EMPTY COMMIT My personal favorite is to simply start the history with a null commit. Git normally won't let you make a commit if there's nothing to do, but you can easily disable that check. My new Git projects now tend to start like so: git init. git commit --allow-empty -m "Initial commit". Boom, done. Now my repository has a single commit in it, the THE 3 PARAGRAPH PITCHTMI ABOUT ME
TMI About me. Submitted by Larry on 22 March 2017 - 3:49pm. Recently, I've become aware of a whisper campaign going around the Drupal world, discussing details about my personal life. I do not know where it started or who all has been part of it. However, it's become apparent that it's not going to settle down, and has now had a major, direct ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY ORMs vs. Query Builders: Database portability. Submitted by Larry on 1 July 2009 - 10:59pm. There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern. THE CONTAINER IS A LIE! Linux kernel specific (not GNU) Other OSes didn't add features, or did very differently; 6 namespace types added piecemeal; UTS Namespace. sethostname(), setdomainname(), and uname()HOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particular LOG IN | GARFIELDTECH Enter the password that accompanies your username. Search. SearchARCHITECTURE
Programming paradigms, like software architecture, have trade-offs. In fact, many of the same methods for comparing architectural designs apply just as well to language design. To do that, though, we need to take a step back and look at more than just PHP-style objects.WEBSITE FEEDBACK
Technical thoughts, tutorials, and musings. Main navigation. Show — Main navigation Hide — Main navigation. Home; Presentations; Contact MAGICAL PHP: __CALL() Each magic method begins with __, and gets called automatically by PHP under certain conditions. Technically some are called overloading methods rather than magic methods, but they all effectively work by the same magic blue smoke. Today we're just going to look at one of them: __call (). If a class implements __call (), then if an object of SHORT AND SAFE ARRAY ITERATION Short and safe array iteration. One reason to follow development mailing lists is you sometimes pick up on some very neat tricks. Here's one that I spotted on the PHP Internals list recently to simplify array iteration in PHP 7. PHP's largely loose, dynamic typing has plenty of both pros and cons. ONE YEAR OF FUNCTIONAL PHP; NOW IN RUSSIAN! Mission accomplished! To celebrate the one year anniversary of the book's publication, I am happy to make two announcements. First, Thinking Functionally in PHP is now available in Russian! The translation is by Alexey Pyltsyn, who is responsible for the Russian translation of the PHP documentation as well as numerous other techbook translations.
AN OPEN LETTER TO CONFERENCE ORGANIZERS An open letter to conference organizers. Submitted by Larry on 19 July 2014 - 6:51pm. Let's be honest, I spend a lot of time at conferences. Over the past 2 years or so I've averaged more than one speaking engagement at a conference per month, including a half-dozen keynotes. I've also helped organize several conferences, mostly DrupalCamps andDON'T GO LOW
Don't go low. Submitted by Larry on 16 April 2017 - 1:09pm. This is a very uncomfortable time for Drupal. It seems there has been a great deal of concern and discomfort lurking just below the surface for a while, and recent events surrounding me have brought them all to the forefront at once. It's going to take some time to unravel it all.GARFIELDTECH
It's been an eventful couple of days, that's for sure Eventful enough that I feel it's necessary to clarify a few points. First off, I want to express my sincere thanks to everyone that has reached out, in public or in private, to express their support in this situation.HOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particularPRESENTER BIO
Long bio. Larry Garfield has been building websites since he was a sophomore in high school, which is longer ago than he'd like to admit. Larry is currently a Staff Engineer with the TYPO3 project.ARCHITECTURE
At DrupalCon Chicago, Dries announced that the development process for Drupal 8 would be a bit different. Rather than a vast dog pile of efforts to improve Drupal in ways big and small, Drupal 8 will feature a number of major "core initiatives". These initiatives highlight major areas of work that represent not just a patch or three but major changes to Drupal's plumbing. ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern.. Rather than tuck such discussion away in an issue queue, I figured it better to document a bit more widely. PHP GROUP BY WITH ARRAYS Note that we're also adding a ksort() line in there. Remember that we're fetching records by color, then name. That means we could get back , , which the while loop will then turn into an array with keys , which is backwards.The ksort() will properly order the top array while maintaining the key/value associations for the lower arrays. SHORT AND SAFE ARRAY ITERATION That avoids any whining from PHP, but at the cost of more annoying boilerplate code. When you're parsing through a large and complex data structure (the aforementioned YAML or JSON data), that can add up to a lot of irritating extra code, especially if you also need to check even deeper array levels. START GIT WITH AN EMPTY COMMIT Recently I've had reason to start several new projects with Git. That's nothing exciting (except for me), but it means I've been making a lot of first-commits, and often rebasing my early commit history before making it public.TMI ABOUT ME
What is a real shame, is that you state emphatically a fear-like expectation of reprisal - This is the Drupal community that has cometo pass, one that
DON'T GO LOW
This is a very uncomfortable time for Drupal. It seems there has been a great deal of concern and discomfort lurking just below the surface for a while, and recent events surrounding me have brought them all to the forefront at once. RESPONSE TO CONVERSATIONS ABOUT MEHOME | GARFIELDTECH
In my last post, I went over some of the pros and cons of various proposals for making PHP objects more immutable-ish, and the contexts in which they would be useful.I also posted the link to the PHP Internals list, where it generated some interesting if meandering discussion (as is par for the course on Internals).. One of the requests was for sample code to demonstrate why I felt particularPRESENTER BIO
Long bio. Larry Garfield has been building websites since he was a sophomore in high school, which is longer ago than he'd like to admit. Larry is currently a Staff Engineer with the TYPO3 project.ARCHITECTURE
At DrupalCon Chicago, Dries announced that the development process for Drupal 8 would be a bit different. Rather than a vast dog pile of efforts to improve Drupal in ways big and small, Drupal 8 will feature a number of major "core initiatives". These initiatives highlight major areas of work that represent not just a patch or three but major changes to Drupal's plumbing. ORMS VS. QUERY BUILDERS: DATABASE PORTABILITY There has been some discussion in recent days regarding Object-Relational Mappers (ORMs), Drupal, and why the latter doesn't use the former. There are, actually, many reasons for that, and for why Drupal doesn't do more with the Active Record pattern.. Rather than tuck such discussion away in an issue queue, I figured it better to document a bit more widely. PHP GROUP BY WITH ARRAYS Note that we're also adding a ksort() line in there. Remember that we're fetching records by color, then name. That means we could get back , , which the while loop will then turn into an array with keys , which is backwards.The ksort() will properly order the top array while maintaining the key/value associations for the lower arrays. SHORT AND SAFE ARRAY ITERATION That avoids any whining from PHP, but at the cost of more annoying boilerplate code. When you're parsing through a large and complex data structure (the aforementioned YAML or JSON data), that can add up to a lot of irritating extra code, especially if you also need to check even deeper array levels. START GIT WITH AN EMPTY COMMIT Recently I've had reason to start several new projects with Git. That's nothing exciting (except for me), but it means I've been making a lot of first-commits, and often rebasing my early commit history before making it public.TMI ABOUT ME
What is a real shame, is that you state emphatically a fear-like expectation of reprisal - This is the Drupal community that has cometo pass, one that
DON'T GO LOW
This is a very uncomfortable time for Drupal. It seems there has been a great deal of concern and discomfort lurking just below the surface for a while, and recent events surrounding me have brought them all to the forefront at once. RESPONSE TO CONVERSATIONS ABOUT MEARCHITECTURE
At DrupalCon Chicago, Dries announced that the development process for Drupal 8 would be a bit different. Rather than a vast dog pile of efforts to improve Drupal in ways big and small, Drupal 8 will feature a number of major "core initiatives". These initiatives highlight major areas of work that represent not just a patch or three but major changes to Drupal's plumbing.WEBSITE FEEDBACK
Technical thoughts, tutorials, and musings. Main navigation. Show — Main navigation Hide — Main navigation. Home; Presentations; Contact BEYOND ABSTRACT CLASSES Recently, Anthony Ferrara has been posting a periodic "Beyond" series about software design philosophy. Some in particular have hinted at concepts I've been pondering as well. With his blessing, therefore, consider this a continuation of that series. SHORT AND SAFE ARRAY ITERATION That avoids any whining from PHP, but at the cost of more annoying boilerplate code. When you're parsing through a large and complex data structure (the aforementioned YAML or JSON data), that can add up to a lot of irritating extra code, especially if you also need to check even deeper array levels. INTRODUCING YOUR DATABASE TEAM Back when Drupal 7 development opened, our database layer was in a sorry state. Based on PHP 3-era concepts it offered few features, but more importantly no one actually cared about it.THE END OF AN ERA
Today is the end of an era. After just over ten and a half years, this is my last day with Palantir.net.. The past decade has seen Palantir grow from a company of 5 to a company of over 30. ANNOUNCING "THINKING FUNCTIONALLY IN PHP" Calling all PHP developers! Want to wrap your brain around functional programming? Are you sick of reading tutorials about this magic "monad" thing but having no idea how to read Haskell?PSR-14: EXAMPLE
So far in our 5 part series we've dug into the details of Events, Dispatchers, and Providers. An awful lot of flexibility can be had from just three simple methods. PHP 7 EXPLAINED: A REVIEW The team over at thePHP.cc recently published an ebook entitled PHP 7 Explained, "Everything you need to know about the next generation.". A few weeks back, I received an email from them stating "As a token of our appreciation for your involvement in the PHP community we would like to gift you a copy of our eBook." TYPE MATCHING IN PHP Technical thoughts, tutorials, and musings. Main navigation. Show — Main navigation Hide — Main navigation. Home; Presentations; Contact Skip to main contentUSER ACCOUNT MENU
Show — User account menu Hide — User account menu* Log in
GarfieldTech
Technical thoughts, tutorials, and musingsMAIN NAVIGATION
Show — Main navigation Hide — Main navigation * " class="is-active">Home* Presentations
* Contact
* Presenter Bio
Byte-sized functional programming: Composition over inheritance forfunctions, too
A popular refrain in object-oriented code is to favor object composition over inheritance. It offers more flexibility, less overhead, and ends up being easier to reason about. The same concept applies to functions, too! A common pattern is to have a function that does some work and then calls another function, which does some work and calls another function, and so on. The problem is that the first function then cannot be used or tested without the entire chain of other functions it calls. This is the function equivalent of "inheritance." Instead, we can compose functions, that is, pipe them together. Instead, take the output of the first function and pass it to the second, then take the second's output and pass it to the third, etc. That way, each of the functions can be reused, tested, and understood in isolation, then we can stick them together like LEGO blocks to build whatever series of steps we want. That is, instead of this:function A($in)
{
// ...
return B($out);
}
function
B($in)
{
// ...
return C($out);
}
function
C($in)
{
// ...
return $out;
}
?>
Structure it like this: function A($in)
{
// ...
return $out;
}
function
B($in)
{
// ...
return $out;
}
function
C($in)
{
// ...
return $out;
}
function
doit($in) {
$out = A($in);
$out = B($out);
$out = C($out);
return $out;
}
?>
Now `A()`, `B()`, and `C()` are all easier to read, understand, and test, and we can more easily add a step B2 or D if we want. So powerful is this concept that many languages have a native operator for piping functions together like that. PHP doesn't, yet, but it's straightforward enough to do in user space anyway. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 15 August 2020 - 10:07am * Read more about Byte-sized functional programming: Composition over inheritance for functions, too Byte-sized functional programming: Filter first Often when working with a list, we only want to work with a subset of a list that meets some criteria. All non-zero values, for example, or all users that have a given role. The procedural way to do that is to stick an if statement inside a foreach loop: foreach ($list as $value) { If (!meets_criteria($value)) { continue;
}
// ...
}
?>
That mixes up the filtering with the iteration, though. It also doesn't work if we're using `array_map()`. Instead, we can make stripping down the list a separate operation called "filter." PHP offers the array_filter() function for that purpose.
$criteria = fn(User $user): bool => $user->hasRole('moderator'); $filtered = array_filter($users, $criteria); ?>
Now we can work with the `$filtered` list, which has only the values we want. That could be a simple foreach loop, or, better, it's now ideally suited for use with array_map(). ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 3 August 2020 - 1:47pm * Read more about Byte-sized functional programming: Filter first Byte-sized functional programming: Mapping out your data Procedural code tends to think in terms of writing out steps, and so the usual way to work with a list is to iterate it using a `for` or `foreach` loop.
Functional code tends to think in terms of the relationships and transformations between data, where those relationships and transformations are defined as functions. That means the natural way to work with a list is to define a function that is the relationship between one list and another. The most common way of doing that is with a "map," which in PHP usually means the `array_map()` function. With `array_map()`, you give it an array and a function. You get back the result of applying that function to every element in the array, individually. Like so: $arr = ;
$fun = fn($x) => $x * 2; $result = array_map($fn, $arr); ?>
The advantages of that over a `foreach` loop are: * The function is a separate operation that can be as complex as you want.
* If it's more than a line or two, make it its own function or method somewhere and test it in isolation. * If it's trivial, you can simply inline it. * It's clear, visually, that every element's transformation is independent of every other's, because that's how `array_map()` works. A `foreach` loop may maintain state from one iteration to the next, but `array_map()` does not. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 27 July 2020 - 3:42pm * Read more about Byte-sized functional programming: Mapping out your data
Byte-sized functional programming: Immutable variables are easier to understand
An immutable variable is one whose value doesn't change after it has been first set. PHP doesn't natively support that, but we can write our classes in such a way to simulate it. For example, rather than this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function moveUp(int $by): void {
$this->y += $by;
}
}
?>
We can write this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function
moveUp(int $by): Point {
$new = clone ($this); $new->y += $by;
return $new;
}
}
?>
In the second version, once a given `Point` object is created it will never change. We can safely pass it to another function or method and be guaranteed that its value won't change without us knowing. Instead, any attempt to change it results in a new object, with its own identity, representing the new point in space. (In practice there would be other methods here as well, but we're focusing on just the mutation part.)
Code that uses immutable variables is easier to think about, because we don't have to worry about "does passing this object to this function change it?" We know it doesn't. Once we know something about an object we can guarantee that fact doesn't change. That can make a lot of subtle bugs impossible, which means we don't have to spend time looking for or correcting them. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 21 July 2020 - 7:30am * Read more about Byte-sized functional programming: Immutable variables are easier to understand Advice for new speakers Someone messaged me recently to say he had just been selected for his first-ever conference talk, and since the talks of mine he'd seen in the past were so inspiring he wanted to know if I had any advice for new speakers. Since flattery will often get you somewhere, I offered the following advice. I figure it's generic enough that I should share it more widely. :-)
_Continue reading this post on PeakD ._
Larry 18 July 2020 - 5:18pm * Read more about Advice for new speakers Byte-sized functional programming: Pure functions make testing easy When testing stateful code, you have to first set up all of the explicit and implicit state that a given piece of code depends on. If the object you're testing has many dependencies that could be complex, or references global variables, or calls many other routines with their own dependencies, you may be looking at a lot of complex setup that makes testing hard to do effectively. Pure functions greatly reduce that problem. A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That means there is, by definition, only one place to inject dependencies: the call itself. And there's only one effect to validate: the return value of the function. No need to call a function and check what the effect was on some other value or piece of code: by definition, the only effect is the return value. If one of the parameters you pass in is itself complex, that may make the test complex. But if the parameter is complex, that can serve as an impetus to simplify it. Rather than passing in an object, for example, just pass in a single function. If that function in practice has more complexity behind it, fine, but it makes passing a mock function trivial. Just... make a new (anonymous) function for the one test.
When your code has fewer sneaky interactions, there's less effort involved in testing as well as fewer things to test. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 14 July 2020 - 9:28am * Read more about Byte-sized functional programming: Pure functions make testing easy
Byte-sized functional programming: Pure functions encourage small code One of the many pieces of advice for a long-term sustainable code base is to keep code small. The larger a code base is, the more effort it takes to understand all the moving parts. Your brain can only keep so much mental model of your code in it at once, and if the code you're looking at is too large then what you can fit in your own "active memory" at once then you will have an increasingly hard time understanding it.
Most useful applications tend to grow larger than what the typical human can fit in their active memory, however, so you need a way to break up your code so you can load a relevant piece into your brain at once to understand and debug it. Usually that takes the form of encapsulation, coupling, cohesion, and other common object-oriented vernacular.
But what about just a pure function? A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That has a number of advantages, such as being _idempotent_ (calling it a second time with the same values is guaranteed to return the same result) and _referential transparency_ (a function and its parameters is synonymous with its result, which can let you optimize the function away entirely in some cases). But perhaps the biggest advantage of pure functions doesn't have a fancy name: It's really easy to fit in your brain.
If you're trying to understand a given piece of code, a pure function will always be the easiest to understand because there is no need for context. There are some explicit inputs, which you can see; There is an explicit output, which you can see; And there's nothing else to care about. Reading the function (and the data definition of its parameters) is all you need to think about, because there's nothing else to think about. Every function becomes a natural "small enough to fit in your brain" unit. A pure function will often call other pure functions, but that only creates slightly more overhead. Once you know a function is pure, it's easy to mentally "unload" as a black box that you can deal with separately. Go read that code first, then put it out of your brain and focus on the next function. Moreover, functional programming-style code tends to favor function composition over direct function calls. That is, rather than function A calling function B which calls function C, you call function A and pass its return value to function B, then pass B's return value to C. That whole process can be wrapped up into another function if necessary. That makes it even easier to focus on only one function at a time, which is virtually guaranteed to fit in your brain at once. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 6 July 2020 - 5:13pm * Read more about Byte-sized functional programming: Pure functions encourage small code A Major Event in PHP ebook now available In early 2019, the PHP Framework Interoperability Group (PHP-FIG) released PSR-14, the Event Dispatcher specification. At the time I posted a long series of blog posts detailing PSR-14 in all its glory. After discussing with a few other FIG folks, I've decided to release that blog series as a small ebook . Mainly that is to provide an easy single-point-of-reference for those who want to really understand PSR-14. Also, it serves as a simple fundraiser. The book itself is available completely free, as the original blog posts were. However, you can also purchase a copy if you want to help support my work on the Framework Interoperability Group and Open Sourcing Mental Illness . OSMI is a non-profit organization that works to raise awareness of and research information about mental health challenges in the tech community. 50% of all royalties for this book are automatically donated to OSMI to support their vital work.
If you just want to grab a copy for free, go for it. If you are able to, though, I would encourage you to pay what you're comfortable with to support both my Open Source efforts and OSMI. (This announcement is a bit late due to a publishing goof on my blog. If you're seeing this for the second time, my apologies.) Larry 6 July 2020 - 5:12pm * Read more about A Major Event in PHP ebook now available Announcing "Thinking Functionally in PHP" Calling all PHP developers! Want to wrap your brain around functional programming? Are you sick of reading tutorials about this magic "monad" thing but having no idea how to read Haskell? Then this book is for you.
Announcing Thinking Functionally in PHP , by yours truly,
available now on LeanPub. _Continue reading this post on PeakD _
Larry 20 May 2020 - 9:16am * Read more about Announcing "Thinking Functionally in PHP" Type Matching in PHP One of the nice features of Rust is the match keyword. match is similar to `switch`, but with two key differences: * It requires an exhaustive match, that is, every possible value must be accounted for or a default must be provided. * match is an expression, meaning you can assign the return value of one of its branches to a variable. That makes match extremely useful for ensuring you handle all possibilities of an enumerated type, say, if using an Optional or Either for error handling. Which... is something I've been experimenting with in PHP. It's hard to make a PHP equivalent of match that forces an exhaustive match, as PHP lacks enumerated types. However, emulating an expression match turns out to be pretty easy in PHP 7.4, and kind of pretty, too. Larry 2 February 2020 - 1:46pm * Read more about Type Matching in PHP PAGINATION
* Page 1
* Next page ››
Subscribe to
SEARCH
Search
FREE SOFTWARE FOUNDATION Support software freedom! Get the monthly newsletter — ___the Free Software Supporter_ Username
Password
* Reset your password FOOTER MENU
Show — Footer menu Hide — Footer menu * Contact
function A($in)
{
// ...
return $out;
}
function
B($in)
{
// ...
return $out;
}
function
C($in)
{
// ...
return $out;
}
function
doit($in) {
$out = A($in);
$out = B($out);
$out = C($out);
return $out;
}
?>
Now `A()`, `B()`, and `C()` are all easier to read, understand, and test, and we can more easily add a step B2 or D if we want. So powerful is this concept that many languages have a native operator for piping functions together like that. PHP doesn't, yet, but it's straightforward enough to do in user space anyway. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP.
Larry 15 August 2020 - 10:07am * Read more about Byte-sized functional programming: Composition over inheritance for functions, too Byte-sized functional programming: Filter first Often when working with a list, we only want to work with a subset of a list that meets some criteria. All non-zero values, for example, or all users that have a given role. The procedural way to do that is to stick an if statement inside a foreach loop:foreach ($list as $value) { If (!meets_criteria($value)) { continue;
}
// ...
}
?>
That mixes up the filtering with the iteration, though. It also doesn't work if we're using `array_map()`. Instead, we can make stripping down the list a separate operation called "filter." PHP offers the array_filter() function for that purpose.
$criteria = fn(User $user): bool => $user->hasRole('moderator'); $filtered = array_filter($users, $criteria); ?>
Now we can work with the `$filtered` list, which has only the values we want. That could be a simple foreach loop, or, better, it's now ideally suited for use with array_map(). ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 3 August 2020 - 1:47pm * Read more about Byte-sized functional programming: Filter first Byte-sized functional programming: Mapping out your data Procedural code tends to think in terms of writing out steps, and so the usual way to work with a list is to iterate it using a `for` or `foreach` loop.
Functional code tends to think in terms of the relationships and transformations between data, where those relationships and transformations are defined as functions. That means the natural way to work with a list is to define a function that is the relationship between one list and another. The most common way of doing that is with a "map," which in PHP usually means the `array_map()` function. With `array_map()`, you give it an array and a function. You get back the result of applying that function to every element in the array, individually. Like so: $arr = ;
$fun = fn($x) => $x * 2; $result = array_map($fn, $arr); ?>
The advantages of that over a `foreach` loop are: * The function is a separate operation that can be as complex as you want.
* If it's more than a line or two, make it its own function or method somewhere and test it in isolation. * If it's trivial, you can simply inline it. * It's clear, visually, that every element's transformation is independent of every other's, because that's how `array_map()` works. A `foreach` loop may maintain state from one iteration to the next, but `array_map()` does not. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 27 July 2020 - 3:42pm * Read more about Byte-sized functional programming: Mapping out your data
Byte-sized functional programming: Immutable variables are easier to understand
An immutable variable is one whose value doesn't change after it has been first set. PHP doesn't natively support that, but we can write our classes in such a way to simulate it. For example, rather than this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function moveUp(int $by): void {
$this->y += $by;
}
}
?>
We can write this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function
moveUp(int $by): Point {
$new = clone ($this); $new->y += $by;
return $new;
}
}
?>
In the second version, once a given `Point` object is created it will never change. We can safely pass it to another function or method and be guaranteed that its value won't change without us knowing. Instead, any attempt to change it results in a new object, with its own identity, representing the new point in space. (In practice there would be other methods here as well, but we're focusing on just the mutation part.)
Code that uses immutable variables is easier to think about, because we don't have to worry about "does passing this object to this function change it?" We know it doesn't. Once we know something about an object we can guarantee that fact doesn't change. That can make a lot of subtle bugs impossible, which means we don't have to spend time looking for or correcting them. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 21 July 2020 - 7:30am * Read more about Byte-sized functional programming: Immutable variables are easier to understand Advice for new speakers Someone messaged me recently to say he had just been selected for his first-ever conference talk, and since the talks of mine he'd seen in the past were so inspiring he wanted to know if I had any advice for new speakers. Since flattery will often get you somewhere, I offered the following advice. I figure it's generic enough that I should share it more widely. :-)
_Continue reading this post on PeakD ._
Larry 18 July 2020 - 5:18pm * Read more about Advice for new speakers Byte-sized functional programming: Pure functions make testing easy When testing stateful code, you have to first set up all of the explicit and implicit state that a given piece of code depends on. If the object you're testing has many dependencies that could be complex, or references global variables, or calls many other routines with their own dependencies, you may be looking at a lot of complex setup that makes testing hard to do effectively. Pure functions greatly reduce that problem. A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That means there is, by definition, only one place to inject dependencies: the call itself. And there's only one effect to validate: the return value of the function. No need to call a function and check what the effect was on some other value or piece of code: by definition, the only effect is the return value. If one of the parameters you pass in is itself complex, that may make the test complex. But if the parameter is complex, that can serve as an impetus to simplify it. Rather than passing in an object, for example, just pass in a single function. If that function in practice has more complexity behind it, fine, but it makes passing a mock function trivial. Just... make a new (anonymous) function for the one test.
When your code has fewer sneaky interactions, there's less effort involved in testing as well as fewer things to test. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 14 July 2020 - 9:28am * Read more about Byte-sized functional programming: Pure functions make testing easy
Byte-sized functional programming: Pure functions encourage small code One of the many pieces of advice for a long-term sustainable code base is to keep code small. The larger a code base is, the more effort it takes to understand all the moving parts. Your brain can only keep so much mental model of your code in it at once, and if the code you're looking at is too large then what you can fit in your own "active memory" at once then you will have an increasingly hard time understanding it.
Most useful applications tend to grow larger than what the typical human can fit in their active memory, however, so you need a way to break up your code so you can load a relevant piece into your brain at once to understand and debug it. Usually that takes the form of encapsulation, coupling, cohesion, and other common object-oriented vernacular.
But what about just a pure function? A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That has a number of advantages, such as being _idempotent_ (calling it a second time with the same values is guaranteed to return the same result) and _referential transparency_ (a function and its parameters is synonymous with its result, which can let you optimize the function away entirely in some cases). But perhaps the biggest advantage of pure functions doesn't have a fancy name: It's really easy to fit in your brain.
If you're trying to understand a given piece of code, a pure function will always be the easiest to understand because there is no need for context. There are some explicit inputs, which you can see; There is an explicit output, which you can see; And there's nothing else to care about. Reading the function (and the data definition of its parameters) is all you need to think about, because there's nothing else to think about. Every function becomes a natural "small enough to fit in your brain" unit. A pure function will often call other pure functions, but that only creates slightly more overhead. Once you know a function is pure, it's easy to mentally "unload" as a black box that you can deal with separately. Go read that code first, then put it out of your brain and focus on the next function. Moreover, functional programming-style code tends to favor function composition over direct function calls. That is, rather than function A calling function B which calls function C, you call function A and pass its return value to function B, then pass B's return value to C. That whole process can be wrapped up into another function if necessary. That makes it even easier to focus on only one function at a time, which is virtually guaranteed to fit in your brain at once. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 6 July 2020 - 5:13pm * Read more about Byte-sized functional programming: Pure functions encourage small code A Major Event in PHP ebook now available In early 2019, the PHP Framework Interoperability Group (PHP-FIG) released PSR-14, the Event Dispatcher specification. At the time I posted a long series of blog posts detailing PSR-14 in all its glory. After discussing with a few other FIG folks, I've decided to release that blog series as a small ebook . Mainly that is to provide an easy single-point-of-reference for those who want to really understand PSR-14. Also, it serves as a simple fundraiser. The book itself is available completely free, as the original blog posts were. However, you can also purchase a copy if you want to help support my work on the Framework Interoperability Group and Open Sourcing Mental Illness . OSMI is a non-profit organization that works to raise awareness of and research information about mental health challenges in the tech community. 50% of all royalties for this book are automatically donated to OSMI to support their vital work.
If you just want to grab a copy for free, go for it. If you are able to, though, I would encourage you to pay what you're comfortable with to support both my Open Source efforts and OSMI. (This announcement is a bit late due to a publishing goof on my blog. If you're seeing this for the second time, my apologies.) Larry 6 July 2020 - 5:12pm * Read more about A Major Event in PHP ebook now available Announcing "Thinking Functionally in PHP" Calling all PHP developers! Want to wrap your brain around functional programming? Are you sick of reading tutorials about this magic "monad" thing but having no idea how to read Haskell? Then this book is for you.
Announcing Thinking Functionally in PHP , by yours truly,
available now on LeanPub. _Continue reading this post on PeakD _
Larry 20 May 2020 - 9:16am * Read more about Announcing "Thinking Functionally in PHP" Type Matching in PHP One of the nice features of Rust is the match keyword. match is similar to `switch`, but with two key differences: * It requires an exhaustive match, that is, every possible value must be accounted for or a default must be provided. * match is an expression, meaning you can assign the return value of one of its branches to a variable. That makes match extremely useful for ensuring you handle all possibilities of an enumerated type, say, if using an Optional or Either for error handling. Which... is something I've been experimenting with in PHP. It's hard to make a PHP equivalent of match that forces an exhaustive match, as PHP lacks enumerated types. However, emulating an expression match turns out to be pretty easy in PHP 7.4, and kind of pretty, too. Larry 2 February 2020 - 1:46pm * Read more about Type Matching in PHP PAGINATION
* Page 1
* Next page ››
Subscribe to
SEARCH
Search
FREE SOFTWARE FOUNDATION Support software freedom! Get the monthly newsletter — ___the Free Software Supporter_ Username
Password
* Reset your password FOOTER MENU
Show — Footer menu Hide — Footer menu * Contact
?>
Now we can work with the `$filtered` list, which has only the values we want. That could be a simple foreach loop, or, better, it's now ideally suited for use with array_map(). ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 3 August 2020 - 1:47pm * Read more about Byte-sized functional programming: Filter first Byte-sized functional programming: Mapping out your data Procedural code tends to think in terms of writing out steps, and so the usual way to work with a list is to iterate it using a `for` or `foreach` loop.
Functional code tends to think in terms of the relationships and transformations between data, where those relationships and transformations are defined as functions. That means the natural way to work with a list is to define a function that is the relationship between one list and another. The most common way of doing that is with a "map," which in PHP usually means the `array_map()` function. With `array_map()`, you give it an array and a function. You get back the result of applying that function to every element in the array, individually. Like so: $arr = ;
$fun = fn($x) => $x * 2; $result = array_map($fn, $arr); ?>
The advantages of that over a `foreach` loop are: * The function is a separate operation that can be as complex as you want.
* If it's more than a line or two, make it its own function or method somewhere and test it in isolation. * If it's trivial, you can simply inline it. * It's clear, visually, that every element's transformation is independent of every other's, because that's how `array_map()` works. A `foreach` loop may maintain state from one iteration to the next, but `array_map()` does not. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 27 July 2020 - 3:42pm * Read more about Byte-sized functional programming: Mapping out your data
Byte-sized functional programming: Immutable variables are easier to understand
An immutable variable is one whose value doesn't change after it has been first set. PHP doesn't natively support that, but we can write our classes in such a way to simulate it. For example, rather than this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function moveUp(int $by): void {
$this->y += $by;
}
}
?>
We can write this:
class Point
{
private int $x;
private int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function
moveUp(int $by): Point {
$new = clone ($this); $new->y += $by;
return $new;
}
}
?>
In the second version, once a given `Point` object is created it will never change. We can safely pass it to another function or method and be guaranteed that its value won't change without us knowing. Instead, any attempt to change it results in a new object, with its own identity, representing the new point in space. (In practice there would be other methods here as well, but we're focusing on just the mutation part.)
Code that uses immutable variables is easier to think about, because we don't have to worry about "does passing this object to this function change it?" We know it doesn't. Once we know something about an object we can guarantee that fact doesn't change. That can make a lot of subtle bugs impossible, which means we don't have to spend time looking for or correcting them. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 21 July 2020 - 7:30am * Read more about Byte-sized functional programming: Immutable variables are easier to understand Advice for new speakers Someone messaged me recently to say he had just been selected for his first-ever conference talk, and since the talks of mine he'd seen in the past were so inspiring he wanted to know if I had any advice for new speakers. Since flattery will often get you somewhere, I offered the following advice. I figure it's generic enough that I should share it more widely. :-)
_Continue reading this post on PeakD ._
Larry 18 July 2020 - 5:18pm * Read more about Advice for new speakers Byte-sized functional programming: Pure functions make testing easy When testing stateful code, you have to first set up all of the explicit and implicit state that a given piece of code depends on. If the object you're testing has many dependencies that could be complex, or references global variables, or calls many other routines with their own dependencies, you may be looking at a lot of complex setup that makes testing hard to do effectively. Pure functions greatly reduce that problem. A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That means there is, by definition, only one place to inject dependencies: the call itself. And there's only one effect to validate: the return value of the function. No need to call a function and check what the effect was on some other value or piece of code: by definition, the only effect is the return value. If one of the parameters you pass in is itself complex, that may make the test complex. But if the parameter is complex, that can serve as an impetus to simplify it. Rather than passing in an object, for example, just pass in a single function. If that function in practice has more complexity behind it, fine, but it makes passing a mock function trivial. Just... make a new (anonymous) function for the one test.
When your code has fewer sneaky interactions, there's less effort involved in testing as well as fewer things to test. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 14 July 2020 - 9:28am * Read more about Byte-sized functional programming: Pure functions make testing easy
Byte-sized functional programming: Pure functions encourage small code One of the many pieces of advice for a long-term sustainable code base is to keep code small. The larger a code base is, the more effort it takes to understand all the moving parts. Your brain can only keep so much mental model of your code in it at once, and if the code you're looking at is too large then what you can fit in your own "active memory" at once then you will have an increasingly hard time understanding it.
Most useful applications tend to grow larger than what the typical human can fit in their active memory, however, so you need a way to break up your code so you can load a relevant piece into your brain at once to understand and debug it. Usually that takes the form of encapsulation, coupling, cohesion, and other common object-oriented vernacular.
But what about just a pure function? A _pure function_ is a function that: * has no inputs other than those explicitly specified; * has no effect on any value other than the value it returns. That has a number of advantages, such as being _idempotent_ (calling it a second time with the same values is guaranteed to return the same result) and _referential transparency_ (a function and its parameters is synonymous with its result, which can let you optimize the function away entirely in some cases). But perhaps the biggest advantage of pure functions doesn't have a fancy name: It's really easy to fit in your brain.
If you're trying to understand a given piece of code, a pure function will always be the easiest to understand because there is no need for context. There are some explicit inputs, which you can see; There is an explicit output, which you can see; And there's nothing else to care about. Reading the function (and the data definition of its parameters) is all you need to think about, because there's nothing else to think about. Every function becomes a natural "small enough to fit in your brain" unit. A pure function will often call other pure functions, but that only creates slightly more overhead. Once you know a function is pure, it's easy to mentally "unload" as a black box that you can deal with separately. Go read that code first, then put it out of your brain and focus on the next function. Moreover, functional programming-style code tends to favor function composition over direct function calls. That is, rather than function A calling function B which calls function C, you call function A and pass its return value to function B, then pass B's return value to C. That whole process can be wrapped up into another function if necessary. That makes it even easier to focus on only one function at a time, which is virtually guaranteed to fit in your brain at once. ------------------------- Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP .
Larry 6 July 2020 - 5:13pm * Read more about Byte-sized functional programming: Pure functions encourage small code A Major Event in PHP ebook now available In early 2019, the PHP Framework Interoperability Group (PHP-FIG) released PSR-14, the Event Dispatcher specification. At the time I posted a long series of blog posts detailing PSR-14 in all its glory. After discussing with a few other FIG folks, I've decided to release that blog series as a small ebook . Mainly that is to provide an easy single-point-of-reference for those who want to really understand PSR-14. Also, it serves as a simple fundraiser. The book itself is available completely free, as the original blog posts were. However, you can also purchase a copy if you want to help support my work on the Framework Interoperability Group and Open Sourcing Mental Illness . OSMI is a non-profit organization that works to raise awareness of and research information about mental health challenges in the tech community. 50% of all royalties for this book are automatically donated to OSMI to support their vital work.
If you just want to grab a copy for free, go for it. If you are able to, though, I would encourage you to pay what you're comfortable with to support both my Open Source efforts and OSMI. (This announcement is a bit late due to a publishing goof on my blog. If you're seeing this for the second time, my apologies.) Larry 6 July 2020 - 5:12pm * Read more about A Major Event in PHP ebook now available Announcing "Thinking Functionally in PHP" Calling all PHP developers! Want to wrap your brain around functional programming? Are you sick of reading tutorials about this magic "monad" thing but having no idea how to read Haskell? Then this book is for you.
Announcing Thinking Functionally in PHP , by yours truly,
available now on LeanPub. _Continue reading this post on PeakD _
Larry 20 May 2020 - 9:16am * Read more about Announcing "Thinking Functionally in PHP" Type Matching in PHP One of the nice features of Rust is the match keyword. match is similar to `switch`, but with two key differences: * It requires an exhaustive match, that is, every possible value must be accounted for or a default must be provided. * match is an expression, meaning you can assign the return value of one of its branches to a variable. That makes match extremely useful for ensuring you handle all possibilities of an enumerated type, say, if using an Optional or Either for error handling. Which... is something I've been experimenting with in PHP. It's hard to make a PHP equivalent of match that forces an exhaustive match, as PHP lacks enumerated types. However, emulating an expression match turns out to be pretty easy in PHP 7.4, and kind of pretty, too. Larry 2 February 2020 - 1:46pm * Read more about Type Matching in PHP PAGINATION
* Page 1
* Next page ››
Subscribe to
SEARCH
Search
FREE SOFTWARE FOUNDATION Support software freedom! Get the monthly newsletter — ___the Free Software Supporter_ Username
Password
* Reset your password FOOTER MENU
Show — Footer menu Hide — Footer menu * Contact
Details
Copyright © 2024 ArchiveBay.com. All rights reserved. Terms of Use | Privacy Policy | DMCA | 2021 | Feedback | Advertising | RSS 2.0