Yes. 

I have used this pattern extensively and locally defined macros is what you 
want. But, just in case it is not obvious, you can also mutate fields in Racket 
and perhaps that’s what you really want. Racket is not ideological about 
functional purity. 



> On Feb 8, 2017, at 3:54 PM, Steve Byan's Lists <steve-l...@byan-roper.org> 
> wrote:
> 
> I'm just learning Racket's object system. I feel like I've missed something, 
> as it seems pretty verbose to functionally update objects.
> 
> The pattern I use when functionally updating object state is to invoke the 
> class constructor with a full set of arguments, some subset of which have 
> updated values. Something like this:
> 
> #lang racket
> 
> (require math/statistics)
> 
> (define foo%
>  (class object%
>    (super-new)
>    (init name)
>    (init (stat empty-statistics))
>    (field [my-name name]
>           [my-stat stat])
>    (define/public (update-stat x)
>      (new this% [name my-name] [stat (update-statistics my-stat x)]))))
> 
> (define foo-stats
>  (for/fold
>   ([current-foo (new foo% [name "foo-object"])])
>   ([x (in-list (list 0.1 0.5 0.15 0.4 0.09))])
>    (send current-foo update-stat x)))
> 
> 
> When the class has lots of fields, it is pretty verbose to provide all the 
> arguments to the constructor, even for fields that are not being updated. I 
> feel like I'm missing something that would reduce the amount of typing 
> involved. I suspect the "something" is macros, but I'm not there yet in my 
> journey through Racket-land. Is there some pre-fabbed construct that I've 
> overlooked?
> 
> Best regards,
> -Steve
> 
> --  
> Steve Byan
> steveb...@me.com
> Littleton, MA
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to