Arc Forum | Okay, I was able to solve a couple problems with my object pattern-matching... ...

1 point by Pauan 4759 days ago | link | parent

Okay, I was able to solve a couple problems with my object pattern-matching...

 [ foo = 5 | bar = 10 ]
The above is a collection of patterns. Specifically, it has a "foo" pattern that maps to 5, and a "bar" pattern that maps to 10. Now, let's put this object into a variable:

 pattern = [ foo = 5 | bar = 10 ]
Now how do we extract the subpatterns? Like so:

 pattern [ foo ]
 pattern [ bar ]
The above returns 5, and then 10. And we can extract multiple patterns at once:

 pattern [ foo | bar ]
The above returns 10. This largely removes the need for "as" patterns, which is something I found cumbersome to use. You can think of | as being kinda like "do". It will first call the foo pattern, then the bar pattern.

Also, in Haskell you might write this:

 fib 0 = 0
 fib 1 = 1
 fib n = fib (n-1) + fib (n-2)
In this language you might write this:

 [ fib 0 = 0
 | fib 1 = 1
 | fib %n = fib (%n - 1) + fib (%n - 2) ]
Hmm... I'm still working out the kinks in the object system...


1 point by Pauan 4759 days ago | link

Okay, even more thinking... what if all objects in this system were simply a collection of functions? Then you'd write something like this:

 duck =
 quack = prn "Quack"
 fly = prn "Flap, Flap"
 person %n =
 quack = prn "@%n walks in the forest and imitates ducks to draw them"
 fly = prn "@%n takes an airplane"
 quack-and-fly %x = %x quack; %x fly
 quack-and-fly: duck
 quack-and-fly: person "Jules Verne"
Wow! Even shorter! This reminds me of CoffeeScript's implicit objects: http://coffeescript.org/#objects_and_arrays

-----




AltStyle によって変換されたページ (->オリジナル) /