Skip to content

Instantly share code, notes, and snippets.

@tiffon
Last active December 19, 2015 02:09
Show Gist options
  • Save tiffon/5881101 to your computer and use it in GitHub Desktop.
Save tiffon/5881101 to your computer and use it in GitHub Desktop.
vamp
`` 'it' keyword replaces 'this'
`` variable declaration ':='
`` lamda function '-->'
`` within an object literal ':prop = value'
`` loops
`` -----------------------------------------------------------------------------
`` for loop
for i := 0; i < limit; i++ {
`` do something
}
`` while loop
for : i < limit {
`` do something
}
`` for in
for index in iterable {
`` do something
}
for key, value in iterable {
`` do something
}
for key, value, i in iterable {
`` i is iteration number
}
`` Range literals
`` -----------------------------------------------------------------------------
`` creates an array of numbers
[0..4] `` [0,1,2,3,4]
[0..<4] `` [0,1,2,3]
[4..0] `` [4,3,2,1,0]
[0..-4] `` [0,-1,-2,-3,-4]
`` specify the step size
[0..10, % 2] `` [0,2,4,6,8,10]
`` specify the number of steps
[0..10, / 5] `` [0,2.5,5,7.5,10]
`` end value can be derived
[0.., % 2, / 5] `` [0,2,4,6,8]
0..4 `` creates a generator with props: stepSize, from, to, etc
`` generator ranges can have an open form
0.. `` 0 to infinity with step-size 1
0.., % Math.PI `` 0 to infinity with step-size Math.PI
0.., / 100 `` Compile Error - Cannot subdivide an open range
[0..] `` Compile Error - Cannot form an Array from an open range
`` define step size with a func
0.., % i --> i**2 `` 0,1,4,9,16,25...
[0.., % i --> i**2, / 5] `` [0,1,4,9,16]
`` Comprehensions
`` -----------------------------------------------------------------------------
`` forms an Array of the ages of the people
ages := [ p in people | p.age ]
`` forms an Array of the names
[ p in people | p.firstName + " " + p.lastName ]
`` an Array of adults
[ p in people | ? p.age >= 18 : p ]
`` an Object mapping names to people
{ p in people | p.firstName + " " + p.lastName <-> p }
`` an Array of employers
[ p in people | j in p.jobs | j.organization ]
`` Generator expressions
`` -----------------------------------------------------------------------------
`` have the same form as comprehensions, but result in a generator (ie lazy)
ages := p in people | p.age
for age in ages {
log(age)
}
ages = p in people | p.age
var age
for : age = ages.next().value {
log(age)
}
`` invocation patterns and function literals
`` -----------------------------------------------------------------------------
`` basic function literal and for loop
repeat := func (times, action) {
for i := 0; i < times; i++ {
action(i)
}
}
repeat = func (times, action) {
`` single statement for loop
for i in 0..<times | action(i)
}
repeat(5, i --> print(i**2))
repeat(times:5, action: --> print($0**2))
repeat(
times:5
action: func (i) {
print(i**2)
}
)
`` match properties to argument names (works with Arrays, positionally)
the_params := {
:times = 5
:action = --> print($0**2)
}
repeat(...the_params)
the_args := [5, i --> print(i**2)]
repeat(...the_args)
`` switch statement
`` -----------------------------------------------------------------------------
part_factor, sign := 1
val_growth := 10
val := 0
for c in "-1_000_045.3" {
switch c.codeAt(0) {
: '_' continue
: '-' sign = -sign
: '.', ',' val_growth = 1
part_factor, factor_chg = 0.1
: >= '0', <= '9' `` $spec is switch test value
val = val * val_growth + ($spec - '0') * part_factor * sign
`` * is alias for default
`` ~ is the assert operator
`` $i is current iteration
`` $src is value being iterated
: * ~ ; ("Invlaid character: %q at %d, string: " % c, $i, $src)
}
}
`` An example of inheritance with logging and serialization utils
`` -----------------------------------------------------------------------------
`` serialization
data_fmt_mx := {
:csv = --> [_, val in it | val].join(",")
:json = --> JSON.stringify(it)
}
`` logging
log_self_mx := module {
to_str := func (val) {
vtype := typeof val
? vtype == "func" {
-> "[func #{val.name || "<anonymous>"}]"
} ^ ? vtype != "object" {
-> "" + val
}
? val.ctor.name {
-> "[object #val.ctor.name]"
} ^ ? val.ctor.proto.name {
-> "[object #val.ctor.proto.name]"
} ^ {
-> "" + val
}
}
const prop_fmt := "\t %s15 %s"
-> {
:alt_to_str = to_str
:log_self = func (props...) {
out := [to_str(it)]
? props.length {
for _, p in props | out.push( prop_fmt % p, to_str( it[p] ))
} ^ {
for p, v in it | out.push(prop_fmt % p, to_str( v ))
}
log( out.join("\n") )
}
}
}
`` base class
type Person
{
Person (name, age, city) {
it.name = name
it.age = age
it.city = city
}
proto {
:move = func (new_city) {
it.city = new_city
}
:birthday = --> ++.age
}
}
`` sub class
type Employee is Person
{
`` 'mix' copies functions to the prototype. A subset can be specified,
`` functions can be renamed, or the entire function set can be copied.
mix {
data_fmt_mx: json as to_json, *
log_self_mx
}
`` private static function
celebrate_birthday := --> log("Happy Birthday!")
`` private static value
total := 0
`` public static function, Employee.num_employees()
static num_employees := --> total
Employee (role, department, name, age, city="New york") {
super(...$namedArgs)
total++
it.role = role
it.department = department
}
proto {
:change_roles = func (new_role) {
it.role = new_role
}
:change_departments = func (new_dept) {
it.department = new_dept
}
:birthday = func {
super.birthday()
celebrate_birthday()
-> ++it.age
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment