Juha Paananen @raimohanska
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 {e => e.title == "manager"}
a paradigm
var active = true;
var timeout = null;
function setInactive() {
bubble("asleep?")
active = false;
}
function setActive() {
if (!active) {
bubble("back!")
active = true;
}
clearTimeout(timeout);
timeout = setTimeout(setInactive, 10000);
}
$(window).on('keydown mousemove', setActive)
setActive()
bubble("Hello")
runreloadreveal
variables, manual timeout mgmt, side-effects
var activityE = $(window).asEventStream('mousemove keydown')
var sleepE = activityE.merge(Bacon.once()).debounce(3000)
var activeP = activityE.map(true).merge(sleepE.map(false))
.toProperty(true).skipDuplicates()
var wakeUpE = activeP.changes().where().equalTo(true)
sleepE.map("asleep?").show()
wakeUpE.map("back!").show()
bubble("hello")
runreloadreveal
an FRP library
Source of events
Observable (subscribe, onValue, onError, onEnd)
map, filter, merge, concat, flatMap
var singleEventStream = Bacon.once("POW")
runreloadreveal
var singleEventStream = Bacon.once("POW");
var countdown = Bacon.sequentially(1000, [5,4,3,2,1,"POW"]);
countdown.show()
var keys = $(document).asEventStream("keyup")
.map(function(e) { return e.keyCode })
.filter(function(key) { return key == 38 }) // UP ARROW
keys.show()
keyups, map, filter "wanna see flatMap"? later, sequentialy flatmap delay throttle
time-varying value
Observable (subscribe, onValue, onError, onEnd)
map, combine
runreloadreveal
var keyState = keyUps(38).map(false)
.merge(keyDowns(38).map(true))
.skipDuplicates()
.toProperty(false)
keyState.show()
runreloadreveal
function fieldValue($field) {
function currentValue() { return $field.val() }
var changesE = $field.asEventStream("keyup");
return changesE.map(currentValue).toProperty(currentValue())
}
var a = fieldValue($("#a")).map(parseInt);
var b = fieldValue($("#b")).map(parseInt);
var c = a.combine(b, function(aVal, bVal) { return aVal + bVal })
c.onValue(function(cVal) {
$("#c").val(cVal)
})
runreloadreveal
var addE = $("#addItem").asEventStream("click")
.flatMap(function() { return $("#itemName").val() })
var removeE = $("#removeItem").asEventStream("click")
var contentsP = Bacon.update([],
addE, function(items, newItem) { return items.concat(newItem) },
removeE, function(items) { return items.slice(1) }
)
contentsP.show()
var checkoutE = $("#checkout").asEventStream("click")
.map(contentsP)
checkoutE.onValue(function(contents) {
bubble("checkout: " + contents)
})