On Github raimohanska / bacon-mloc
Juha Paananen / Reaktor
List managers = new ArrayList()
for (Employee employee : employees) {
if (employee.title.equals("manager")) {
managers.add(e);
}
}
return managers;
variables, loops, ifs, mutable data structures
employees.filter(_.title == "manager")
a paradigm
$.ajax("/cats").done(function(cats) {
$.ajax("/dogs").done(function(dogs) {
doStuffWithCatsAndDogs(cats, dogs)
})
})
Imagine adding giraffes and bats
var active = true;
var timeout = null;
function setInactive() {
active = false;
}
$(window).on('keydown mousemove', function() {
active = true;
clearTimeout(timeout);
timeout = setTimeout(setInactive, 30000);
})
variables: 2
a library
Source of events
Observable
map, filter, merge, concat, flatMap
keyups, map, filter "wanna see flatMap"? later, sequentialy flatmap delay throttle
time-varying value
Observable
map, combine, flatMap
var cats = Bacon.UI.ajax("/cats")
var dogs = Bacon.UI.ajax("/dogs")
Bacon.combineAsArray(cats, dogs)
.onValues(doStuffWithCatsAndDogs)
function get(name) { return Bacon.UI.ajax(name) }
var zoo = Bacon.combineTemplate({
mammals: {
cats: get("cats"),
dogs: get("dogs")
},
fish: {
sharks: get("sharks"),
whales: get("whales")
}
})
{
mammals: {
cats: ["Garfield", "Hercules"]
dogs: ["Yo Dawg"]
},
fish: {
sharks: ["Jaws"],
whales: ["I'm mammal!"]
}
}
var activity = $(window).asEventStream('mousemove keydown')
var timeout = activity.throttle(30000)
var active = activity.awaiting(timeout)
// a real one, but stripped
function TodoListModel() {
// todoAdded :: Bus Todo
this.todoAdded = new Bacon.Bus()
this.todoModified = new Bacon.Bus()
this.todoDeleted = new Bacon.Bus()
// allTodos :: Property [Todo]
this.allTodos
this.activeTodos
this.completedTodos
}
// a real one, but stripped
function TodoListModel() {
this.todoAdded = new Bacon.Bus()
this.todoModified = new Bacon.Bus()
this.todoDeleted = new Bacon.Bus()
// changes :: EventStream ([Todo] -> [Todo])
var changes
this.allTodos = changes.scan([], function(todos, f) {
return f(todos)
})
}
baconjs.blogspot.com
function NewTodoView(element, model) {
// todoAdded :: EventStream Todo
var todoAdded;
model.todoAdded.plug(todoAdded);
}
baconjs.blogspot.com
function TodoApp() {
var model = new TodoListModel();
var hash = Bacon.UI.hash('#/');
new FilterView($('#filters'), hash);
new TodoListView($('#todo-list'), model, hash);
new NewTodoView($('#new-todo'), model);
new ClearCompletedView($('#clear-completed'), model);
new TodoCountView($('#todo-count'), model);
new ToggleAllView($('#toggle-all'), model);
}
baconjs.blogspot.com