-
Notifications
You must be signed in to change notification settings - Fork 4
2015 003d STRING
signature STRING
We propose three new functions for the STRING
signature.
This page is part of proposal 2015-003.
val rev : string -> string
val implodeRev : char list -> string
val concatWithMap : string -> ('a -> string) -> 'a list -> string
rev s
returns the reverse of `s`.
implodeRev chrs
is equivalent to the expression `implode (List.rev chrs)`.
concatWithMap sep f items
returns a string that is constructed by converting the items to strings using the function `f` and then joining them using the separator string `sep`. For example, `concatWithMap "," Int.toString [1, 2, 3]` evaluates to the string `"1,2,3"`.
We might also want to add a concatRev
operation that is equivalent to
concat o List.rev
for the same reason that implodeRev
is useful.
I am not sure what the best order for the arguments to concatWithMap
is. I decided to put
the separator first, because I could imagine wanting to format lists of various different
element types using the same separator.
Furthermore, since it follows the order of the pattern that it replaces; i.e.,
concatWith sep (List.map f items)
On the other hand, the Basis Library convention for higher-order combinators has been to put the function argument first.
The rev
function provides an efficient way to reverse a string, which is an occasionally
useful operation.
The implodeRev
functions is useful for when a list of characters has been constructed,
but is in the wrong order.
The concatWithMap
function captures an extremely common pattern where one must first
map a to-string conversion over a list and the concatenate the result with a separator.
Combining the map and concatenation in a single function provides convinence to the
programmer and is amenable to a more efficient implementation.