Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose untranspiled CJS & ESM modules #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 106 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@ methods can be called in-line as opposed composing function _a priori_.

## Usage

let f = require('functify');
```javascript
const f = require('functify');

let numbers = f([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

for (let odd in numbers.filter(n => n % 2)) {
console.log(n); // 1, 3, 5, ...
}

for (let even in numbers.filter(n => !(n % 2))) {
console.log(n); // 2, 4, 6, ...
}

for (let [odd, even] in numbers.split(n => n % 2, n => !(n % 2)).zip()) {
console.log(`odd = ${odd}, even = ${even}`); // [1, 2], [3, 4], ...
}

for (let square in numbers.take(3).map(n => n * n)) {
console.log(square); // 1, 4, 9
}
const numbers = f([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

for (const odd of numbers.filter(n => n % 2)) {
console.log(n); // 1, 3, 5, ...
}

for (const even of numbers.filter(n => !(n % 2))) {
console.log(n); // 2, 4, 6, ...
}

for (const square of numbers.take(3).map(n => n * n)) {
console.log(square); // 1, 4, 9
}
```

## Maps and Objects

Expand All @@ -50,21 +48,27 @@ The new Map class in ES6 has three methods which return iterators:

A Map instance itself can be used as an iterator, e.g.

let map = new Map();
map.set('x', 5);
map.set('y', 10);

for (let [k, v] of map) {
console.log(`map['${k}'] = ${v}`); // map['x'] = 5, map['y'] = 10
}
```javascript
const map = new Map([
['x', 5],
['y', 10]
]);

for (const [k, v] of map) {
console.log(`map['${k}'] = ${v}`); // map['x'] = 5, map['y'] = 10
}
```

`functify` wraps Map instances and exposes versions of `keys()`, `values()`,
and `entries()` that methods like `map()` and `filter()` can be chained to, e.g.

for (let v2 of functify(map).entries().map(pair => pair[1] * pair[1])) {
console.log(v2); // 25, 100
}


```javascript
for (const v2 of functify(map).entries().map(pair => pair[1] * pair[1])) {
console.log(v2); // 25, 100
}
```

Note: chaining in the opposite order is not allowed because map may return
something that isn't an entry, i.e. a [key, value] pair.

Expand All @@ -76,15 +80,17 @@ arrays which consume memory.
`functify` wraps Object instances, adding `keys()`, `values()`, and `entries()`
methods along with all the other methods that `functify` provides.

let obj = {
x: 5,
y: 10
}

for (let [k, v] of functify(obj)) {
console.log(`obj['${k}'] = ${v}`); // obj['x'] = 5, obj['y'] = 10
}

```javascript
const obj = {
x: 5,
y: 10
}

for (const [k, v] of functify(obj)) {
console.log(`obj['${k}'] = ${v}`); // obj['x'] = 5, obj['y'] = 10
}
```

The combines the simple creation and access syntax of Objects with the powerful
iterators provided by Map.

Expand All @@ -93,38 +99,44 @@ iterators provided by Map.
functify wraps iterables in an object with methods to performan map, reduce,
filter, etc. This object is also iterable. Here's how:

class Functified {
constructor(iterable) {
this.iterable = iterable;
}
```javascript
class Functified {
constructor(iterable) {
this.iterable = iterable;
}

*[Symbol.iterator]() {
for (let value of this.iterable) {
yield value;
}
*[Symbol.iterator]() {
for (let value of this.iterable) {
yield value;
}
}

// various instance and static methods
// various instance and static methods
```

In order to make it easier to write methods on Functified there's also a static
method `fromGenerator(generator)` which takes a generator and returns an iterator.

static fromGenerator(generator) {
return funcitify({
[Symbol.iterator]: generator
});
}

```javascript
static fromGenerator(generator) {
return funcitify({
[Symbol.iterator]: generator
});
}
```

This allows methods to be easily implemented. Here's the implementation for `map`:

map(callback) {
var iterable = this.iterable;
return Functified.fromGenerator(function* () {
for (let value of iterable) {
yield callback(value);
}
});
}
```javascript
map(callback) {
var iterable = this.iterable;
return Functified.fromGenerator(function* () {
for (let value of iterable) {
yield callback(value);
}
});
}
```

## Pausable

Expand All @@ -133,36 +145,40 @@ with those values, and then resume taking values where you left of at some point
in the future. Normally you would have to resort to creating an iterator and
calling `next()` manually, e.g.

var numbers = [1,2,3,4,5];
var iterator = numbers[Symbol.iterator]();

for (let i = 0; i < 2; i++) {
console.log(iterator.next().value);
}

// do something else

while (true) {
let result = iterator.next();
if (result.done) {
break;
}
let value = iterator.next().value;
let square = value * value;
console.log(value * value);
```javascript
const numbers = [1,2,3,4,5];
const iterator = numbers[Symbol.iterator]();

for (let i = 0; i < 2; i++) {
console.log(iterator.next().value);
}

// do something else

while (true) {
const result = iterator.next();
if (result.done) {
break;
}
const value = iterator.next().value;
const square = value * value;
console.log(square);
}
```

The `toPausable()` creates an iterator Below is an example of how this works.

var numbers = [1,2,3,4,5];
var pausableNumbers = numbers.toPausable();

for (let n of pausableNumbers.take(2)) {
console.log(n); // 1 2
}

// do something else

for (let n of pausableNumbers.map(x => x * x).takeUntil(x => x > 16)) {
console.log(n); // 9 16
}
```javascript
const numbers = [1,2,3,4,5];
const pausableNumbers = numbers.toPausable();

for (const n of pausableNumbers.take(2)) {
console.log(n); // 1 2
}

// do something else

for (const n of pausableNumbers.map(x => x * x).takeUntil(x => x > 16)) {
console.log(n); // 9 16
}
```
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "functify",
"version": "0.4.0",
"description": "Add functional methods like map, reduce, filter, etc. iterables (ES6 Iterators).",
"main": "dist/functify.js",
"main": "src/functify",
"module": "src/functify.mjs",
"jsnext:main": "src/functify.mjs",
"directories": {
"test": "test"
},
Expand Down
2 changes: 1 addition & 1 deletion src/functify.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,4 @@ functify.fromGenerator = Functified.fromGenerator;
functify.range = Functified.range;
functify.zip = Functified.zip;

export default functify;
if(typeof module != 'undefined') module.exports = functify;
Loading