In this exercise you will implement a partial set of utility routines to help a developer clean up identifier names.
In the 6 tasks you will gradually build up the functions transform to convert single characters and clean to convert strings.
A valid identifier comprises zero or more letters, underscores, hyphens, question marks and emojis.
If an empty string is passed to the clean function, an empty string should be returned.
Implement the transform function to replace any hyphens with underscores.
transform '-' // => "_"Remove all whitespace characters. This will include leading and trailing whitespace.
transform ' ' // => ""Modify the transform function to convert camelCase to kebab-case
transform 'D' // => "-d"Modify the transform function to omit any characters that are numeric.
transform '7' // => ""Modify the transform function to replace any Greek letters in the range 'α' to 'ω'.
transform 'β' // => "?"Implement the clean function to apply these operations to an entire string.
Characters which fall outside the rules should pass through unchanged.
clean " a2b Cd-ω😀 " // => "ab-cd_?😀"This topic will be covered in detail later in the syllabus.
For now, it may be useful to know that there is a higher order function called String.collect that converts a collection of chars to a string, using a function that you supply.
let transform ch = $"{ch}_"
String.collect transform "abc" // => "a_b_c_"In this exercise you will implement a partial set of utility routines to help a developer clean up identifier names.
In the 6 tasks you will gradually build up the functions transform to convert single characters and clean to convert strings.
A valid identifier comprises zero or more letters, underscores, hyphens, question marks and emojis.
If an empty string is passed to the clean function, an empty string should be returned.
Implement the transform function to replace any hyphens with underscores.
transform '-' // => "_"Remove all whitespace characters. This will include leading and trailing whitespace.
transform ' ' // => ""Modify the transform function to convert camelCase to kebab-case
transform 'D' // => "-d"Modify the transform function to omit any characters that are numeric.
transform '7' // => ""Modify the transform function to replace any Greek letters in the range 'α' to 'ω'.
transform 'β' // => "?"Implement the clean function to apply these operations to an entire string.
Characters which fall outside the rules should pass through unchanged.
clean " a2b Cd-ω😀 " // => "ab-cd_?😀"This topic will be covered in detail later in the syllabus.
For now, it may be useful to know that there is a higher order function called String.collect that converts a collection of chars to a string, using a function that you supply.
let transform ch = $"{ch}_"
String.collect transform "abc" // => "a_b_c_"