html>
names: (`bob; `carol; `ted; `alice) emp.id: 100 _draw 999999999 / social security numbers emp.name: names[100 _draw 4] emp.salary: 100 _draw 1000000 `show $ `emp / display it
names: (`bob; `carol; `ted; `alice) name: names[100 _draw 4] id: 100 _draw 999999999 / social security numbers salary: 100 _draw 1000000 / now we construct the table emp2: .+(`id`name`salary;(id;name;salary)) / now we display it `show $ `emp2
names: (`bob; `carol; `ted; `alice) name: names[100 _draw 4] id: 100 _draw 999999999 / social security numbers salary: 100 _draw 1000000 emp3: .+(`id`name`salary;(id;name;salary)) id[!10] / returns nothing \d emp3
\v / list elements of the node
id[!10] / returns the first 10 ids name[!10] / returns the first 10 names \d ^ name[!10] / returns nothing \d . / root node
.kFor example,
names: (`bob; `carol; `ted; `alice) name: names[100 _draw 4] id: 100 _draw 999999999 / social security numbers salary: 100 _draw 1000000 \d emp3: .+(`id`name`salary;(id;name;salary)) \d .k.emp3 newtable: .+(`new1 `new2;(!10; 10 + !10)) \d \d .k.emp3.newtable / specifying a location with an absolute path \d
\v
\d ^ / go up \d \d newtable / go back down using relative navigation \d
\v
\d
.dir1.dir2.dir3.v
c:\dir1\dir2\dir3\v
\d dir2.dir3The corresponding command in windows is
cd dir2\dir3
\dThe windows equivalent is
cd
\d ^The equivalent in windows is
cd ..
\d . / go back to root i: & emp3.salary > 123000
emp3.name[i]
# & emp3.salary > 250000
i: < emp3.salary emp3.salary[i] / salaries in order emp3.name[i] / names ordered by salary
part: = emp3.name / list of entries, each holding / an indexes of everyone with same name.
r.people: ? emp3.name
r.sumofsalaries: +/'emp3.salary[part] / where the +/' means sum over each / partition.
i: < emp3.salary midindex: _ (#i) % 2 / floor of the middle midsal: emp3.salary[i[midindex]] j: &emp3.salary = midsal emp3.name[j]
purchase.date: (!10), (|!5) purchase.item: (10 # `candy), (5 # `toys) purchase.qty: 100 + 15 _draw 400 purchase.price: 20 + !15form the itempurchase table which holds each item with all the purchase, qty and price information.
itempurchase.item: ? purchase.item / remove duplicates part: = purchase.item / gives itemsets in the same order as above itempurchase.qtys: purchase.qty[part] itempurchase.price: purchase.price[part] itempurchase.date: purchase.date[part]
purchase.date: (!10), (|!5) purchase.item: (10 # `candy), (5 # `toys) purchase.qty: 100 + 15 _draw 400 purchase.price: 20 + !15 sold.item: `candy `toys sold.qty: 123 756 result.item: () result.cost: ()
/ given an item, the quantity sold, the quantities bought in order / and the prices of those purchases, / produce the cost of selling that item. cogs:{[item; soldqty; boughtvec; pricevec] scanqty: +\boughtvec i: (soldqty < scanqty) ? 1 / finds the first point when that holds if[i > 0 cost: +/ boughtvec[!i] * pricevec[!i] cost+: (soldqty-scanqty[i-1]) * pricevec[i] ] if[i = 0 cost: soldqty*pricevec[0] ] result.item,: item result.cost,: cost } / takes an index of the sold table. setup:{[sellindex] item: sold.item[sellindex] soldqty: sold.qty[sellindex] i: & purchase.item = item boughtvec: purchase.qty[i] pricevec: purchase.price[i] dates: purchase.date[i] j: < dates boughtvec: boughtvec[j] pricevec: pricevec[j] cogs[item;soldqty;boughtvec;pricevec] } setup'!#sold.item `show $ `sold `show $ `result `show $ `purchase
/ sale(saleid, amount, district, salesagent) size: 10 sale.saleid: !size sale.amount: 5000 + size _draw 100000 districts: `rich `posh `extravagant sale.district: districts[size _draw 3] salesagents: `alice `bob `carol `fred sale.salesagent: salesagents[size _draw 4] `show $ `sale
/ I. Find all sales by salesagent fred. i: & sale.salesagent = `fred / Find the indexes of sales by fred. +sale[;i] / the initial + puts it in record format / II. Find the sum of sales by district. part: = sale.district +(?sale.district; +/'sale.amount[part]) / III. Find the sum of sales by district of fred. i: & sale.salesagent = `fred part: = sale.district[i] / Among those in i, find groups with the same district. +(?sale.district[i]; +/' sale.amount[i][part]) / IV. Find the top three sales per district. / If there are fewer than three sales in a district, / then don't give any of them. top:{[number; district] i: & sale.district = district / find the indexes in that district amounts: sale.amount[i] / find the sales there orderedvec: amounts[> amounts] / sort by amount :[number < #orderedvec / depending on number in result, take the top district, ,orderedvec[!number] district, ,orderedvec]} top[3]'?sale.district / V. Find the sum of sales by district and sales agent / select salesagent, district sum(sale) / from sale / group by salesagent, district part: = sale.salesagent,' sale.district / need the each to get all pairs groupers: ? sale.salesagent ,' sale.district groupers ,' +/'sale.amount[part] / VI. Find pairs of salesagents who work in the same district. / select s1.district, s1.salesagent, s2.salesagent / from sale s1, sale s2 / where s1.district = s2.district findcross:{[district] i: & sale.district = district all: sale.salesagent[i] ,\:/: sale.salesagent[i] allnodups: ?,/all district ,/: allnodups} findcross'?sale.district
/ finds the non-duplicated union of two lists union:{[x;y] ?x,y} /finds intersection of two lists intersect:{[x;y] x[& x _in\: y]} /finds set difference of two lists x - y differ:{[x;y] x[&~ x _in\: y]} / select r1 from r1, r2 where r1.a1 = r2.a2 / This is for a foreign key join with r2.a2 as the key field, / so there is at most one r2 tuple with a given a2 value. foreignKeyJoin:{[r1;a1;r2;a2] r1[;& r1[a1] _in\: r2[a2]]} subset:{[subList; superList] &/ subList _lin superList}
jack 23 45.633 jill 24 55.33 ed 4 5.33 ellen20 444.3 jack 23 45.633 jill 24 55.33 ed 4 5.33 ellen20 444.3 jack 23 45.633 jill 24 55.33 ed 4 5.33 ellen20 444.3 jack 23 45.633 jill 24 55.33 ed 4 5.33 ellen20 444.3 jack 23 45.633 jill 24 55.33 ed 4 5.33 ellen20 444.3
/ k import infile / infile is for text and outputs are fieldnames infile: _i[0] / take input from this file size: 30 / chunk size for importing process:{[infile;start;size; types; sizes] while[#*b:(types; sizes) 0:(infile;start;size) / while more data (fieldnames) 5:' b start+:size ] /increment starting byte } fieldnames: `id `age `salary fieldnames 1:' 0 #' (`;0;0.0) / initialize this types: "SIF " / last one is for the carriage return/linefeed fieldsizes: 5 6 8 1 / field widths, the last 1 is for carriage return. / calculate size to be an integral number of fieldsizes multiplier: _ size % (1 + +/fieldsizes) / 1 is for carriage return/linefeed size: multiplier * (1 + +/fieldsizes) process[infile;0;size;types;fieldsizes]