I was a Scala programmer before. In Scala, case class is widely adopted for data processing. I just implemented the equiv of case class in Goldfish Scheme and packaged it in the (liii case) R7RS library: https://github.com/LiiiLabs/goldfish/blob/main/goldfish/liii/case.scm <https://github.com/LiiiLabs/goldfish/blob/main/goldfish/liii/case.scm > Here are sample usages of define-case-class. Sample code 1: simplest use case ---------------------------------------------------------- (define-case-class person ((name string? "Bob") (age integer?))) (let1 bob (person :name "Bob" :age 21) (check (bob 'name) => "Bob") (check (bob 'age) => 21) (check ((bob :name "hello") 'name) => "hello") (check-catch 'value-error (bob 'sex)) (check-true (person? bob))) (check-true (person=? (person "Bob" 21) (person "Bob" 21))) (check-false (person=? (person "Bob" 21) (person "Bob" 20))) (check-catch 'type-error (person 1 21)) -------------------------------------------------------------------------- Note: let1 is the simplified version of let in the (liii base) library Sample code 2: Use case class with pattern matching --------------------------------------------------------------------------------- (let ((bob (person "Bob" 21)) (get-name (lambda (x) (case* x ((#<person?>) (x 'name)) (else (???)))))) (check (get-name bob) => "Bob") (check-catch 'not-implemented-error (get-name 1))) ------------------------------------------------------------------------------------ Note: (???) means (error 'not-implemented-error), it comes for Scala Sample code 3: Use case class with companion functions ------------------------------------------------------------------------------------ (define-case-class jerson ((name string?) (age integer?)) (define (to-string) (string-append "I am " name " " (number->string age) " years old!")) (define (greet x) (string-append "Hi " x ", " (to-string))) ) -----------------------------------------------------------------------------------
_______________________________________________ Cmdist mailing list [email protected] https://cm-mail.stanford.edu/mailman/listinfo/cmdist
