Fix the Flavors: Solution Review
Solution review.
We’ll cover the following
#
We know from the last exercise that lensPath is the easiest way to focus on everyone’s favorite flavor.
修正风格:解决方案回顾 解决方案审查。
我们将介绍以下内容 上装口味 添加风味 # 从上一个练习中我们知道,lensPath是关注每个人最喜欢的口味的最简单方法。
import { lensPath } from 'ramda';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
Then combining it with map and view will fetch them.
import { lensPath, map, view } from 'ramda';
import employees from './employees.json';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
const result = map(view(favoriteFlavor), employees);
console.log({ result });
But now there’s work to do in between. Before view, we need a setter lens. Unfortunately set is only good for static values.
但现在中间还有工作要做。在观看之前,我们需要一个调整镜头。不幸的是,set只适用于静态值。
set(favoriteFlavor, 'Chocolate');
Uppercasing Flavors #
over, however, lets us set using functions! We can use Ramda’s toUpper function to uppercase all the flavors.
上装口味# 不过,让我们使用函数设置!我们可以使用Ramda的toUpper函数将所有口味都大写。
import { lensPath, map, over, toUpper, view } from 'ramda';
import employees from './employees.json';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
const modifiedFlavors = map(over(favoriteFlavor, toUpper), employees);
const result = map(view(favoriteFlavor), modifiedFlavors);
console.log({ result });
Again, it’s a list of employees so map’s necessary here. over focuses on an object using the favoriteFlavor lens, and uppercases its value.
That returns a new object with the modified flavors.
Passing that new array to map(view) returns the capitalized flavors.
同样,这是一份员工名单,所以这里需要地图。使用favoriteFlavor镜头过度聚焦对象,大写其值。 它返回一个具有修改的风格的新对象。 将新数组传递给map(view)返回大写的样式。
Appending to Flavors #
But wait there’s more! We must also append “IS A GREAT FLAVOR” to each of them.
We could combine that with toUpper using good ol’ pipe.
添加风味# 但是等等,还有更多!我们还必须在每一个词后面加上“味道很好”。 我们可以把它和toUpper结合起来使用好的ol’pipe。
import { lensPath, map, pipe, over, toUpper, view } from 'ramda';
import employees from './employees.json';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
const modifiedFlavors = map(over(favoriteFlavor, pipe(toUpper, (flavor) => `${flavor} IS A GREAT FLAVOR`)), employees);
const result = map(view(favoriteFlavor), modifiedFlavors);
console.log({ result });
This does the job, but not very eloquently. Look at that nesting!
const modifiedFlavors = map(over(favoriteFlavor, pipe(toUpper, (flavor) => `${flavor} IS A GREAT FLAVOR`)), employees);
Let’s create a helper function, emphasize.
import { lensPath, map, pipe, over, toUpper, view } from 'ramda';
import employees from './employees.json';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
const emphasize = pipe(
toUpper,
(flavor) => `${flavor} IS A GREAT FLAVOR`
);
const modifiedFlavors = map(over(favoriteFlavor, emphasize), employees);
const result = map(view(favoriteFlavor), modifiedFlavors);
console.log({ result });
Good, let’s go a step further.
Remember that lenses compose too. We don’t need to separate them and call map twice!
import { lensPath, map, pipe, over, toUpper, view } from 'ramda';
import employees from './employees.json';
const favoriteFlavor = lensPath([
'interests',
'foods',
'sweets',
'iceCream',
'favoriteFlavor'
]);
const emphasize = pipe(
toUpper,
(flavor) => `${flavor} IS A GREAT FLAVOR`
);
const emphasizeFlavor = pipe(
over(favoriteFlavor, emphasize),
view(favoriteFlavor)
);
const emphasizeFlavors = map(emphasizeFlavor);
const result = emphasizeFlavors(employees);
console.log({ result });
import { view, map, concat, flip, lensPath, over, pipe, toUpper } from "ramda";
import employees from "./employees.js";
const flavorLens = lensPath([
"interests",
"foods",
"sweets",
"iceCream",
"favoriteFlavor",
]);
const emphasize = pipe(toUpper, flip(concat)("IS A GREAT FLAVOR"));
const emphasizeFlavors = map(
pipe(over(flavorLens, emphasize), view(flavorLens))
);
const result = emphasizeFlavors(employees);
console.log({ result });