-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #130
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Y-combinator | ||
* | ||
* The Y combinator is an interesting function which only works with functional languages, | ||
* showing how recursion can still be done even without any variable or function declarations, | ||
* only functions and parameters | ||
* | ||
* @func Y | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/2.3.0|v2.3.0} | ||
* @category Function | ||
* @sig (a, ... -> b -> b) -> (a, ... -> b) | ||
* @param {Function} le Recursive function maker | ||
* @return {Function} | ||
* @see {@link http://kestas.kuliukas.com/YCombinatorExplained/|Y combinator explained} | ||
* @example | ||
* | ||
* const makeFact = givenFact => (n) => { | ||
* if (n < 2) { return 1 } | ||
* return n * givenFact(n - 1); | ||
* }; | ||
* | ||
* const factorial = RA.Y(makeFact); | ||
* | ||
* factorial(5); //=> 120 | ||
*/ | ||
|
||
const Y = le => (f => f(f))(g => le(x => (g(g))(x))); | ||
|
||
export default Y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
|
||
describe('Y', function() { | ||
it('tests for making a factorial function', function() { | ||
const makeFact = givenFact => (n) => { | ||
if (n < 2) { return 1 } | ||
return n * givenFact(n - 1); | ||
}; | ||
|
||
const factorial = RA.Y(makeFact); | ||
|
||
eq(factorial(5), 120); | ||
}); | ||
}); |