The following demonstrates the usage of the Bacon.js library in implementing a Node.js chat application in the functional reactive programming style. We'll start by re-implementing the canonical Socket.IO chat application in an FRP-idiomatic way, moving on to stream composition, sampling, debouncing, error handling and logging.
This file contains hidden or 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
var connections = Bacon.fromBinder(function(sink) { | |
io.on('connection', sink) | |
}); |
This file contains hidden or 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
var request = require('request-json'), | |
express = require('express'), | |
app = express(), | |
http = require('http').Server(app), | |
io = require('socket.io'), | |
db = require('mongodb').MongoClient; | |
/////////////////////////////////////////////////////////// | |
// boilerplate code: ignore | |
// |
This file contains hidden or 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
var request = require('request-json'), | |
express = require('express'), | |
app = express(), | |
http = require('http').Server(app), | |
io = require('socket.io'), | |
Bacon = require('baconjs').Bacon, | |
client = request.newClient('http://localhost:3000/api/'), | |
db = require('mongodb').MongoClient; | |
/////////////////////////////////////////////////////////// |
This file contains hidden or 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
/* | |
We'll be building a chat application for Seattle-based weather aficionados. | |
On top of basic chat functionality (sending messages to a chat room, being | |
notified when users enter/leave the room), the application will provide two | |
interesting, user-facing features: | |
1. New users, upon connection to the server, will be sent a private message | |
containing a summary of the current weather in Seattle. |
This file contains hidden or 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
class Prunable | |
THIRTY_DAYS_AGO_SEC = 60 * 60 * 24 * 30 | |
def self.from_assets(exclusion_time, assets) | |
assets.select do |asset| | |
self.is_prunable?(exclusion_time, asset) | |
end | |
end | |
def self.is_prunable?(exclusion_time, asset) |
This file contains hidden or 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
module Main where | |
import Control.Applicative | |
-- represents our API - its constructor takes two functions | |
-- | |
data API = API { apiDouble :: Integer -> Integer, apiTriple :: Integer -> Integer } | |
-- run a pure function as a computation in the IO monad. | |
-- note that the function won't be run until you bind the |
- implement a function "foldFromRight" which takes:
- a function with two parameters (a "binary function")
- a starting value (the accumulator)
- a list of values to reduce into a single value
The behavior of your foldFromRight function will be similar to "reduce" in JavaScript and Ruby - except for the fact that it will move from the end of the list to the beginning (instead of from the beginning to the end).
This file contains hidden or 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
module Main where | |
-- essentially we're defining functions that makes sense to | |
-- call with things that can be doubled. it can be, well, | |
-- a value of any type (as long as that type instantiates | |
-- the Dubbleable type class). Dispatch can happen by virtue | |
-- of what you're going to do with the return value of the | |
-- function or what you're passing to the function as its | |
-- arguments | |
-- |
This file contains hidden or 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
struct MatchSearch { | |
combineId int [optional] | |
} | |
struct Match { | |
id int | |
name string | |
startsAt int | |
teams []Team | |
heats []Heat |