loading...

### re: Daily Challenge #50 - Number Neighbor VIEW POST

// areaCode :: String -> String
const areaCode = str => str.substring(0,3);
// threeCode :: String -> String
const threeCode = str => str.substring(3,6);
// fourCode :: String -> String
const fourCode = str => str.substring(6);
// formatPhone :: String -> String
const formatPhone = str => ${areaCode(str)}-${threeCode(str)}-${fourCode(str)}; // getNeighbors :: String -> [Number] -> [String] const getNeighbors = number => [parseInt(number) + 1, parseInt(number) - 1] .map(neighbor => formatPhone(${neighbor}));


Attempting to learn and understand more functional paradigms. Is this right way to solve this problem functionally?

Way to go buddy!

From my perspective, to format the phone number in 3 segments, I would have used this instead of the 3 calls you made.

"use strict"

const phoneNumber = "123456789"

const formatPhoneNumber = (string, glue) => string.match(/.{1,3}/g).join(glue)

console.log(formatPhoneNumber(phoneNumber, "-")) // 123-456-789
console.log(formatPhoneNumber(phoneNumber, " ")) // 123 456 789
console.log(formatPhoneNumber(phoneNumber, ".")) // 123.456.789


This leverage the regular expressions to match three characters at most. I know regular expressions is not the easiest thing to use but in this case it can be quite handy. As it returns an array, joining the items will produce a string back so the signature of the function can be written as follow in a purely functional programming language like PureScript.

formatPhoneNumber :: String -> String -> String


Unfortunately, currying (partially applying parameters) is not built-in in JavaScript. You could implement your own curry function and use it on all your future function definitions. But this would be overkill for this challenge. But if you would ask me how I would implement a currying function in JavaScript, here is my take.

"use strict"

function curry(callable, ...initialParameters) {
if (typeof callable !== "function") {
throw new TypeError("Function expected as first argument")
}

return function(...additionalParameters) {
const parameters = [...initialParameters, ...additionalParameters]

if (parameters.length >= callable.length) {
return callable(...parameters)
}

return curry(callable, ...parameters)
}
}

// format :: String -> Number -> String -> String
const format = curry(function(glue, segments, phone) {
return phone.match(new RegExp(.{1,\${segments}}, "g")).join(glue)
})

// usFormat :: String -> String
const usFormat = format("-", 3)

// frenchFormat :: String -> String
const frenchFormat = format(".", 2)

console.log(usFormat("123456789")) // 123-456-789
console.log(frenchFormat("1234567890")) // 12.34.56.78.90


Last tip, when creating a function, try to put the data structure at the end, that way you can construct partial application of your function without pain, even in JavaScript. In this case, this means puting the phone number to format at the very end of the parameter's list in your function definition. Hope that helps you in your functional programing journey pal.

Hey thanks for the response I will look at regexes next time for this kind of problem. Also I thought currying was supported in ES6?
via

const f = a => b=> a + b


What you did is indeed currying but it is not transparent in the sense that if you want to pass all three arguments at once you will have to make three calls. The curry implementation allows you to either pass all arguments at once like a regular function call or partially calling the function. I kinda dislike calling my function multiple times and the only two reasons are aesthetic and the fact that it is less natural to define functions like that but arrow functions syntax makes it a little bit cleaner.

Code of Conduct Report abuse