민화와 람다는 왜 이렇게 다른가요?
나는 DrBoolean의 책 을 읽고 자바 스크립트 FP를 배우고 있습니다.
함수형 프로그래밍 라이브러리를 검색했습니다. Ramda와 Folktale을 찾았습니다. 둘 다 함수형 프로그래밍 라이브러리라고 주장합니다.
그러나 그들은 너무 다릅니다.
Ramda 는 목록을 처리하기위한 유틸리티 함수를 포함하는 것 같습니다 : 맵, 리 듀스, 필터 및 순수 함수 : 카레, 작성. 그것은 모나드, 펑터를 다루는 어떤 것도 포함하지 않습니다.
그러나 Folktale 에는 목록이나 기능에 대한 유틸리티가 포함되어 있지 않습니다. 모나드와 같은 자바 스크립트에서 일부 대수 구조를 구현하는 것 같습니다 : Maybe, Task ...
사실 더 많은 도서관을 찾았는데 모두 두 가지 범주에 속하는 것 같습니다. 밑줄, lodash는 Ramda와 매우 비슷합니다. Fantasy-land, pointfree-fantasy는 민화와 같습니다.
이러한 매우 다른 라이브러리를 모두 기능적 이라고 할 수 있습니까 ? 그렇다면 각각을 기능적 라이브러리로 만드는 것은 무엇입니까?
기능적 특징
함수형 프로그래밍 또는 함수형 라이브러리를 정의하는 데 명확한 경계가 없습니다. 기능 언어의 일부 기능은 Javascript에 내장되어 있습니다.
- 일류, 고차 함수
- Lambda / 익명 함수, 클로저 포함
다른 것들은주의해서 자바 스크립트에서 수행 할 수 있습니다.
- 불변성
- 참조 투명성
나머지는 ES6의 일부이며 현재 부분적으로 또는 완전히 사용 가능합니다.
- 컴팩트하고 간결한 기능
- 꼬리 호출 최적화를 통한 성능 재귀
그리고 자바 스크립트의 정상적인 범위를 넘어서는 많은 것들이 있습니다.
- 패턴 매칭
- 게으른 평가
- 동질성
그러면 라이브러리는 지원하려는 기능의 종류를 선택하고 선택할 수 있으며 여전히 "기능적"이라고 합리적으로 호출 할 수 있습니다.
판타지 랜드 사양
Fantasy-land 는 수학적 범주 이론 및 추상 대수에서 함수형 프로그래밍 (예 : Monoid , Functor 및 Monad)으로 이식 된 여러 표준 유형에 대한 사양입니다 . 이러한 유형은 상당히 추상적이며 더 친숙한 개념을 확장 할 수 있습니다. 예를 들어, Functor map
는 함수 map
를 사용하여 배열을 ped over 할 수있는 컨테이너입니다 Array.prototype.map
.
민화
Folktale 은 Fantasy-land 사양의 다양한 부분을 구현하는 유형 모음과 작은 동반 유틸리티 함수 모음입니다. 이러한 유형은 Maybe , Either , Task (다른 곳에서 Future라고 부르는 것과 매우 유사하며 Promise에 대한 더 합법적 인 사촌) 및 Validation과 같은 것입니다.
Folktale은 아마도 Fantasy-land 사양의 가장 잘 알려진 구현이며 잘 알려져 있습니다. 그러나 확정적 또는 기본 구현과 같은 것은 없습니다. fantasy-land는 추상 유형 만 지정하며 물론 구현은 이러한 구체적인 유형을 만들어야합니다. 기능적 라이브러리라는 Folktale의 주장은 분명합니다. 일반적으로 기능적 프로그래밍 언어에서 볼 수있는 데이터 유형을 제공하므로 기능적 방식으로 프로그래밍하는 것이 훨씬 더 쉽습니다.
Folktale 문서 의이 예제 는 사용 방법을 보여줍니다.
// We load the library by "require"-ing it
var Maybe = require('data.maybe')
// Returns Maybe.Just(x) if some `x` passes the predicate test
// Otherwise returns Maybe.Nothing()
function find(predicate, xs) {
return xs.reduce(function(result, x) {
return result.orElse(function() {
return predicate(x)? Maybe.Just(x)
: /* otherwise */ Maybe.Nothing()
})
}, Maybe.Nothing())
}
var numbers = [1, 2, 3, 4, 5]
var anyGreaterThan2 = find(function(a) { return a > 2 }, numbers)
// => Maybe.Just(3)
var anyGreaterThan8 = find(function(a) { return a > 8 }, numbers)
// => Maybe.Nothing
람다
Ramda (disclaimer: I'm one of the authors) is a very different type of library. It does not provide new types for you.1 Instead, it provides functions to make it easier to operate on existing types. It is built around the notions of composing smaller functions into larger ones, of working with immutable data, of avoiding side-effects.
Ramda operates especially on lists, but also on objects, and sometimes on Strings. It also delegates many of its calls in such a manner that it will interoperate with Folktale or other Fantasy-land implementations. For instance, Ramda's map
function, operates similarly to the one on Array.prototype
, so R.map(square, [1, 2, 3, 4]); //=> [1, 4, 9, 16]
. But because Folktale's Maybe
implements the Fantasy-land Functor
spec, which also specifies map, you can also use Ramda's map
with it:
R.map(square, Maybe.Just(5)); //=> Maybe.Just(25);
R.map(square, Maybe.Nothing); //=> Maybe.Nothing
Ramda's claims to being a functional library lie in making it easy to compose functions, never mutating your data, and presenting only pure functions. Typical usage of Ramda would be to build up more complex function by composing smaller ones, as seen in an article on the philosphy of Ramda
// :: [Comment] -> [Number]
var userRatingForComments = R.pipe(
R.pluck('username') // [Comment] -> [String]
R.map(R.propOf(users)), // [String] -> [User]
R.pluck('rating'), // [User] -> [Number]
);
Other Libraries
Actually I found more libraries, they all seem fall into the two categorys. underscore, lodash are very like Ramda. Fantasy-land, pointfree-fantasy are like folktale.
That's not really accurate. First of all, Fantasy-land is simply a specification that libraries can decide to implement for various types. Folktale is one of many implementations of that specification, probably the best-rounded one, certainly one of the most mature. Pointfree-fantasy and ramda-fantasy are others, and there are many more.
Underscore and lodash are superficially like Ramda in that they are grab-bag libraries, providing a great number of functions with much less cohesion than something like Folktale. And even the specific functionality often overlaps with Ramda's. But at a deeper level, Ramda has very different concerns from those libraries. Ramda's closest cousins are probably libraries like FKit, Fnuc, and Wu.js.
Bilby is in a category of it's own, providing both a number of tools such as the ones provided by Ramda and some types consistent with Fantasy-land. (The author of Bilby is the original author of Fantasy-land as well.)
Your Call
All of these libraries have right to be called functional, although they vary greatly in functional approach and degree of functional commitment.
Some of these libraries actually work well together. Ramda should work well with Folktale or other Fantasy-land implementations. Since their concerns barely overlap, they really don't conflict, but Ramda does just enough to make the interoperation relatively smooth. This is probably less true for some of the other combinations you could choose, but ES6's simpler function syntax may also take some of the pain out of integrating.
The choice of library, or even style of library to use, is going to depend on your project and your preferences. There are lots of good options available, and the numbers are growing, and many of them are improving greatly. It's a good time to be doing functional programming in JS.
1Well, there is a side-project, ramda-fantasy doing something similar to what Folktale does, but it's not part of the core library.
참고URL : https://stackoverflow.com/questions/33027305/why-are-folktale-and-ramda-so-different
'IT박스' 카테고리의 다른 글
양식 POST에서 헤더 필드를 설정하는 방법은 무엇입니까? (0) | 2020.10.06 |
---|---|
장고 신호 vs. 저장 방법 재정의 (0) | 2020.10.06 |
Ruby on Rails에서 컨트롤러 간 코드 재사용을위한 모범 사례 (0) | 2020.10.05 |
.NET에서 "HttpRequest.RequestType"및 "WebRequest.Method"값에 대한 상수는 어디에 있습니까? (0) | 2020.10.05 |
C ++의 부호없는 키워드 (0) | 2020.10.05 |