DEV Community

Discussion on: Daily Challenge #50 - Number Neighbor

Collapse
 
aminnairi profile image
Amin

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.

Collapse
 
jasman7799 profile image
Jarod Smith

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
Thread Thread
 
aminnairi profile image
Amin

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.