Created
July 17, 2016 12:59
-
-
Save ehlyzov/387133a63bd7c5abcf6dbe1f2a30e0fe to your computer and use it in GitHub Desktop.
FizzBuzz based on nools.js
This file contains 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
'use strict'; | |
const nl = require('nools'); | |
class Message { | |
constructor() { | |
this.text = ''; | |
} | |
set append(text) { | |
this.text = this.text + text; | |
} | |
get value() { | |
return this.text; | |
} | |
} | |
let flow; | |
let session; | |
nl.deleteFlow('FizzBuzz'); | |
flow = nl.flow("FizzBuzz", function(flow) { | |
flow.rule("FizzBuzz", {salience: 3}, [ | |
[Number, 'num', "num % 15 == 0"] | |
], function (facts) { | |
console.log(facts.num, 'FizzBuzz'); | |
this.retract(facts.num); | |
}); | |
flow.rule("Fizz", {salience: 1}, [ | |
[Number, 'num', "num % 3 == 0"], | |
], function (facts) { | |
console.log(facts.num, 'Fizz'); | |
this.retract(facts.num); | |
}) | |
flow.rule("Buzz", {salience: 2}, [ | |
[Number, 'num', "num % 5 == 0"], | |
], function (facts) { | |
console.log(facts.num, 'Buzz'); | |
this.retract(facts.num); | |
}) | |
}); | |
session = flow.getSession(); | |
for (let i = 1; i<100; i++) { | |
session.assert(i); | |
} | |
session.match().then( | |
function(){ | |
console.log("Done"); | |
}, | |
function(err){ | |
//uh oh an error occurred | |
console.error(err.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment