In this chapter:

Book: The Judo Language 0.9

Chapter 8. Threads and Dynamic Evaluation

By James Jianbo Huang


non-printer version

Not done yet. For now, please read this article.

 
Runtime Context   to be done

a

b


 
Thread Programming   to be done


 
Execute Dynamic and External Scripts   to be finished

You can dynamically create Judo code and run. There are a couple of either in the current runtime context with eval command or in a separate context with evalExternal and evalFile. evalFile takes a file name instead of code.

Listing 8.1 eval.judo
x = 'aliens';
eval [[*
  println 'Hello, you ', x, '!';
  y = 9;
*]];
println 'y was set within eval; now, y = ', y, nl;

evalExternal "println 'Hello, you ', x, '!'; ";

eval 'println "ABCDEFG"';

When the dynamic code is evaluated with eval, the declarations such as functions, classes and constants are taken into the current runtime context. evalExternal and evalFile do not.

eval [[*
  const #acon = 'a';
  function foo() { println 'From foo()'; }
  class BarCls { constructor { println 'BarCls.ctor()'; } }
*]];

println '#acon = ', #acon;
foo();
new BarCls;

evalExternal and evalFile can take parameters using the with clause:

code = [[*
  if #args.length < 0 {
    println <err> 'Usage: java judo ', #prog, ' x';
    exit 0;
  }

  println 'The argument is: ', #args[0];
*]];
evalExternal code;
evalExternal code with 'abcd';

The result is:

Usage: java judo <string> x
The argument is: abcd

The following is an example. Last July, Rainbow Inc., a retail company, ramped up its e-commerce presence. This July, Mr. Smart is hired to head the marketing department. He devised a sophisticated pricing scheme to promote certain products. Fortunately, the consultants who designed the system used Judo's dynamic evaluation feature, so Mr. Smart's unforeseeable request is entered into the system without any code rewrite.

Listing 8.2 new_pricing.judo
 1: // pre-Mr.Smart pricing -- flat
 2: pricing = lambda prod, quantity {
 3:              switch prod {
 4:              case 'candy': return 1.49 * quantity;
 5:              case 'chips': return 2.49 * quantity;
 6:              case 'beer':  return 2.99 * quantity;
 7:              default:      return 0.99 * quantity;
 8:              }
 9:            };
10:
11: println '10 candies: ', pricing('candy', 10);
12:
13: // post-Mr.Smart pricing -- quantity discount
14: eval [[[*
15:   pricing = lambda prod, quantity {
16:                switch prod {
17:                case 'candy': unitprice = 1.49; break;
18:                case 'chips': unitprice = 2.49; break;
19:                case 'beer':  unitprice = 2.99; break;
20:                default:      unitprice = 0.99; break;
21:                }
22:                if quantity <= 5 {
23:                  return unitprice * quantity;
24:                } elif quantity <= 10 {
25:                  return (unitprice * 0.95 * quantity).fractionDigits(2);
26:                } else {
27:                  return (unitprice * 0.90 * quantity).fractionDigits(2);
28:                }
29:              };
30: *]];
31:
32: println ' 5 candies: ', pricing('candy', 5);
33: println '10 candies: ', pricing('candy', 10);
34: println '15 candies: ', pricing('candy', 15);

What happens is, the software has a admin-tool that allows a human operator to type in the new pricing scheme (lines 15 through 29) and submit to the system, test out and commit the changes.


 
back to top
 


Copyright c 2001-2021 JudoScript.COM. All Rights Reserved.