Sponsored Links

Rabu, 22 November 2017

Sponsored Links

OBM - Programming language - Q (programming language from Kx ...
src: i.ytimg.com

Q is a proprietary array processing language developed by Arthur Whitney and commercialized by Kx Systems. The language serves as the query language for kdb+, a disk based and in-memory, column-based database. kdb+ is based upon K, a terse variant of APL. Q is a thin wrapper around K, providing a more readable, English-like interface.


Video Q (programming language from Kx Systems)



Overview

The fundamental building blocks of Q are atoms, lists and functions. Atoms are scalars and include numeric, character, date and time data types. Lists are ordered collections of atoms (or other lists) upon which the higher level data structures dictionaries and tables are internally constructed. A dictionary is a map of a list of keys to a list of values. A table is a transposed dictionary of symbol keys and equal length lists (columns) as values. A keyed table, analogous to a table with a primary key placed on it, is a dictionary where the keys and values are arranged as two tables.

The following code demonstrates the relationships of the data structures (expressions to be evaluated appear prefixed with the "q)" prompt, with the output of the evaluation shown beneath):

These entities are manipulated via functions, which include the built-in functions that come with Q (which are actually defined as K macros) and user-defined functions. Functions are themselves a data type, and can be placed into lists, dictionaries and tables, or passed into other functions as parameters.


Maps Q (programming language from Kx Systems)



Examples

Like K, Q is interpreted and the result of the evaluation of an expression is immediately displayed, unless terminated with a semi-colon. The Hello world program is therefore trivial:

The following expression sorts a list of strings stored in the variable x descending by their lengths:

The expression is evaluated from right to left as follows:

  1. "count each x" returns the length of each word in the list x.
  2. "idesc" returns the indices that would sort a list of values in descending order.
  3. @ use the integer values on the right to index into the original list of strings.

The factorial function can be implemented directly in Q as

or recursively as

Note that in both cases the function implicitly takes a single argument called x - in general it is possible to use up to three implicit arguments, named x, y and z, or to give arguments local variable bindings explicitly.

In the direct implementation, the expression "til x" enumerates the integers from 0 to x-1, "1+" adds 1 to every element of the list and "prd" returns the product of the list.

In the recursive implementation, the syntax "$[condition; expr1; expr2]" is a ternary conditional - if the condition is true then expr1 is returned; otherwise expr2 is returned. The expression ".z.s" is loosely equivalent to 'this' in Java or 'self' in Python - it is a reference to the containing object, and enables functions in q to call themselves.

When x is an integer greater than 2, the following function will return 1 if it is a prime, otherwise 0:

The function is evaluated from right to left:

  1. "til x" enumerate the positive integers less than x.
  2. "2_" drops the first two elements of the enumeration (0 and 1).
  3. "x mod" performs modulo division between the original integer and each value in the truncated list.
  4. "min" find the minimum value of the list of modulo result.

The Q programming language contains its own table query syntax called q-sql, which resembles traditional SQL but has important differences, mainly due to the fact that the underlying tables are oriented by column, rather than by row.

  q)select from t where name like "ja*",age>50  name age  --------  jack 60    q)select rows:count i by age from t  age| rows  ---| ----  20 | 1  50 | 2  60 | 1  

Big Data in Retail - Retail Analytics in Real Time with Kx Technology
src: kx.com


References


kdb/q tutorial on \t - YouTube
src: i.ytimg.com


Further reading

  • Borror, Jeffry A. Q For Mortals: A Tutorial in Q Programming. ISBN 978-1-4348-2901-6. 
  • Psaris, Nick. Q Tips: Fast, Scalable and Maintainable Kdb+. ISBN 978-9-8813-8990-9. 

Kx Systems (@kxsystems) | Twitter
src: pbs.twimg.com


External links

  • Kx Systems
  • Official page for KDB+
  • Online documentation and code samples wiki
  • Online kdb Tutorials
  • qStudio an IDE with timeseries charting for kdb
  • Free online version of "Q for Mortals"
  • q for Gods Whitepaper Series

Source of the article : Wikipedia

Comments
0 Comments