From a1b17b853ef719d4b04ab7fee6189798339ea505 Mon Sep 17 00:00:00 2001 From: dys-bigwig <45558889+dys-bigwig@users.noreply.github.com> Date: Wed, 10 Jul 2019 20:38:50 +0100 Subject: [PATCH 1/4] added update functions Hope that's alright. Couldn't decide which names would be best for the dummy functions required for ref and set-ref in the define/generic forms. Hopefully there won't be alternate versions of ref or set-ref in the future that would be more deserving of said names ;) --- collections-lib/data/collection/indexable.rkt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/collections-lib/data/collection/indexable.rkt b/collections-lib/data/collection/indexable.rkt index 9c17b50..e75ac3f 100644 --- a/collections-lib/data/collection/indexable.rkt +++ b/collections-lib/data/collection/indexable.rkt @@ -7,15 +7,26 @@ (provide gen:indexable indexable? indexable/c - ref set-ref) + ref set-ref update-ref) (define-generics indexable (ref indexable . _) (set-ref indexable . _) + (update-ref indexable . _) #:defaults ([hash? (define ref hash-ref) - (define set-ref hash-set)] + (define set-ref hash-set) + (define update-ref hash-update)] [dict? (define ref dict-ref) - (define set-ref dict-set)] + (define set-ref dict-set) + (define update-ref dict-update)] [sequence? (define ref nth) - (define set-ref set-nth)])) + (define set-ref set-nth) + (define update-ref update-nth)] + ) + #:fallbacks + [(define/generic ref* ref) + (define/generic set-ref* set-ref) + (define (update-ref indexable index proc) + (let ([old (ref* indexable index)]) + (set-ref* indexable index (proc old))))]) From 3147c5e37644676bc25042301cea8326454656d2 Mon Sep 17 00:00:00 2001 From: dys-bigwig <45558889+dys-bigwig@users.noreply.github.com> Date: Fri, 12 Jul 2019 17:12:19 +0100 Subject: [PATCH 2/4] changed names to be more consistent --- collections-lib/data/collection/indexable.rkt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/collections-lib/data/collection/indexable.rkt b/collections-lib/data/collection/indexable.rkt index e75ac3f..40150e2 100644 --- a/collections-lib/data/collection/indexable.rkt +++ b/collections-lib/data/collection/indexable.rkt @@ -22,11 +22,10 @@ (define update-ref dict-update)] [sequence? (define ref nth) (define set-ref set-nth) - (define update-ref update-nth)] - ) + (define update-ref update-nth)]) #:fallbacks - [(define/generic ref* ref) - (define/generic set-ref* set-ref) + [(define/generic -ref ref) + (define/generic -set-ref set-ref) (define (update-ref indexable index proc) - (let ([old (ref* indexable index)]) - (set-ref* indexable index (proc old))))]) + (let ([old (-ref indexable index)]) + (-set-ref indexable index (proc old))))]) From 53bd868dfef8af7ed0e2eb3990f2bf4bbac19e0f Mon Sep 17 00:00:00 2001 From: dys-bigwig <45558889+dys-bigwig@users.noreply.github.com> Date: Fri, 12 Jul 2019 17:13:26 +0100 Subject: [PATCH 3/4] added tests for indexables --- .../tests/data/collection/indexable.rkt | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 collections-test/tests/data/collection/indexable.rkt diff --git a/collections-test/tests/data/collection/indexable.rkt b/collections-test/tests/data/collection/indexable.rkt new file mode 100644 index 0000000..3a01016 --- /dev/null +++ b/collections-test/tests/data/collection/indexable.rkt @@ -0,0 +1,70 @@ +#lang racket/base + +(require rackunit + data/collection + racket/set + racket/stream + racket/generic + racket/function) + +(struct my-list (items) + #:transparent + #:methods gen:indexable + [(define/generic -ref ref) + (define/generic -set-ref set-ref) + (define (ref indexable . args) + (define pos (car args)) + (list-ref (my-list-items indexable) + pos)) + (define (set-ref indexable . args) + (define pos (car args)) + (define item (cadr args)) + (define ls (my-list-items indexable)) + (my-list (-set-ref ls pos item)))]) + +(test-case "ref" + "list" + (check-equal? (ref '(a b) 1) 'b) + "vector" + (check-equal? (ref #(a b c) 1) 'b) + "hash" + (check-equal? (ref (hash 'a 'b) 'a) 'b) + "set" + (check-equal? (ref (set 'a 'b) 1) 'b) + "alist" + (check-equal? (ref '((a . b)) 'a) 'b) + "custom data type my-list" + (check-equal? (ref (my-list '(a b)) 1) 'b)) + +(test-case "set-ref" + "list" + (check-equal? (set-ref '(a b) 0 'c) + '(c b)) + "hash" + (check-equal? (set-ref (hash 'a 'b) 'a 'c) + (hash 'a 'c)) + "set" + (check-equal? (stream->list (set-ref (set 0 1) 0 3)) + (stream->list (stream 3 0))) + "alist" + (check-equal? (set-ref '((a . b)) 'a 'c) + '((a . c))) + "custom data type my-list" + (check-equal? (set-ref (my-list '(a b)) 0 'c) + (my-list '(c b)))) + +(test-case "update-ref" + "list" + (check-equal? (update-ref '(a b) 0 identity) + '(a b)) + "hash" + (check-equal? (update-ref (hash 'a 'b) 'a identity) + (hash 'a (identity 'b))) + "set" + (check-equal? (stream->list (update-ref (set 'a 'b) 0 identity)) + (stream->list (stream 'a 'b))) + "alist" + (check-equal? (update-ref '((a . b)) 'a identity) + `((a . ,(identity 'b)))) + (check-equal? (update-ref (my-list '(a b)) 1 identity) + (my-list `(a ,(identity 'b))))) From 39d75a3a5099110108de4f50ae4c817cc9c68029 Mon Sep 17 00:00:00 2001 From: dys-bigwig <45558889+dys-bigwig@users.noreply.github.com> Date: Fri, 12 Jul 2019 18:39:55 +0100 Subject: [PATCH 4/4] removed tests for set data structures --- collections-test/tests/data/collection/indexable.rkt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/collections-test/tests/data/collection/indexable.rkt b/collections-test/tests/data/collection/indexable.rkt index 3a01016..8034033 100644 --- a/collections-test/tests/data/collection/indexable.rkt +++ b/collections-test/tests/data/collection/indexable.rkt @@ -2,7 +2,6 @@ (require rackunit data/collection - racket/set racket/stream racket/generic racket/function) @@ -29,8 +28,6 @@ (check-equal? (ref #(a b c) 1) 'b) "hash" (check-equal? (ref (hash 'a 'b) 'a) 'b) - "set" - (check-equal? (ref (set 'a 'b) 1) 'b) "alist" (check-equal? (ref '((a . b)) 'a) 'b) "custom data type my-list" @@ -43,9 +40,6 @@ "hash" (check-equal? (set-ref (hash 'a 'b) 'a 'c) (hash 'a 'c)) - "set" - (check-equal? (stream->list (set-ref (set 0 1) 0 3)) - (stream->list (stream 3 0))) "alist" (check-equal? (set-ref '((a . b)) 'a 'c) '((a . c))) @@ -60,9 +54,6 @@ "hash" (check-equal? (update-ref (hash 'a 'b) 'a identity) (hash 'a (identity 'b))) - "set" - (check-equal? (stream->list (update-ref (set 'a 'b) 0 identity)) - (stream->list (stream 'a 'b))) "alist" (check-equal? (update-ref '((a . b)) 'a identity) `((a . ,(identity 'b))))