When you read the great book
"Programming Erlang" from Joe Armstrong
there is a chapter about Mnesia. Mnesia is a distributed, soft real-time database
management system written in the Erlang and included in the Erlang distribution.
One great thing, when you looking at the Mnesia queries, it is not SQL or a new exotic own query language.
No, It is pure Erlang and called list comprehension. You have to know only Erlang!
qlc:q([X#shop.item || X <- mnesia:table(shop),
X#shop.quantity < 250,
Y <- mnesia:table(cost),
X#shop.item =:= Y#cost.name,
Y#cost.price < 2
]).
%% SQL equivalent
%% SELECT shop.item, shop.quantity, cost.name, cost.price
%% FROM shop, cost
%% WHERE shop.item = cost.name
%% AND cost.price < 2
%% AND shop.quantity < 2
A list comprehension is a syntactic construct for creating a list based on existing lists.
It can be implemented using map and filter too. But list compressions provide you a simpler to use and more natural syntax.
Today, I realized that the same feature is already included in Javascript since version 1.7.
What a shame ! :)
It called array comprehensions but works similar like list comprehension in Erlang.
Here the equivalent full javascript code:
<script type="application/javascript;version=1.7">
var shop_table = [
{item:"apple", quantity: 20, cost:2.3},
{item:"orange", quantity: 100, cost:3.8},
{item:"pear", quantity: 200, cost:3.6},
{item:"banana", quantity: 420, cost:4.5},
{item:"potato", quantity: 2456, cost:1.2}
];
var cost_table = [
{name: "apple", price: 1.5},
{name: "orange", price: 2.4},
{name: "pear", price: 2.2},
{name: "banana", price: 1.5},
{name: "potato", price: 0.6}
];
var items = [s.item for each (s in shop_table)
for each (c in cost_table)
if (s.quantity < 250 && s.item==c.name && c.price<2)
];
console.log(items);
// Result: ["apple"]
</script>
[1]
https://developer.mozilla.org/en/New_in_JavaScript_1.7#Array_comprehensions
[2]
http://ecmascript4.com/doc/arraycomprehensions
[3]
http://en.wikibooks.org/wiki/Erlang_Programming/List_Comprehensions
[4]
http://www.scribd.com/doc/103818/MNESIA-Database-Management-System