Skip to content

Instantly share code, notes, and snippets.

@ken-okabe
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken-okabe/7e7333e27549ca5c631b to your computer and use it in GitHub Desktop.
Save ken-okabe/7e7333e27549ca5c631b to your computer and use it in GitHub Desktop.
#facebook-immutable で階乗計算
#facebook-immutable で階乗計算
facebook-immutable で階乗計算
##Range()
http://facebook.github.io/immutable-js/docs/#/Range
>Returns a IndexedSeq of numbers from `start` (inclusive) to `end` (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to infinity. When `start` is equal to `end`, returns empty range.
>Range(start?: number, end?: number, step?: number): IndexedSeq<number>
```js
Range() // [0,1,2,3,...]
Range(10) // [10,11,12,13,...]
Range(10,15) // [10,11,12,13,14]
Range(10,30,5) // [10,15,20,25]
Range(30,10,5) // [30,25,20,15]
Range(30,30,5) // []
```
##コード
```js
var _ = require('immutable');
var plus = function(a, b)
{
return a * b;
};
```
階乗を求める関数
```js
var factorial = function(n)
{
var x = _.Range(1, n + 1) // natural numbers starts from 1 to n
.reduce(plus);
return x;
};
```
または、
```js
var factorial = function(n)
{
var x = _.Range(1); // natural numbers starts from 1
.take(n)
.reduce(plus);
return x;
};
```
表示
```js
console.log(factorial(3)); // 1*2*3 = 6
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment