All pastes #814026 Raw Edit

k4st

public text v1 · immutable
·published 2007-12-12 23:14 UTC
rendered paste body

/* syntax for quickly typed simple functions and more involved functions by
   returning a language construct 
 */

simple(x) => x+2

complex(x) => do:
	x = x+2
	return x
;

many_args(...) => do:
	return args /* array of arguments */
;

/* example of an iterator */
ArrayIterator(some_array) => for elem in some_array:
	yield elem
;

/* Object from dictionary, dictionaries can be accessed using the '.' notation 
   assuming there are valid characters in them. Do not confuse this with real 
   objects!
 */
Object(d:dict) => do:
	this = d
	
	if !("construct" in this):
		this.construct = () => null
	;
	
	return (...) =>do:
		apply(this.construct, args)
		return this
	;
;

ArrayIterator = Object({
	
	ls: null,
	ln: 0,
	i: 0,
	
	construct: (ls:list) => do:
		this.ls = ls;
		this.ln = length(ls)
	;,
	next: () => do:
		if this.i in this.ls:
			return this.ls[this.i++]
		;
		
		error: 0; /* note: error would be a construct like 'do', thus allowing
		             for interesting errors! This is a sort of break/return.
		           */
	;,
	/* etc */
})