Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // example of input user
- let user1 = {
- access: [true, false],
- utility: [2, 5]
- };
- // example of input object
- let object1 = {type: 0, cost: 15};
- // static vertions of the cost function
- const costFunction = (user, object) => {
- switch (object.type){
- case 0:
- if(user.access[0]){
- return object.cost * user.utility[0];
- }else{
- return Infinity;
- };
- case 1:
- if(user.access[1]){
- return object.cost * user.utility[1];
- }else{
- return Infinity;
- };
- };
- };
- costFunction(user1, object1) // sould return 2*15 = 30
- // dynamically generated a user-adapted const function
- const costFunctionGenerator = (user) => {
- ... some code ...
- const costFunction = (object) => {
- ... some code ...
- };
- return costFunction;
- };
- const user1CostFunction = costFunctionGenerator(user1)
- const user1CostFunction = (object) => {
- switch (object.type){
- case 0:
- return object.cost * 2;
- case 1:
- return Infinity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement