比较器函数ascending
接受两个参数 - a
和b
。它必须返回一个比较两者的整数。
我有一个列表,我想按名称排序,所以我写了以下函数。
我是否可以使用功能成语来组合这两个函数,而不是让byName
负责编写结果函数?
const ascending = (a, b) => a.localeCompare(b);
const byName = (i) => i.get('name');
const useTogether = (...fns) => ...; // is there an idiomatic function like this?
// usage
items.sort(useTogether(byName(ascending)));
答案 0 :(得分:7)
为了正确地理解它们,让我们从检查最基本的分类程序
开始
const compare = (a, b) =>
a.localeCompare (b)
const data =
[ 'Cindy'
, 'Alice'
, 'Darius'
, 'Bertrand'
]
data.sort (compare)
console.log (data)
// Alice, Bertrand, Cindy, Darius
没什么特别的。让我们制作我们的第一个逆变函子Comparison
。这种类型会导致突变,但无论如何它只是用于演示。专注于contramap
const compare = (a, b) =>
a.localeCompare (b)
const Comparison = (f = compare) =>
({ contramap : g =>
Comparison ((a, b) => f (g (a), g (b)))
, sort : xs =>
xs.sort (f)
})
const data =
[ { name: 'Cindy' }
, { name: 'Alice' }
, { name: 'Darius' }
, { name: 'Bertrand' }
]
Comparison ()
.contramap (x => x.name)
.sort (data)
console.log (data)
// Alice, Bertrand, Cindy, Darius
组成法持有
m.contramap(f).contramap(g) == m.contramap(compose(f,g))
const compare = (a, b) =>
a.localeCompare (b)
const Comparison = (f = compare) =>
({ contramap : g =>
Comparison ((a, b) => f (g (a), g (b)))
, sort : xs =>
xs.sort (f)
})
const data =
[ { name: 'Cindy' }
, { name: 'Alice' }
, { name: 'Darius' }
, { name: 'Bertrand' }
]
const compose = (f, g) =>
x => f (g (x))
Comparison ()
.contramap (compose (x => x.substr (1), x => x.name))
// equivalent to
// .contramap (x => x.substr (1)) // sort starting with second letter
// .contramap (x => x.name) // get name property
.sort (data)
console.log (data)
// sorted by second letter this time (A, E, I, L)
// Darius, Bertrand, Cindy, Alice
// ^ ^ ^ ^
实现monoid接口可以为您提供诸如“多排序”之类的很酷的东西
const Eq =
0
const Lt =
-1
const Gt =
1
const Ord =
{ empty: Eq
, concat: (a,b) =>
a === Eq ? b : a
}
const compare = (a, b) =>
a < b ? Lt
: a > b ? Gt
: Eq
const Comparison = (f = compare) =>
({ compare: f
, contramap : g =>
Comparison ((a, b) => f (g (a), g (b)))
, concat : m =>
Comparison ((a, b) =>
Ord.concat (f (a, b), m.compare (a, b)))
, sort : xs =>
xs.sort (f)
})
const data =
[ { name: 'Alicia', age: 10 }
, { name: 'Alice', age: 15 }
, { name: 'Alice', age: 10 }
, { name: 'Alice', age: 16 }
]
const sortByName =
Comparison ()
.contramap (x => x.name)
const sortByAge =
Comparison ()
.contramap (x => x.age)
sortByName
.concat (sortByAge)
.sort (data)
console.log ('sorted by (name,age)', data)
// Alice 10
// Alice 15
// Alice 16
// Alicia 10
sortByAge
.concat (sortByName)
.sort (data)
console.log ('sorted by (age,name)', data)
// Alice 10
// Alicia 10
// Alice 15
// Alice 16
阅读链接文章以获取更多有用信息和传感器介绍
答案 1 :(得分:5)
我不确定它是否满足您正在寻找的内容,但这里有一个可能的useTogether
(有一些不同的签名)表达式。我并不知道具有恰当效果的标准功能。
const ascending = (a, b) => a.localeCompare(b);
const byName = (i) => i['name'];
const useTogether = (selector, consumer) => (...fnArgs) => consumer(...fnArgs.map(selector));
var items = [{ name: "C" }, { name: "A" }, { name: "B" }];
console.log(
items.sort(useTogether(byName, ascending))
)
&#13;