in reply to Currying--useful examples?
sub mk_tagger { my($tag) = @_; return sub { my($txt) = @_; "<$tag>$txt</$tag>" }; } my ($bold, $underline, $italic) = map { mk_tagger($_) } qw( b u i ); print $bold->("what a " . $underline->("nice") . $italic->(" day"));
Here's the equivalent Perl 6:
sub mk_tagger ($tag) { -> { "<$tag>$^txt</$tag>" } }
The idea is that the mk_taggger function is intentionally designed not to be used directly but to generate tagging functions.
Update: In Haskell and other languages that had autocurrying, you'd not need this explicit distinction.
tagStr :: String -> String -> String tagStr tag text = "<" ++ tag ++ ">" ++ text ++ "</" ++ tag ++ ">" -- now I can use it either way. tagStr "b" "a bold moose" underline = tagStr "u" underline "an underlined moose"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Currying--useful examples?
by gaal (Parson) on Jan 10, 2007 at 20:50 UTC |