From 53109568481ce082d7378c37a6e43b186be0ac3d Mon Sep 17 00:00:00 2001 From: Sam Caldwell Date: Wed, 15 May 2019 17:25:11 -0400 Subject: [PATCH] more hash functions --- racket/typed/examples/roles/flink.rkt | 12 +++++------- racket/typed/hash.rkt | 10 ++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/racket/typed/examples/roles/flink.rkt b/racket/typed/examples/roles/flink.rkt index b0292f1..6d44482 100644 --- a/racket/typed/examples/roles/flink.rkt +++ b/racket/typed/examples/roles/flink.rkt @@ -127,10 +127,10 @@ The JobManager then performs the job and, when finished, asserts (job-finished I (define (word-count-increment [h : WordCount] [word : String] -> WordCount) - (hash-update h - word - add1 - #;(λ x 0))) + (hash-update/failure h + word + add1 + (lambda () 0))) (define (count-new-words [word-count : WordCount] [words : (List String)] @@ -168,9 +168,7 @@ The JobManager then performs the job and, when finished, asserts (job-finished I (define wc (count-new-words (ann (hash) WordCount) (string->words data))) (set! state (finished wc))] [(reduce-work (bind left WordCount) (bind right WordCount)) - ;; TODO - this kind of hash-union - #;(define wc (hash-union left right #:combine +)) - (define wc left) + (define wc (hash-union/combine left right +)) (set! state (finished wc))]) (set! status IDLE)] [#t diff --git a/racket/typed/hash.rkt b/racket/typed/hash.rkt index 73f9f61..86ae599 100644 --- a/racket/typed/hash.rkt +++ b/racket/typed/hash.rkt @@ -7,6 +7,8 @@ hash-ref hash-has-key? hash-update + (typed-out [[hash-update/failure- : (∀ (K V) (→fn (Hash K V) K (→fn V V) (→fn V) (Hash K V)))] + hash-update/failure]) hash-remove hash-map hash-keys @@ -15,6 +17,8 @@ hash-count hash-empty? hash-union + (typed-out [[hash-union/combine- : (∀ (K V) (→fn (Hash K V) (Hash K V) (→fn V V V) (Hash K V)))] + hash-union/combine]) ) (require "core-types.rkt") @@ -67,3 +71,9 @@ [hash-union : (∀ (K1 V1 K2 V2) (→fn (Hash K1 V1) (Hash K2 V2) (Hash (U K1 K2) (U V1 V2))))] ;; TODO - hash-union with #:combine ) + +(define- (hash-update/failure- h k u err) + (#%app- hash-update- h k u err)) + +(define- (hash-union/combine- h1 h2 combine) + (#%app- hash-union- h1 h2 #:combine combine))