开发者

Unsure of how to design a useful library using combinators

开发者 https://www.devze.com 2023-03-28 06:51 出处:网络
I\'ve been reading about combinators and seen how useful they are (for example, in Haskell\'s Parsec).My problem is that I\'m not quite sure how to use them practically.

I've been reading about combinators and seen how useful they are (for example, in Haskell's Parsec). My problem is that I'm not quite sure how to use them practically.

Here's an outline of the problem: distributions can be generated, filtered, and modified. Distributions may be combined to create new distributions.

The basic interfaces are (in pseudo-Haskell type terminology):

generator::      parameters -> distribution

selector::       parameters -> (distribution -> distribution)

modifier::       parameters -> (distribution -> distribution)

Now, I think that I see three combinators:

combine::     generator -> generator -> generator

filter::      generator -> selector -> generator

modify::      generator -> modifier -> generator

Are these actually combina开发者_运维百科tors? Do the combinators make sense/are there any other obvious combinators that I'm missing?

Thanks for any advice.


The selector and modifier functions are already perfectly good combinators! Along with generator and combine you can do stuff like (I'm going to assume statistical distributions for concreteness and just make things up!):

modifier (Scale 3.0) $ generator StandardGaussian `combine` selector (LargerThan 10) . modifier (Shift 7) $ generator (Binomial 30 0.2)

You may have to mess around a bit with the priority of the combine operator for this to work smoothly :)

In general, when I'm trying to design a combinator library for values of type A, I like to keep my A's "at the end", so that the partially applied combinators (your selector and modifier) can be chained together with . instead of having to flip through hoops.

Here's a nice blog article which can help you design combinators, it influenced a lot of my thinking: Semantic Editor Combinators.

EDIT: I may have misread your question, given the type signature of combine. Maybe I'm missing something, but wouldn't the distributions be the more natural objects your combinator should work on?

0

精彩评论

暂无评论...
验证码 换一张
取 消