- Notifications
You must be signed in to change notification settings - Fork5
2015 003d STRING
signature STRINGWe propose three new functions for theSTRING signature.
This page is part of proposal2015-003.
val rev : string -> stringval implodeRev : char list -> stringval 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 aconcatRev operation that is equivalent to
concat o List.rev
for the same reason thatimplodeRev is useful.
I am not sure what the best order for the arguments toconcatWithMap is. I decided to putthe separator first, because I could imagine wanting to format lists of various differentelement 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 putthe function argument first.
Therev function provides an efficient way to reverse a string, which is an occasionallyuseful operation.TheimplodeRev functions is useful for when a list of characters has been constructed,but is in the wrong order.TheconcatWithMap function captures an extremely common pattern where one must firstmap ato-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 theprogrammer and is amenable to a more efficient implementation.
