Skip to content

Commit cf13c23

Browse files
authored
Add consing-onto-static-list rule (#459)
Add `consing-onto-static-list` rule
1 parent b178702 commit cf13c23

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

default-recommendations/list-shortcuts-test.rkt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,26 @@ test: "list of only two contiguous selections not refactorable to take and drop"
246246
(define vs (list 'foo 'bar 'baz 'blah 'zorp 'zoog 'karp))
247247
(list (list-ref vs 2) (list-ref vs 3))
248248
------------------------------
249+
250+
251+
test: "consing onto static proper list expression can be simplified"
252+
- (cons 1 (list 2 3 4 5))
253+
- (cons 1 (cons 2 (cons 3 (list 4 5))))
254+
- (list* 1 2 3 (list 4 5))
255+
- (list* 1 2 3 4 5 (list))
256+
- (cons 1 (list* 2 3 4 (list 5)))
257+
- (list* 1 2 3 (cons 4 (list 5)))
258+
- (list* 1 2 (list* 3 4 (list 5)))
259+
- (list* (list 1 2 3 4 5))
260+
- (list 1 2 3 4 5)
261+
262+
263+
test: "consing onto static improper list expression can be simplified"
264+
- (cons 1 (list* 2 3 4 5))
265+
- (cons 1 (cons 2 (cons 3 (list* 4 5))))
266+
- (list* 1 2 3 (list* 4 5))
267+
- (cons 1 (list* 2 3 4 (list* 5)))
268+
- (list* 1 2 3 (cons 4 (list* 5)))
269+
- (list* 1 2 (list* 3 4 (list* 5)))
270+
- (list* (list* 1 2 3 4 5))
271+
- (list* 1 2 3 4 5)

default-recommendations/list-shortcuts.rkt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,43 @@
214214
(take target-list-with-prefix-dropped amount-to-take))
215215

216216

217+
(define-syntax-class static-list-expression
218+
#:literals (cons list list*)
219+
#:attributes ([element 1] maker improper-tail simplifiable?)
220+
221+
(pattern ((~and list maker) ~! element ...)
222+
#:attr improper-tail #false
223+
#:attr simplifiable? #false)
224+
225+
(pattern ((~and list* maker) element ... (~and improper-tail (~not :static-list-expression)))
226+
#:attr simplifiable? #false)
227+
228+
(pattern (cons ~! head tail:static-list-expression)
229+
#:attr [element 1] (cons #'head (attribute tail.element))
230+
#:attr maker (attribute tail.maker)
231+
#:attr improper-tail (attribute tail.improper-tail)
232+
#:attr simplifiable? #true)
233+
234+
(pattern (list* head ... tail:static-list-expression)
235+
#:attr [element 1] (append (attribute head) (attribute tail.element))
236+
#:attr maker (attribute tail.maker)
237+
#:attr improper-tail (attribute tail.improper-tail)
238+
#:attr simplifiable? #true))
239+
240+
241+
(define-refactoring-rule consing-onto-static-list
242+
#:description "This list-constructing expression can be simplified"
243+
#:literals (cons list)
244+
expr:static-list-expression
245+
#:when (attribute expr.simplifiable?)
246+
(expr.maker expr.element ... (~? expr.improper-tail)))
247+
248+
217249
(define-refactoring-suite list-shortcuts
218250
#:rules (append-single-list-to-single-list
219251
append*-and-map-to-append-map
220252
build-list-const-to-make-list
253+
consing-onto-static-list
221254
equal-null-list-to-null-predicate
222255
filter-to-remove*
223256
filter-to-remq*

0 commit comments

Comments
 (0)