lambda - Does Haskell provide an idiom for pattern matching against many possible data constructors? -
working on haskell project, i'm dealing event
data type fsnotify package. constructors event
all:
added filepath utctime modified filepath utctime removed filepath utctime
in code, i'm interested in extracting filepath
event
, doing same action regardless of type constructor; because of this, i'm tempted make lambda.
unfortunately, code suffers reduced readability when drop case
expression lambda pattern match against 3 cases; there built-in idiom extract filepath
in 1 expression without having manually pattern match, given relative homogeneity of type constructors?
i've tried passing expression anonymous function in call watchdir
action:
wm <- startmanager sw <- watchdir wm "." (\_ -> true) (\(_ f t) -> putstrln f)
but, predictably, don't-care value in lambda's pattern match causes parse error.
the simplest way reflect homogenity of alternatives in type declaration, instead of observing it:
data action = added | modified | removed data event = fileevent action filepath utctime | otherevent ... f :: event -> filepath f (fileevent _ path _) = path
in general, haskell has no way know constructor alternatives have same number of arguments , same type, no, can't abstract on choice of alternative.
Comments
Post a Comment