Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # REFERENTIAL TRANSPARENCY
- An expression is referentially transparent if it can be replaced with its corresponding value without changing the program's behavior. Therefore evaluating a referentially transparent function is pure, it always gives the same output for the same arguments
- # GOOD FUNCTIONS
- * Allow us to avoid complexity by abstracting details
- * Do just one thing (one function per step)
- * Don't have side effects (purity)
- * Always return the same kind of thing
- * Take few arguments (maybe up to 2, no cheating with a hash)
- * If there are more, there may be too many responsibilities assigned to this function
- # HIGHER ORDER FUNCTIONS
- Functions that take other functions as arguments
- Perl: map, grep
- PHP: array_filter, array_map, array_reduce
- JS: Array.prototype.map, Array.prototype.filter, Array.prototype.reduce
- # MAP
- my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
- my @double = map { $_ * 2} @list; ## 2, 4, 6, 8, 10, ..., 20
- my @utf8 = map { encode 'UTF-8' => $_ } qw/foo bar bäz/
- # FILTER (grep)
- my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
- my @odds = grep { $_ ^ % 2 } @list; ## 1, 3, 5, 7, 9
- my @evens = grep { not $_ % 2 } @list; ## 2, 4, 6, 8, 10
- # SORT
- use List::Util qw(shuffle);
- my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
- @list = shuffle @list;
- my @lo2hi = sort { $a cmp $b } @list;
- my @hi2lo = sort { $b cmp $a } @list;
- # REDUCE
- use List::Util qw(reduce);
- my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
- my $total = reduce { $a + $b } @list;
- # CLOSURE
- use strict;
- use warnings;
- use Mojo::UserAgent;
- my $ua = Mojo::UserAgent->new;
- my $ba_tx_maker = basic_auth_tx_maker({Authorization => 'Basic ' . b64_encode("Foo:Bar", "")});
- my $tx = $ba_tx_maker->('GET', 'http://auth.sonic.test');
- sub basic_auth_tx_maker {
- my $headers = shift;
- return sub {
- my ($method, $url, $body) = @_;
- my $tx = Mojo::UserAgent->new;
- $tx->req->url->parse($url);
- $tx->req->headers->from_hash($headers);
- $tx->req->method($method);
- $tx->req->body($body);
- return $gx;
- }
- }
- # COMPOSITION
- well, maybe a little math
- f(x) = x^2
- g(x) = 2x
- compose g(x) with f(x): f(g(x)) = f(2x) = (2x)^2 = 4x^2
- evaluate f(g(3)): f(g(3)) = f(2*3) = f(6) = 6^2 = 36
- evaluate h(3): 4*3^2 = 4*9 = 36
- this one in JS 'cause it's what I could steal
- /*
- All in one
- */
- function getOnlyIndie(albums) {
- let filtered = [];
- for (let album of albums) {
- if(album.genre === 'indie') {
- filtered.push(album);
- }
- }
- filtered.sort((album1, album2) => {
- if(album1.artist === album2.artist) return 0;
- return album1.artist > album2.artist ? 1 : -1;
- });
- return filtered;
- }
- /*
- One function per step
- */
- const byArtistAsc = (album1, album2) => {
- if(album1.artist === album2.artist) {
- return 0;
- }
- return album1.artist > album2.artist ? 1 : -1;
- };
- const getOnlyIndie = album => album.genre === 'indie';
- albums.filter(getOnlyIndie).sort(byArtistAsc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement