Aya

From Esolang
Jump to: navigation, search

Overview

Aya is a stack based programming language originally intended for code golf and programming puzzles. Unlike other stack-based programming languages, it supports user-defined types, macro-like function definitions, key-value pair dictionaries, and natural variable scoping rules allowing for more complex programs and data structures.

Aya comes with a rapidly-growing standard library written entirely in Aya code. The standard library features types such as matrices, stacks, dates and more. It also features hundreds of functions for working working on numerical computations, strings, plotting and file I/O. It even features a basic turtle library for creating drawings in the plot window.

Aya also features a minimal GUI that interfaces with Aya's stdin and stdout. The GUI features, plotting, tab-completion for special characters, and an interactive way to search QuickSearch help data.

Basic language features

Aya is a stack based language. The code

1 1 +

will return "2".

All lowercase letters are used for variables. The colon (:) operator is used for assignment.

.# This is a line comment
"Hello" :first
"World!" :snd

Almost everything else is an operator. The :P operator will print the item on the top of the stack to stdout.

first " " + snd + :P

Blocks are used to define functions.

{2*}:double
4 double .# will return 8

Blocks may have arguments and local variables. In the example below, a, b, and c are arguments and x, y, and z are local variables.

{a b c : x y z, 
 [a b c] .# a list with a, b and c inside
}:myfun;

the following will call "myfun" and assign 1 to a, 2 to b, and 3 to c within the scope of the function. It will return the list [1 2 3].

1 2 3 myfun

Aya also supports dictionaries. {,} creates an empty dictionary. The operator .: is used for dictionary assignment.

{,} :dict
3 dict.:x
dict.x .# returns 3

Variables may also be assigned within the dictionary literal

{, "hi":a  4:b }:dict
dict.a .# returns "hi"
dict.b .# returns 4

Additional features are outlined in the wiki

Standard library

The Aya standard library consists of type definitions, mathematical functions, string and list operations, plotting tools and even a small turtle graphics library. It also defines functions and objects for working with colors, dates, files, GUI elements, and basic data structures such as queues, stacks, and sets. The standard library also contains a file which defines extended ASCII operators for use when code golfing.

The complete standard library can be explored here.


Examples

Project Euler problem 1

Find the sum of all the multiples of 3 or 5 below 1000.

A golfed version using very few characters:

kVR{15.+!}IS

A version using procedural programming features:

0:sum;
for 'x (999R) {
  x5%0= x3%0= | then {x sum +:sum}
};
sum

Recursive factorial

This first function is written with a C-style syntax. The backtick (`) operator is used to convert postfix operators into infix ones.

`:factorial {n,
  if (n `.< 1) {
    1
  } {
    n `* ( `factorial(n`-1) )
  }
}

Another version:

{n,{n_Vfactorial*}1n1.<?}:factorial;

User defined types

Below is a definition of a simple 2D vector type

{,

 .# Constructor
 {x y, {, x:x y:y} vec MO}:new;

 .# Print Override
 {self, "<$(self.x),$(self.y)>"}:repr;

 .# Member Function
 {self, self.x2^ self.y2^ + Mq}:len;

 .# Operator Overload
 {a b, [a.x b.x+ a.y b.y+] vec MO}:add

}:vec;


Call constructor using ! operator and print using .repr definition:

aya> 1 2 vec!
<1,2>

Perform operations on the type:

aya> 3 4 vec! :v
<3,4>

aya> v.len
5.0

Operator overloading:

aya> 10 10 vec! v +
<13,14>


Basic Ploting

Aya uses JFreeChart to plot expressions and arrays. The standard library file "plot" defines a plot object which is used to easily draw plots.

plot!:p;

.# The domain
0 2pi* 600 linspace p.domain

.# Add each function
"sin" 2 colors.red.rgb    {sin} p.addexpr
"cos" 2 colors.blue.rgb   {cos} p.addexpr
"ln"  2 colors.orange.rgb {ln} p.addexpr

.# Plot title
"A Sample Expression Plot" p.:title

.# Other plot parameters
[(-2) 2] p.:yaxis;
[0 2pi*] p.:xaxis;
1        p.:legend;
"f(x)"   p.:ylabel;
400      p.:width;
300      p.:height;

.# Open the plot window
p.view

.# Uncomment to save the plot
"sample_plot.png" p.save

Output: A Basic Plot


Plot a Lorenz Attractor

.# Starting parameters
0.01 :x;
0.1  :y;
0.3  :z;
10   :a;
28   :b;
8`/3 :c;
0.01 :dt;

.# List to keep track of state
[]

{
  .# Update the point
  [
    y x - a * dt* x+:x
    b z - x * y - dt* y+:y
  ]
  x y * c z * - dt* z+:z;

  .# Append to state list
  \.B
} 5000 %

.# Transpose to separate list of x and y values
transpose ~ plot.line:p

"Lorenz Attractor" p.:title;

p.view

Output: Lorenz Attractor


Additional features

Interactive documentation

Add a ? to a line comment operator .# to add the comment to the interactive help. The interactive help can be searched from within the REPL or IDE and can be used to document source files.

aya> .#? A help comment!\n  This comment will be added to the interactive help
aya> \? help comment
A help comment!
    This comment will be added to the interactive help

Sample documentation from math.aya

{Mp}:primes;        .#? N primes\n  returns a list containing the primes up to and including N
{{*}U}:product;     .#? L product\n  product of a list
{.!}:signnum;       .#? N signnum \n  returns the sign of a number (1,0,-1)
{Mq}:sqrt;          .#? N sqrt\n  square root

External resources