Skip to content

Instantly share code, notes, and snippets.

@evincarofautumn
Created August 23, 2017 23:10
Show Gist options
  • Save evincarofautumn/e260ecec8861cbcfa5ddb14ccf49c6f7 to your computer and use it in GitHub Desktop.
Save evincarofautumn/e260ecec8861cbcfa5ddb14ccf49c6f7 to your computer and use it in GitHub Desktop.
OOP experiments in Funk
if := {
|True t _| t()
|False _ e| e()
}
point := {|x y| {
; Multiple constants in a pattern are a parse error. I’d like to write:
; |Has X| True
; |Has Y| True
; |Has "+"| True
; |Has _| False
|Has| { |X| True |Y| True |"+"| True |Show| True |_| False }
|X| x
|Y| y
|"+" p| point(x + p.X, y + p.Y)
|Show| "(" + x + ", " + y + ")"
}}
point3d := {|x y z|
super := point(x, y)
{
; Would like to write:
; |Has Z| True
; |Has other| super.Has(other)
|Has| { |Z| True |other| (super.Has)(other) }
|Z| z
|"+" p|
; p.Has(Z)
if (p.Has.Z) {
point3d(x + p.X, y + p.Y, z + p.Z)
} {
q := super + p
point3d(q.X, q.Y, z)
}
|Show| "(" + x + ", " + y + ", " + z + ")"
; Variadic functions would be nice: |...| super(...)
|f0| super(f0)
|f1 a| super(f1, a) ; I’d also like: super.(f1)(a)
|f2 a b| super(f2, a, b)
; ...
}
}
p1 := point3d(5, 7, 11)
p2 := point(1, 2)
p3 := point3d(1, 2, 5)
p4 := p1 + p2
p5 := p1 + p3
system.SetText(p1.Show + " + " + p2.Show + " = " + p4.Show + "; " + p1.Show + " + " + p3.Show + " = " + p5.Show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment