Here are some examples to give you a brief overview and a feel for the Axiom language.
print 'hello world!'
:hello(name) print 'hello',name+'!' hello('aaron') #output: hello aaron!
@cat num = 0 #no need to declare "self" everywhere (like in Python) :new() this.id = num++ :meow() print 'Cat #'+this.id,'says meow!' #no need to declare "static" explicitly (like in C++/Python) #static methods are just methods that never reference "this" :print_total() print 'There are',num,'cats total.' c = cat() d = cat() d.meow() #output: Cat #1 says meow! cat.print_total() #output: There are 2 cats total.
#class syntax also doubles as a way to declare static variables within functions #static declarations are initialized on first call (like C++, and unlike the hacks you have to use # in Python/Javascript where you have to keep track of first call and initialize/declare static # variables in multiple places) :count() @static x = 0 return static.x++ print count() print count() ## output: 0 1 ##
#load and execute file, similar to #include in C++, or adding a script tag in Javascript #(does not wrap the code in a module/namespace, unlike Python) import('stuff.axm')
#load and execute code from anywhere on the Internet. no need for a package manager! #code gets cached in a subdirectory named .axiom #similar to Deno import('https://axm.dev/example.axm') ## you can also just run it from the command line like: $ axiom https://axm.dev/example.axm ##
#strings are mutable! this is something that Python/Java/Javascript can't do s = 'hello' s[1] = 'a' print s #output: hallo #multiline strings :poem() #indentation is removed, unlike in Python/Javascript return ` mary had a little lamb its fleece was white as snow` print poem() ## output: mary had a little lamb its fleece was white as snow ##
#object literals can take identifiers as keys (like Javascript) #(no need to put quotes everywhere like in Python) o = { a: 1, b: 2 } #get type (can be 'array', 'object', 'string', 'number', 'boolean', or 'null') print o.type() #output: object #object key-value pairs are ordered and can be retrieved by index #(no need for in/of like in Javascript or .iteritems()/range() like in Python) for n,o.len() print o.keys(n),o.values(n) ## output: a 1 b 2 ## #methods are distinct from members #(this avoids the .hasOwnProperty()/Object.keys(o)/prototype hacks in Javascript) o.len = 44 #assign new key (does not overwrite .len method) print o.len() #now 3 because keys are: a,b,len print o['len'] #still 44 (can use [] to retrieve key masked by method)
#get current timestamp t = time() #get timestamp for a specific date t = time(2000,1,2,3,45,56) #format timestamp as date string print t.date() #output: 2000/01/02 03:45:56 Sun
#load entire file into memory data = io.load('smallfile.txt') #process file line-by-line f = io.file('bigfile.txt') while (line = f.read_line())!=null #side note: no need for a separate walrus operator like in Python print 'LINE:',line
#native threads are still something that Python/Javascript can't do #(for a while Java was in that boat as well, but they fixed it) :hello(name) print 'hello',name+'!' t = os.thread(hello,null,['from another thread']) t.start() t.wait() #output: hello from another thread!
#much less syntax compared to ctypes in Python #supports more parameters (up to 9) compared to MJS #missing from NodeJS (one of the creator's biggest regrets, according to a talk he gave) some_library = native('some_library.so') narf = some_library.declare('int narf(int,char,char*)') print narf(3,'c','axiom is great!') ## C source of native code ## ## int narf(int i, char c, const char* s){ printf("i = %d, c = %c, s = %s\n",i,c,s); return 99; } ## ## output: i = 3, c = c, s = axiom is great! 99 ##
#way simpler than the default libraries in both Python (urllib2) and Javascript (XMLHttpRequest) res = web.req('http://example.com/') print res.head print res.body
:on_request() web.server.write_200('text/html','<html>Hello world!</html>') web.server.use(on_request) web.server.run({port:9000}) #see other options in docs
See the complete language manual and library reference here.