Created
December 11, 2019 21:53
-
-
Save samdphillips/e140877368c94658b102a9e4cb2a1ee7 to your computer and use it in GitHub Desktop.
Normalize a bunch of records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require ...) | |
(define (fill-missing-fields [missing-value #f]) | |
(make-transducer | |
#:name 'fill-missing | |
#:starter | |
(lambda () | |
(variant #:consume (pair null empty-keyset))) | |
#:consumer | |
(lambda (state a-record) | |
(match-define (pair record-buf field-names) state) | |
(define next-state | |
(pair (cons a-record record-buf) | |
(reduce-all into-keyset | |
(sequence-append | |
(in-keyset field-names) | |
(in-keyset (record-keywords a-record)))))) | |
(variant #:consume next-state)) | |
#:half-closer | |
(lambda (state) | |
(match-define (pair records-reversed field-names) state) | |
(variant #:half-closed-emit | |
(pair (reverse records-reversed) | |
(ensure-fields #:missing missing-value | |
(in-keyset field-names))))) | |
#:half-closed-emitter | |
(lambda (state) | |
(match state | |
[(pair (list a-record) update-record) | |
(half-closed-emission (variant #:finish #f) | |
(update-record a-record))] | |
[(pair (cons a-record rest) update-record) | |
(half-closed-emission | |
(variant #:half-closed-emit (pair rest update-record)) | |
(update-record a-record))])) | |
#:emitter impossible | |
#:finisher void)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment