massung
2021-7-26 16:03:59

Racket related, but API design question I’m curious what people prefer…

I have a container type and I’m making a function(s) that extract returns a stream of values from the container. If the container was an ordered hash, then the functions might be something like…

(ordered-hash->stream h) ;=> stream of all values stored in all keys (ordered-hash->stream h key) ;=> stream of all values stored at key (ordered-hash->stream h #:from #:to) ;=> stream of all values from key up to key Obviously there’s no polymorphism in Racket, so, what does your programming sensibility lean towards…

  1. A “do all” function with a signature like (h [key #f] #:from [from #f] #:to [to #f]), but then the function attempts to do all sorts of error checking (cannot combine key with #:from and #:to)?
  2. Two functions: one for an exact key and the other with #:from and #:to where if they are not specified it’s the entire container space? I’m heavily leaning towards the second, but then trying to get names. Maybe something like ->stream for the key case and ->range for the from/to case? Maybe there are similar examples in Racket of this already where a naming pattern already exists?

massung
2021-7-26 16:14:36

Or… 3. Only support the #:from - #:to case, but make it inclusive instead of #:to being exclusive.


samth
2021-7-26 16:20:18

I like option 3, and then you don’t need a keyword for #:from