Rhombus Programming Language

1 min read Original article ↗
 
class Rect(left, top, right, bottom)
                     
fun area(r):
  let w = r.right - r.left
  let h = r.bottom - r.top
  w*h
                                
area(Rect(0, 0, 10, 5))

 
class Rect(left, top, right, bottom)
                     
fun rect_like_to_rect(v):
  match v
  | Rect(_, _, _, _): v
  | {"LT": [l, t], "RB": [r, b]}: Rect(l, t, r, b)
  | {"TL": [t, l], "BR": [b, r]}: Rect(l, t, r, b)
                                
rect_like_to_rect({"LT": [0, 2], "RB": [10, 5]})

rect_like_to_rect({"TL": [0, 2], "BR": [10, 5]})

                     
fun all_same([str0, str, ...]):
  all(str0 == str, ...)
 
all_same(["hello", "hello", "hello"])

all_same(["hello", "goodbye", "hello"])

 
class Posn(x, y)
 
fun flip_all([Posn(x, y), ...]):
  [Posn(y, x), ...]
 
flip_all([Posn(1, 2), Posn(3, 4)])

flip_all([Posn(5, 6)])

 
class Tree(val, children :: List.of(Tree)):
  method flatten(): 
    for fold(lst = [val]) (child in children):
      
      lst ++ child.flatten()
 
Tree(1, [Tree(2, [Tree(4, [])]),
         Tree(3, [Tree(5, [])])])
  .flatten() 
 
class Rect(left, top, right, bottom)
 
bind.macro 'OriginRect($right, $bottom)':
  'Rect(0, 0, $right, $bottom)'
 
fun area(r):
  match r
  | OriginRect(r, b): r*b
  | Rect(l, t, r, b): (r-l)*(b-t)
                      
area(Rect(0, 0, 10, 5))