The preface

Abstract: RECENTLY I have been writing a scripting language, the main object is to run dry set calculator, Python, etc. It has certain application scenarios, but the way similar to Excel seriously affects the acceptability of programming. In addition, some of its syntax feels very inconsistent with Java habits, so I wrote one myself, which is currently under intense verification, and will be completely open source after verification. Welcome mark, who is interested in playing with us. Finally open source git address is: git.oschina.net/tinyframewo… If fork exceeds 1000+, open source immediately. The open source agreement is Apache V2.

run

We provide Eclipse and Idea plug-ins, you can directly run the tinyscript extension of the file oh:

The sample

99 tables

for(i=1; i<=9; i++){ for(j=1; j<=i; j++){ printf("%d*%d=%d\t",i,j,i*j); } println(); }Copy the code

The results

1 * 1 = 1 2 * 2 * 2 = 1 = 2 3 4 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 4 * 4 = 3 = 12 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 5 * 5 = 20 6 * 1 = 6 * 2 = 12 * 3 = 18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81Copy the code

We support simplified println, which you can use in Java, such as system.out.println.

An array of

,2,4,9,6,3 ABC = {1}; println(max(abc)); println(min(abc)); println(avg(abc)); println(median(abc)); println(varp(abc)); println(stdevp(abc)); def={"aa","bb","cc"}; println(max(def));Copy the code

The results

9 1 4.1666666666667 3.5 7.138888888888889 2.6718699236468995 ccCopy the code

The list of

A = [1, 2, 3, 4, 5]; B =,3,4,6 [2]; println(a-b); println(a+b); println(a&b); println(a^b); c=["abc","def","gg"]; d=["abc","def1","gg1"]; println(c+d); println(c-d); println(d-c);Copy the code

The results

[1, 5]
[1, 2, 3, 4, 5, 6]
[2, 3, 4]
[1, 5, 6]
[abc, def, gg, def1, gg1]
[def, gg]
[def1, gg1]Copy the code

There could be more fun moves, too

A = [1, 2, 3]; println(a); a++; println(a); a*=3; println(a); b=["abc","def","ghijk"]; println(b.length());Copy the code
[1, 2, 3]
[2, 3, 4]
[6, 9, 12]
[3, 3, 5]Copy the code

Of course, lambda is also possible:

add(a,b)->{ return a+b; }; System. The out. Println (add (2, 4));Copy the code

Running results:

6Copy the code

Java syntax can be used directly:

import java.util.*; List =,2,3,9,8,7 [1]; Collections.sort(list); println(list);Copy the code

Running results:

[1, 2, 3, 7, 8, 9]Copy the code

Of course, the results in a scripting language can also be used perfectly in Java without any sense of violation.

All Java properties and methods can be called directly:

A = [1, 2, 3, 4, 5]; println(a.getClass().getName()); b=[]; for(i:a){b.add(i*i)}; println(b);Copy the code

Running results:

java.util.ArrayList
[1, 4, 9, 16, 25]Copy the code

Again, it is completely consistent with Java, and in that sense it can be an extension of AVA.

Of course, I’m not doing it for fun, I’m doing it for simplicity, and the “#” here represents a specific element of the set element

A = [1, 2, 3, 4, 5]; println(a.filter(#%2==1)); println(a.filter(#%2==0)); println(a.filter(#>3&&#%2==0));Copy the code

Running results:

[1, 3, 5]
[2, 4]
[4]
Copy the code

Filter the list by taking odd numbers, even numbers, and even numbers greater than 3.

Since the following expressions can be written arbitrarily, there are very good scenarios:

class User{ age,name; } userList=[]; for(i=1; i<9; i++){ user=new User(); user.name="user"+i; user.age=i; userList.add(user); } filterList=userList.filter(#.age>3&&#.age%2==1); println(filterList);Copy the code

Running results:

[User[name=user5,age=5], User[name=user7,age=7]]Copy the code

The logic above is to fetch a list of users older than 3 with an odd age.

Map

map={"abc":1,"aaa":2,"ddd":4};
map1={"abc":1,"aaa":2,"bbb":3};
println(map);
println(map.get("abc"));
println(map+map1);
println(map^map1);
println(map&map1);
println(map1-map);Copy the code

Running results:

{aaa=2, abc=1, ddd=4}
1
{aaa=2, abc=1, bbb=3, ddd=4}
{bbb=3, ddd=4}
{aaa=2, abc=1}
{bbb=3}Copy the code

The whole arrangement

Ele = [1, 2, 3]; for(i:ele){ ele.permute(i,(e) -> { println(e); }); println("=============="); }Copy the code

The results

[1] [2] [3] = = = = = = = = = = = = = = [1, 1] [1, 2], [1, 3], [2, 1] [2, 2], [2, 3], [3, 1], [3, 2) (3, 3) = = = = = = = = = = = = = = [1, 1, 1] [1, 1, 2] [1, 1, 3] [1, 2, 1] [1, 2, 2] [1, 2, 3] [1, 3, 1] [1, 3, 2] [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2] [3, 1, 3], [3, 2, 1], [3, 2, 2] [3, 2, 3], [3, 3, 1], [3, [3, 3, 3] ==============Copy the code

Of course, you can do things with permutations

Daffodil number

class Narcissus{ compute(num){ for(i=100; i<num; i++){ a=i/100; b=(i-a*100)/10; c=i-a*100-b*10; if(pow(a,3)+pow(b,3)+pow(c,3)==i){ System.out.println(i); } } } } narcissus=new Narcissus(); narcissus.compute(999);Copy the code

Running results:

153
370
371
407
Copy the code

And you can see that the normal algorithm looks something like this.

The algorithm for using full permutation is as follows:

Elements =,1,2,3,4,5,6,7,8,9 [0]; elements.permute(3,(e) -> { value=e[1]*100+e[2]*10+e[3]; if(pow(e[1],3)+pow(e[2],3)+pow(e[3],3)==value){ System.out.println(value); }});Copy the code

Running results:

0
1
153
370
371
407Copy the code

If 000=0 and 001=1, then 0 and 1 can also be counted as qualified.

Rock-paper-scissors game

Info =[" rock "," scissors "," paper "]; for(i=0; i<10; i++){ mine=randInt()%3; your=randInt()%3; If (mime = = your) {printf (" flat, is % s \ n ", the info/mime + 1); } else {the if (mime = = your 1 | | mime = = 2 && your = = 0) {printf (" I win, I'm a % s is % s \ n "you, info [mime + 1], the info [your + 1)); } else {printf (" I lose, I'm a % s is % s \ n "you, info [mime + 1], the info [your + 1)); }}}Copy the code

Running results:

I lose, I'm paper you're scissors I lose, I'm scissors you're rock flat, it's all rock I win, IT's all scissors you're paper flat, it's all scissors I lose, it's all paper flat, it's all rock I lose, it's all paper flat, it's all rock I lose, it's all paper flat, it's all rock I lose, it's all paper flat, it's all rock I lose, it's all paper flat, it's all rock I loseCopy the code

There seems to be no problem playing with ordinary algorithms.

Class supports

class User { name,age; User(name,age){ this.name = name; this.age =age; } User(name){ this.name = name; }} user = new user (" user ",18); user2 = new User("tom"); user3 = new User(); // No argument constructor println(user); println(user2); println(user3);Copy the code

Running results:

User[name= red,age=18] User[name= Tom,age=null] User[name=null,age=null]Copy the code

Count stocks that have risen by the daily limit for three consecutive days

The following is the daily closing price record of a stock exchange in one month, where CODE is the stock CODE, DT is the date, and CL is the closing price. Try to find stocks that have traded by the daily limit three days in a row this month. To avoid rounding errors, the limit was set at 9.5%.

Please refer to the following figure for partial data (please refer to the attachment stockRecords.txt for complete records, and the following examples also comply with this specification)

CountStock (path) {ratio = 0.095d; countStock(path) {ratio = 0.095d; ds = readTxt(path); groupds =ds.insertColumn(3,"UP").convert(CL,"double").group(CODE).sortGroup("DT ASC"); Groupds. SubGroup (1, 1). The update (UP to 0 d); / / the first day of every month, harden rate was 0 groupds. Update (UP, (CL [0] - CL [1])/CL [1]). // Then count the daily limit rate of the day. resultds = groupds.filterGroup(UP[0]>ratio && UP[1]>ratio && UP[2]>ratio); return resultds; } } m = new Example1(); groupDs= m.countStock(\"src/test/resources/StockRecords.txt\"); for(int i=0; i<groupDs.getRows(); I++) {println (" code = "+ groupDs. GetData (I + 1, 1)); }Copy the code

The code is a bit complicated this time, so here’s how:

Set daily limit growth rate of 0.095 and above.

To read data from a text file, or from a database, change it to the following:

ds=[[select * from stockRecords]];Copy the code

Then add a column, convert the CL column to a double, group by CODE, and sort by DT.

Then set the first day’s growth rate for each stock to zero, and calculate the subsequent growth rate.

Then filter 3 days of continuous growth rate is greater than the stock trading limit.

Running results:

code=201745
code=550766
code=600045
code=700071Copy the code

Calculate postage on orders

A B2C website needs to calculate the shipping cost of an order. In most cases, the shipping cost is determined by the total weight of the package, but when the price of an order exceeds $300, it offers free shipping. The detailed rules are shown in the mailCharge table below:

FIELD MINVAL MAXVAL CHARGE
COST 300 1000000 0
WEIGHT 0 1 10
WEIGHT 1 5 20
WEIGHT 5 10 25
WEIGHT 10 1000000 40

This table records the postage of each field in a range of values. For example, the first record indicates that when the cost field is between 300 and 1000000, the postage is 0 (free shipping). The second record says that when the weight field is between 0 and 1 (kg), the postage is 10 (US $).

A B2C website needs to calculate the shipping cost of an order. In most cases, the shipping cost is determined by the total weight of the package, but when the price of an order exceeds $300, it offers free shipping. The detailed rules are shown in the mailCharge table below:

FIELD MINVAL MAXVAL CHARGE
COST 300 1000000 0
WEIGHT 0 1 10
WEIGHT 1 5 20
WEIGHT 5 10 25
WEIGHT 10 1000000 40

This table records the postage of each field in a range of values. For example, the first record indicates that when the cost field is between 300 and 1000000, the postage is 0 (free shipping). The second record says that when the weight field is between 0 and 1 (kg), the postage is 10 (US $).

Here are some orders from the site:

ID COST WEIGHT(KG)

JOSH1

150 6
    DRAKE 100 3
    MEGAN 100 1
    JOSH2 200 3
    JOSH3 500 1

Please calculate the postage details for these orders.

Class Example2 {/* Count postage for different orders according to the rule */ countMailCharge(path1,path2){ruleDs = readTxt(path1).convert(MINVAL,"int").convert(MAXVAL,"int").convert(CHARGE,"int"); orderDs = readTxt(path2).convert(COST,"int").convert(WEIGHT,"int").insertColumn(4,"POSTAGE"); costRuleDs = ruleDs.filter(FIELD=="COST").sort("MINVAL"); //cost dimension rule set weightRuleDs = ruleDs. Filter (FIELD=="WEIGHT").sort("MINVAL"); OrderDs. Match (costRuleDs,COST > MINVAL). Update (POSTAGE,CHARGE); // Match the COST rule orderds. match(weightRuleDs,POSTAGE ==null && WEIGHT > MINVAL && WEIGHT <=MAXVAL).update(POSTAGE,CHARGE); // Match the WEIGHT rule with the match function return orderDs; } } m = new Example2(); resultDs=m.countMailCharge(\"src/test/resources/mailCharge.txt\",\"src/test/resources/testOrder.txt\"); for(int i=0; i<resultDs.getRows(); i++){ resultDs.absolute(i+1); println("POSTAGE="+resultDs.getData("POSTAGE")); }Copy the code

Running results:

POSTAGE=25
POSTAGE=20
POSTAGE=10
POSTAGE=20
POSTAGE=0Copy the code

Count big sales accounts

The following is the historical sales contract record of an enterprise:

The following is the customer information table of an enterprise:

The customers ranked by sales volume in a certain year and the top N customers that account for half of the sales volume are called “big customers” in that year. Please list the big customers of this enterprise in 1998.

Class Example3 {/* Counting big customers in a certain year */ countVip(){contractDs = readTxt("src/test/resources/Contract.txt").convert(Amount,"double"); clientDs = readTxt("src/test/resources/Client.txt"); firstDs = contractDs.filter(SellDate.startsWith("1998")).join(clientDs,Client = ID); GroupDs = firstds.select ("Name","Amount").group(Name); Groupds.sumgroup (Amount); // Keep the calculation fields and group them by user. HalfAmount = groupds.sort ("sumGroup_Amount desc"). Sum (sumGroup_Amount)/2; list = new java.util.ArrayList(); ForEach ((I) -> {halfAmount -= sumGroup_Amount; if(halfAmount>0){ list.add(Name); }}); return list; } } m = new Example3(); obj=return m.countVip(); for(Object obj:list){ println("name="+obj); }Copy the code

Running results:

Name = Quick-stop name=Save-a-lot Markets name=Ernst Handel name= Mere Paillarde name=Hungry Owl All-night Grocers name=Rattlesnake Canyon Grocery name=Simons bistro name=Berglunds snabbk? p name=Lehmanns Marktstand name=HILARION-Abastos name=Folk och f? HBCopy the code

Collect customer account balance

There are four tables involved, the first is the Customers table, which records customer ids and customer account balances:

The second table, Orders, records each order and its customers:

The third table, OrderDetails, records the details of each order, including the product ID and quantity ordered:

The last table, Products, records all the product information of the enterprise:

The problem is to find the average acct_balance (account balance) for each customer who orders all of the company’s products and for each customer who does not order all of the products.

Class Example4 {/* Count customer account balances for ordered products */ countBalance(){customerDs = readTxt("src/test/resources/Customers.txt").convert(acct_balance,"double"); orderDs = readTxt("src/test/resources/Orders.txt"); orderDetailDs = readTxt("src/test/resources/OrderDetails.txt"); productDs = readTxt("src/test/resources/Products2.txt"); tempDs = orderDetailDs.join(orderDs,order_id=order_id).join(customerDs ,customer_id=customer_id).copy().select("customer_id","order_id","item_id","acct_balance"); GroupDs = tempds. group(customer_id); DistinctGroup (item_id); // groupDs. DistinctGroup (item_id); Num = productds.getrows (); AllProductDs = groupds.filterGroup (distinctGroup_item_id. Size ()==num); OtherDs = groupds.filterGroup (distinctGroup_item_id. Size () < num); Balance1 = allProductDs. Avg (acct_balance); balance2 = otherDs.avg(acct_balance); System.out.println("balance1="+balance1+" balance2="+balance2); } } m = new Example4(); m.countBalance();Copy the code

Running results:

Balance1 balance2 = 4990.5 = 7363.875Copy the code