转换厘米为英尺和英寸

时间:2019-09-15 03:35:54

标签: javascript functional-programming ramda.js

我正在尝试编写一个将cm转换为feetinches的函数

cmToFtIn(30) === {"feet": 1, "inches": 0}
cmToFtIn(29) === {"feet": 0, "inches": 11}

我已经做的就是这个复杂的功能

const cmToFeetMultiplier = multiply(1/30.5)
const ftToInMultiplier = multiply(12)
const floor = (num) => Math.floor(num)
const round = (num) => Math.round(num)
const cmToFtIn = pipe(
   cmToFeetMultiplier,
   juxt([identity, floor]),
   juxt([identity, pipe(apply(subtract), ftToInMultiplier, round)]),
   flatten,
   cond([
     [pipe(nth(2), equals(12)), (d) => ({feet: d[1] + 1, inches: 0})],
     [always(true), (d) => ({feet: d[1], inches: d[2]})],
   ])
  )

也许有人对如何简化它有一些建议?

Playground

3 个答案:

答案 0 :(得分:2)

当您用Google搜索“厘米为英寸”时:

enter image description here

和“厘米到英尺”:

enter image description here

然后我们可以构建inchfoot函数:

const inch = flip(divide)(2.54);
const foot = flip(divide)(30.48);

inch(30); //=> 11.811023622047244
foot(30); //=> 0.984251968503937

如果需要从 cm 中的值返回对象{inch, foot},则可以不用Ramda:

const cmToFtIn = cm => ({inch: inch(cm), foot: foot(cm)});

cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}

使用Ramda:

const cmToFtIn = applySpec({inch, foot});

cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}

我个人建议您不要直接从inchfoot函数返回舍入值。您可以在需要的地方应用第二遍将它们四舍五入。两种选择:

在您的Math.round对象上应用{inch, foot}

map(Math.round, cmToFtIn(30));
//=> {"foot": 1, "inch": 12}

或编写Math.roundinch / foot函数:

const cmToFtIn = applySpec({
  inch: compose(Math.round, inch),
  foot: compose(Math.round, foot)
});

cmToFtIn(30);
//=> {"foot": 1, "inch": 12}

答案 1 :(得分:2)

除非这是学习Ramda的练习,否则我建议Ramda不是适合此工作的工具。我是Ramda的作者之一,并且是狂热爱好者,但是它是一个工具,旨在帮助使FP样式在JS(而不是通用实用程序库)中更可口。

问题在于此函数可以用香草JS非常简单地编写:

DispatchQueue.main.async { //make sure all UI updates are on the main thread.
        nav?.view.layer.add(CATransition().rightToLeft(), forKey: nil)
        nav?.pushViewController(YourViewController(), animated: false)
    }

或者,如果您不喜欢默认参数,则可能是:

const cmToInFt = (cm, inches = Math .round (cm / 2.54)) => ({
  feet: Math .floor (inches / 12),
  inches: inches % 12
})

const myHeight = cmToInFt (185)

console .log (myHeight)

Ramda对此几乎没有增加。显然,如您的方法和customcommander的答案所示,您可以使用Ramda函数提供帮助,并且通常使用少量const cmToInFt = (cm) => { const inches = Math .round (cm / 2.54) return { feet: Math .floor (inches / 12), inches: inches % 12 } } 是有用的。但这(与Aadit M Shah的答案非常相似,结构略有不同)已经很干净而且可读。我几乎看不到会更容易使用它。

答案 2 :(得分:1)

一英寸有2.54厘米。因此,我们有了方程式。

const cmToInches = cm => cm / 2.54;

英尺12英寸。因此,我们有了方程式。

const inchesToFtIn = inches => ({
  feet: Math.floor(inches / 12),
  inches: inches % 12,
});

现在,要将厘米转换为英尺和英寸,我们只需编写这两个函数。

const cmToInches = cm => cm / 2.54;

const inchesToFtIn = inches => ({
  feet: Math.floor(inches / 12),
  inches: inches % 12,
});

const cmToFtIn = cm => inchesToFtIn(cmToInches(cm));

console.log(cmToFtIn(30.48)); // 1 foot
console.log(cmToFtIn(27.94)); // 11 inches

如果您需要四舍五入,那是一个很小的变化。

const cmToInches = cm => cm / 2.54;

const inchesToFtIn = inches => ({
  feet: Math.floor(inches / 12),
  inches: inches % 12,
});

const cmToFtIn = cm => inchesToFtIn(Math.round(cmToInches(cm)));

console.log(cmToFtIn(30)); // 1 foot
console.log(cmToFtIn(29)); // 11 inches

希望有帮助。