这可以重构为使用通用功能原则吗?

Ben*_*Ben 5 javascript functional-programming

比较器函数ascending接受两个参数 - ab.它必须返回一个比较两者的整数.

我有一个列表,我想按名称排序,所以我写了以下函数.

是否有一种功能习惯用法可以用来组合这两个功能,而不是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))); 
Run Code Online (Sandbox Code Playgroud)

Tha*_*you 7

你正在寻找逆变函子

为了正确地理解它们,让我们从检查最基本的分类程序开始

const compare = (a, b) =>
  a.localeCompare (b)

const data =
  [ 'Cindy'
  , 'Alice'
  , 'Darius'
  , 'Bertrand'
  ]
  
data.sort (compare)

console.log (data)
// Alice, Bertrand, Cindy, Darius
Run Code Online (Sandbox Code Playgroud)

没什么特别的.让我们制作我们的第一个逆变函子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
Run Code Online (Sandbox Code Playgroud)

组成法成立

m.contramap(f).contramap(g) == m.contramap(compose(f,g))
Run Code Online (Sandbox Code Playgroud)

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
//  ^       ^         ^      ^
Run Code Online (Sandbox Code Playgroud)

实现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
Run Code Online (Sandbox Code Playgroud)

阅读链接文章以获取更多有用信息和传感器介绍


S M*_*han 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))
)
Run Code Online (Sandbox Code Playgroud)