loop

loop is a little language for the jvm. it takes advanced functional features and makes them easy and fun to use!

see for yourself

hello world
pointer to call your attention to examples
click for more
  1. print('hi')
  2. greet(name) ->
      print("hi, @{name}!")
  3. [1, 3, 3, 4]   # a list
    
    {3, 3, 4, 4}   # a set => {3, 4}
  4. # a hashmap
    { 'name'    : 'Harry Potter',
      'nemesis' : 'Voldemort' }
    
    # a tree => { 'l','o','o','p' }
    [ 'o' : 2,
      'p' : 4 ,
      'l' : 1 ,
      'o' : 3 ].keySet()
  5. # a map, with symbols
    { @name    : 'Harry Potter',
      @nemesis : 'Voldemort' }
    
    # a set, similarly => false
    { @sprinkles, @syrup, @cherry }.contains(@fudge)
    
  6. # transform list => [ 'STEVE', 'WOZ', 'RANDY' ]
    i.toUpperCase() for i in ['steve', 'woz', 'randy']
    
    
    # filter list => [ 'ipod', 'ipad', 'iphone' ]
    ('i' + p) for p in ['mac',
                        'pod',
                        'pad',
                        'phone'] if p.startsWith('p')
    
  7. # local variables, private to function
    minutes(num) ->
      "@{num * year} minutes in @{num} years"
      where
        hour: 60
        day : 24 * hour
        week: 7 * day
        year: 52 * week
    
  8. # reverses a list => [3, 14, 15, 268]
    reverse(ls) =>
      []      : []
      [x:xs]  : reverse(xs) + [x]
    
    reverse([268, 15, 14, 3])
    
  9. # pattern matching + guards => 'qi'
    show(host) =>
      ('Stephen ' : sur)  | sur == 'Fry'      : @qi
                          | sur == 'Colbert'  : @report
                          | else              : @boo
    
    
    show('Stephen Fry')
    
  10. # exceptions handled with pattern matching
    cleanup(file) except handler ->
      file.open().truncate()
    
    
    handler(e) =>
      FileNotFoundException  : "no harm, no file"
      IOException            : "failed: @{e.message}"
    
  11. # sorts a list => [3, 14, 15, 268]
    quicksort(ls) =>
      []      : []
      [x:xs]  : (quicksort(i for i in xs if i > x) + [x] +
                 quicksort(i for i in xs if i < x))
    
    
    quicksort([15, 268, 3, 14])
    
  12. # creates instance of Star with default age
    class Star ->
      name
      mass
      age  : '4 billion years'
    
    new Star(name: 'Proxima Centauri'
             mass: 0.123)
    
  13. # instance method => 'mostly'
    'They mostly come at night, mostly'.substring(28)
    
    # static method => 133434532556
    `java.lang.System`.currentTimeMillis()
    
    # constructor => Sun 15 Apr 2012 14:22:50 UTC
    new java.util.Date()
    
    # property => 1 (getTime)
    new java.util.Date(1).time
    

here I am "adding" a method to a simple java string

greet(name) ->
  "hi, @{name}!"

'Zaphod Beeblebrox'.~greet()~

loop produces clear, useful stack traces when things go wrong

java.lang.RuntimeException: i'm complaining
    at prelude.raise(prelude.loop:9)
    at myapp.func3(my_app.loop:3)
    at myapp.func2(my_app.loop:6)
    at myapp.func1(my_app.loop:9)
    at myapp.main(my_app.loop:13)

other fun stuff