"포인트 프리"스타일 란 무엇입니까 (기능적 프로그래밍)?
-
09-09-2019 - |
해결책
그냥보세요 위키 백과 기사 정의를 얻으려면 :
Tacit 프로그래밍 (Point-Free Programming)은 함수 정의가 변수 대신 조합 및 기능 구성 [...]를 사용하여 인수에 관한 정보를 포함하지 않는 프로그래밍 패러다임입니다.
Haskell 예 :
기존 (인수를 명시 적으로 지정) :
sum (x:xs) = x + (sum xs)
sum [] = 0
점이없는 (sum
명백한 주장이 없습니다. +
0으로 시작 : :
sum = foldr (+) 0
또는 심지어 간단한 : 대신 g(x) = f(x)
, 당신은 그냥 쓸 수 있습니다 g = f
.
예 : 카레와 밀접한 관련이 있습니다 (또는 기능 구성과 같은 작업).
다른 팁
포인트 프리 스타일은 정의되는 함수의 인수가 명시 적으로 언급되지 않았으며 함수는 함수 구성을 통해 정의된다는 것을 의미합니다.
두 가지 기능이있는 경우
square :: a -> a
square x = x*x
inc :: a -> a
inc x = x+1
이 두 기능을 계산하는 두 기능으로 결합하려면 x*x+1
, 당신은 다음과 같이 "point-full"을 정의 할 수 있습니다.
f :: a -> a
f x = inc (square x)
포인트가없는 대안은 논쟁에 대해 이야기하지 않는 것입니다. x
:
f :: a -> a
f = inc . square
자바 스크립트 샘플 :
//not pointfree cause we receive args
var initials = function(name) {
return name.split(' ').map(compose(toUpperCase, head)).join('. ');
};
const compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];
const join = m => m.join();
//pointfree
var initials = compose(join('. '), map(compose(toUpperCase, head)), split(' '));
initials("hunter stockton thompson");
// 'H. S. T'
포인트 프리 스타일은 코드가 존재하고 사용되고 있음에도 불구하고 코드가 인수를 명시 적으로 언급하지 않음을 의미합니다.
이것은 기능이 작동하는 방식으로 인해 Haskell에서 작동합니다.
예를 들어:
myTake = take
하나의 인수를 취하는 함수를 반환하므로 원하지 않는 한 인수를 명시 적으로 입력 할 이유가 없습니다.
다음은 다른 라이브러리가없는 TypeScript의 한 예입니다.
interface Transaction {
amount: number;
}
class Test {
public getPositiveNumbers(transactions: Transaction[]) {
return transactions.filter(this.isPositive);
//return transactions.filter((transaction: {amount: number} => transaction.amount > 0));
}
public getBigNumbers(transactions: Transaction[]) {
// point-free
return transactions.filter(this.moreThan(10));
// not point-free
// return transactions.filter((transaction: any) => transaction.amount > 10);
}
private isPositive(transaction: Transaction) {
return transactions.amount > 0;
}
private moreThan(amount: number) {
return (transaction: Transaction) => {
return transactions.amount > amount;
}
}
}
포인트 프리 스타일이 더 "유창하고"읽기 쉽다는 것을 볼 수 있습니다.
제휴하지 않습니다 StackOverflow