Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
added README and example
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Dec 27, 2011
1 parent eb253da commit a571e7b
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
120 changes: 120 additions & 0 deletions README.md
@@ -0,0 +1,120 @@
( ( (emit) ) )
================

(emit) is a [functional reactive programming][frp] toolkit based on JavaScript primitives, allowing you to compose functional applications using the APIs you already know.

(emit) is a very rough work in progress. It can run both in the browser and on [node.js][node].

Example
-------

```javascript
emit = require("emit")

// emit method APIs are identical to their native counterparts...
Math.pow(2, 2) // 4
emit.Math.pow(2, 2) // 4

Array(1, 2, 3) // [1, 2, 3]
emit.Array(1, 2, 3) // [1, 2, 3]

1 / 2 // .5
emit.ops["/"](1, 2) // .5

// with one exception: emit methods can also take functions, which are
// treated as yet-unevaluated values that can be "watched". All emit
// methods compose these (possibly unevaluated) values into a single
// value; if any of these values are functions, the returned value will
// also be a function.

// Here's an example of such a function; it reports an incremented number
// back to its listener every second.
function count(cb) {
var send = function(){ cb(null, i++) }
, i = 0

send()
setInterval(send, 1000)
}

// Since the value reported by count changes every second, so does
// the value of any method that uses it. So these are all functions:
emit.Math.pow(2, count) // [Function]
emit.Array(1, 2, count) // [Function]
emit.ops["/"](1, count) // [Function]

// We can observe these changing values through the console.
function log(e, data){ console.log(e || data) }

emit.Math.pow(2, count)(log) // 1, 2, 4, 8, 16, 32...
emit.Array(1, 2, count)(log) // [1, 2, 0], [1, 2, 1], [1, 2, 2]...
emit.ops["/"](1, count)(log) // Infinity, 1, .5, .25...

// Now we can now start to compose these into increasingly
// complex values.
random = emit.Math.random(ping)
randBin = emit.Math.round(random)
coinToss = emit.ops["?:"](randBin, "heads", "tails")
message = emit.ops["+"]("The coin landed on ", coinToss)

message(log) // "The coin landed on heads"
// "The coin landed on tails"
// "The coin landed on heads"
// ...
```

Installation
------------

To install, head to your terminal and enter:

npm install emit

Support
-------

### Current

- `Array`: all ES3/ES5 methods
- `Function`: `apply`, and `call`
- `Math`: all statics and function
- operators: most accessors, including `!`, `~`, `typeof`, `void`, `*`, `/`, `%`, `+`, `-`, `<<`, `>>`, `>>>`, `<`, `<=`, `>`, `>=`, `==`, `!=`, `===`, `!==`, `&`, `^`, `|`, `&&`, `||`, `, `, `?:`, `[]`, and `in`

### Planned

- RegExp, String, Date, Number, Boolean
- Function::bind
- DOM/HTML
- DOM events
- JSON
- node.js file system, etc.

License
-------

Copyright (c) 2012 Jed Schmidt, http://jed.is/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[frp]: http://en.wikipedia.org/wiki/Functional_reactive_programming
[node]: http://nodejs.org/
[flapjax]: http://www.flapjax-lang.org/
[tangle]: http://worrydream.com/Tangle/
[spreadsheet]: http://en.wikipedia.org/wiki/Spreadsheet
5 changes: 5 additions & 0 deletions index.js
@@ -0,0 +1,5 @@
exports.Array = require("./lib/Array")
exports.ops =
exports.operators = require("./lib/operators")
exports.Function = require("./lib/Function")
exports.Math = require("./lib/Math")
52 changes: 52 additions & 0 deletions test.js
@@ -0,0 +1,52 @@
emit = require("emit")

// emit method APIs are identical to their native counterparts...
Math.pow(2, 2) // 4
emit.Math.pow(2, 2) // 4

Array(1, 2, 3) // [1, 2, 3]
emit.Array(1, 2, 3) // [1, 2, 3]

1 / 2 // .5
emit.ops["/"](1, 2) // .5

// with one exception: emit methods can also take functions, which are
// treated as yet-unevaluated values that can be "watched". All emit
// methods compose these (possibly unevaluated) values into a single
// value; if any of these values are functions, the returned value will
// also be a function.

// Here's an example of such a function; it reports an incremented number
// back to its listener every second.
function count(cb) {
var send = function(){ cb(null, i++) }
, i = 0

send()
setInterval(send, 1000)
}

// Since the value reported by count changes every second, so does
// the value of any method that uses it. So these are all functions:
emit.Math.pow(2, count) // [Function]
emit.Array(1, 2, count) // [Function]
emit.ops["/"](1, count) // [Function]

// We can observe these changing values through the console.
function log(e, data){ console.log(e || data) }

emit.Math.pow(2, count)(log) // 1, 2, 4, 8, 16, 32...
emit.Array(1, 2, count)(log) // [1, 2, 0], [1, 2, 1], [1, 2, 2]...
emit.ops["/"](1, count)(log) // Infinity, 1, .5, .25...

// Now we can now start to compose these into increasingly
// complex values.
random = emit.Math.random(count)
randBin = emit.Math.round(random)
coinToss = emit.ops["?:"](randBin, "heads", "tails")
message = emit.ops["+"]("The coin landed on ", coinToss)

message(log) // "The coin landed on heads"
// "The coin landed on tails"
// "The coin landed on heads"
// ...

0 comments on commit a571e7b

Please sign in to comment.