Advertisement
Guest User

Untitled

a guest
May 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. # REFERENTIAL TRANSPARENCY
  2. 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
  3.  
  4.  
  5. # GOOD FUNCTIONS
  6. * Allow us to avoid complexity by abstracting details
  7. * Do just one thing (one function per step)
  8. * Don't have side effects (purity)
  9. * Always return the same kind of thing
  10. * Take few arguments (maybe up to 2, no cheating with a hash)
  11. * If there are more, there may be too many responsibilities assigned to this function
  12.  
  13.  
  14. # HIGHER ORDER FUNCTIONS
  15. Functions that take other functions as arguments
  16. Perl: map, grep
  17. PHP: array_filter, array_map, array_reduce
  18. JS: Array.prototype.map, Array.prototype.filter, Array.prototype.reduce
  19.  
  20. # MAP
  21.  
  22. my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
  23. my @double = map { $_ * 2} @list; ## 2, 4, 6, 8, 10, ..., 20
  24. my @utf8 = map { encode 'UTF-8' => $_ } qw/foo bar bäz/
  25.  
  26.  
  27. # FILTER (grep)
  28.  
  29. my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
  30. my @odds = grep { $_ ^ % 2 } @list; ## 1, 3, 5, 7, 9
  31. my @evens = grep { not $_ % 2 } @list; ## 2, 4, 6, 8, 10
  32.  
  33. # SORT
  34.  
  35. use List::Util qw(shuffle);
  36. my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
  37. @list = shuffle @list;
  38.  
  39. my @lo2hi = sort { $a cmp $b } @list;
  40. my @hi2lo = sort { $b cmp $a } @list;
  41.  
  42.  
  43. # REDUCE
  44.  
  45. use List::Util qw(reduce);
  46. my @list = (1..10); ## 1, 2, 3, 4, 5, ..., 10
  47.  
  48. my $total = reduce { $a + $b } @list;
  49.  
  50.  
  51. # CLOSURE
  52. use strict;
  53. use warnings;
  54. use Mojo::UserAgent;
  55.  
  56. my $ua = Mojo::UserAgent->new;
  57. my $ba_tx_maker = basic_auth_tx_maker({Authorization => 'Basic ' . b64_encode("Foo:Bar", "")});
  58.  
  59. my $tx = $ba_tx_maker->('GET', 'http://auth.sonic.test');
  60.  
  61. sub basic_auth_tx_maker {
  62. my $headers = shift;
  63.  
  64. return sub {
  65. my ($method, $url, $body) = @_;
  66.  
  67. my $tx = Mojo::UserAgent->new;
  68. $tx->req->url->parse($url);
  69. $tx->req->headers->from_hash($headers);
  70.  
  71. $tx->req->method($method);
  72. $tx->req->body($body);
  73.  
  74. return $gx;
  75. }
  76. }
  77.  
  78.  
  79.  
  80. # COMPOSITION
  81. well, maybe a little math
  82.  
  83. f(x) = x^2
  84. g(x) = 2x
  85. compose g(x) with f(x): f(g(x)) = f(2x) = (2x)^2 = 4x^2
  86. evaluate f(g(3)): f(g(3)) = f(2*3) = f(6) = 6^2 = 36
  87. evaluate h(3): 4*3^2 = 4*9 = 36
  88.  
  89. this one in JS 'cause it's what I could steal
  90.  
  91. /*
  92. All in one
  93. */
  94. function getOnlyIndie(albums) {
  95. let filtered = [];
  96. for (let album of albums) {
  97. if(album.genre === 'indie') {
  98. filtered.push(album);
  99. }
  100. }
  101. filtered.sort((album1, album2) => {
  102. if(album1.artist === album2.artist) return 0;
  103. return album1.artist > album2.artist ? 1 : -1;
  104. });
  105. return filtered;
  106. }
  107.  
  108. /*
  109. One function per step
  110. */
  111.  
  112. const byArtistAsc = (album1, album2) => {
  113. if(album1.artist === album2.artist) {
  114. return 0;
  115. }
  116. return album1.artist > album2.artist ? 1 : -1;
  117. };
  118.  
  119. const getOnlyIndie = album => album.genre === 'indie';
  120.  
  121. albums.filter(getOnlyIndie).sort(byArtistAsc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement