Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2015/05/01

Explaining the Schwartzian Transform

  1. #!/usr/bin/env perl  
  2.   
  3. # what is a schwartzian transform?  
  4.   
  5. # http://en.wikipedia.org/wiki/Schwartzian_transform  
  6.   
  7. use feature qw{ say } ;   
  8.   
  9. # notice how all of these are not in any order?  
  10.   
  11. my @tedx_feeds = qw{  
  12.     tedx  
  13.     tedxboston  
  14.     tedxindianapolis  
  15.     tedxiu  
  16.     tedxpurdueu  
  17.     tedxlafayette  
  18.     tedxohiostateu  
  19.     tedxpsu  
  20.     tedxuiowa  
  21.     } ;  
  22.   
  23. # we'll read this backwards.   
  24. my @sorted =   
  25.     map { $_->[0] }   
  26.     # ... and then we just use the words, jettisoning  
  27.     # the lengths and creating the final array.  
  28.   
  29.     sort { $a->[1] <=> $b->[1] }  
  30.     # ... which we sort numerically by the length  
  31.       
  32.     map { [ $_ , length $_ ] }   
  33.     # ... becomes an array of arrays, being all  
  34.     # the words and their lengths, starting   
  35.     # with [ 'tedx' , 4 ] ...  
  36.   
  37.     @tedx_feeds ; # the array in unsorted order ...  
  38.   
  39. # Start here and go up <-----------------<<<<<  
  40.   
  41. say join "\n" , @sorted ;  
  42.   
  43. # output looks like:  
  44. # tedx  
  45. # tedxiu  
  46. # tedxpsu  
  47. # tedxuiowa  
  48. # tedxboston  
  49. # tedxpurdueu  
  50. # tedxlafayette  
  51. # tedxohiostateu  
  52. # tedxindianapolis  

It took me quite a long time to get the Transform, and the use of map and grep. But they aren't hard. Once you get them, you'll use them everywhere. Maybe too much....

No comments:

Post a Comment