Changes between Initial Version and Version 1 of RefactoringSteps/ListComprehensions


Ignore:
Timestamp:
Feb 19, 2012, 10:00:20 PM (13 years ago)
Author:
manualwiki
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RefactoringSteps/ListComprehensions

    v1 v1  
     1= Transform list comprehension = 
     2 
     3Turn {{{lists:map}}}, {{{lists:foreach}}} and {{{lists:filter}}} calls into list comprehension syntax, or do it backwards. 
     4 
     5The two main cases of the transformation: 
     6 
     7 * {{{lists:map/2}}} or {{{lists:filter/2}}} or {{{lists:foreach/2}}} to list comprehension 
     8  * The transformation is applied only if {{{lists:map/2}}} or {{{lists:filter/2}}} or {{{lists:foreach/2}}} is selected 
     9  * If the first parameter is an explicit or an implicit fun expression the arity of the function must be equal to {{{2}}} 
     10  * If the first parameter is not a fun it is not checked what it really is 
     11  * It is not checked whether the second parameter really a list is 
     12 
     13 * list comprehension to {{{lists:map/2}}} and/or {{{lists:filter/2}}} 
     14  * The transformation does not supports list comprehensions that contain more than one list generator 
     15  * The result is a {{{lists:filter/2}}} or a {{{lists:map/2}}} or a composition of a {{{lists:filter/2}}} and a {{{lists:map/2}}} 
     16  * The transformation does not optimize according to unused variables 
     17  * It is not checked whether the generator really a list is 
     18 
     19 
     20Transform between two equivalent forms of list filtering {{{im}}}: 
     21 
     22{{{ 
     23f(Xs) -> 
     24   [X || X <- Xs, X < 5]. 
     25}}} 
     26 
     27 
     28{{{ 
     29f(Xs) -> 
     30   lists:filter( 
     31      fun(X) -> 
     32         X < 5 
     33      end, Xs). 
     34}}} 
     35 
     36== Side conditions == 
     37 
     38 * {{{lists:map/2}}} or {{{lists:filter/2}}} or {{{lists:foreach/2}}} to list comprehension 
     39  * The selection should contain an application of one of the relevant functions 
     40  * If the first parameter is an implicit function or an explicit function, the arity has to be {{{1}}}. 
     41 * list comprehension to {{{lists:map/2}}} and/or {{{lists:filter/2}}} 
     42  * The list comprehension can have only one generator; transforming more complex comprehensions would likely complicate the code even more. 
     43 
     44 
     45== Transformation steps and compensations == 
     46 
     47 * {{{lists:map/2}}} or {{{lists:foreach/2}}} to list comprehension  
     48  * If the first parameter is an implicit function, a new variable is created in the list comprehension and the implicit function is called with this variable. 
     49  * If the first parameter is an explicit function 
     50   * If it has only one clause without pattern matching and guards 
     51    * If it has only one body the body is copied into the head of the list comprehension. 
     52    * If it has more then one body, a {{{begin..end}}} expression is created. 
     53   * If it has more then one clause or one clause with pattern matching or with guards, a case expression is created in the head of the list comprehension. The application of the case is a new variable, and the branches of the case are the clauses of the function. 
     54  * If the first parameter is anything else, an application is created for it in the head of the list comprehension. 
     55 
     56 * {{{lists:filter/2}}} to list comprehension 
     57  * If the first parameter is an implicit function, a new variable is created in the list comprehension and the implicit function is called with this variable. 
     58  * If the first parameter is an explicit function 
     59   * If the function has one clause and the function has no pattern matching and no guards and the body returns a boolean value 
     60    * If the function has only one body then it is inserted into the list comprehension as filter(s). 
     61    * If the function has more then one body, then the bodies are inserted into the list comprehension in a {{{begin..end}}} structure. 
     62   * If the function has two clauses and the second body has no pattern matching and no guards and returns constantly false 
     63    * If the first clause has pattern matching, it is used in the generator. 
     64    * If the first clause has guards, they are are inserted into the list comprehension as filters. 
     65    * If the first clause has only one body which returns boolean value, it is inserted into the list comprehension as filter(s). 
     66    * If the first clause has more then one body, then the bodies are inserted into the list comprehension in a {{{begin..end}}} structure. 
     67   * If the function has more then two clauses it is transformed into a case structure. 
     68  * If the first parameter is everything else it is inserted into the list comprehension as an application. 
     69  
     70 * list comprehension to {{{lists:map/2}}} and/or {{{lists:filter/2}}} 
     71  * Building the {{{lists:filter/2}}} 
     72   * The filters are inserted a function expressions body. 
     73   * If the generator has pattern matching then the created function expression will have two clauses where the first clause has the pattern matching the second clause is constantly false. 
     74   * If no filters and no pattern matching occurs then no {{{lists:filter/2}}} is created. 
     75  * Building the {{{lists:map/2}}} 
     76   * If the head and the pattern of the list comprehension are equals, no {{{lists:map/2}}} is created. 
     77   * Else a fun expression is created with the list comprehensions pattern and the list comprehensions head as body.