Forums
Destructuring-bind and/or pattern matching
3 posts
• Page 1 of 1
Destructuring-bind and/or pattern matching
I know that Cobra supports multi assignment, but are there any plans to support those other features?
- hack.augusto
- Posts: 2
Re: Destructuring-bind and/or pattern matching
Not in the short run. There is still some "catch up" to do wrt to Python and C# such as multi-line strings and picking up C# extension methods.
However, if you want to improve the chances that any particular features show up in Cobra, you could put out a few examples that you think are compelling and make sense with respect to the rest of the language, and the discussion could begin.
However, if you want to improve the chances that any particular features show up in Cobra, you could put out a few examples that you think are compelling and make sense with respect to the rest of the language, and the discussion could begin.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Destructuring-bind and/or pattern matching
I asked most out of curiosity, as the Cobra language brings lots of good stuff from other language maybe you had plans for those, but here is some thought:
Thinking of Nice and Pizza languages, each have an approach to pattern matching, looks as follow:
Nice:
Pizza:
What I had in mind was a mix of nice's and pizza's approaches with some syntax sugar on top for basic data types (arrays, lists, tuples(?)), something more like Lisp's macros params or Haskell's patterns:
Thinking of Nice and Pizza languages, each have an approach to pattern matching, looks as follow:
Nice:
- Code: Select all
int fib(int n) requires n >= 0 : "Fibonacci function is not defined for negative integers!";
fib(0) = 1;
fib(1) = 1;
fib(n) = fib(n-2) + fib(n-1);
- Code: Select all
enum State {Idle, On, Active}
class FinitiStateMachine {
transition( State s, String e ){ last = "Unknown"; }
transition( Idle, "TurnOn" ){ last = "TurnOn"; state = On; }
transition( On, "TurnOff" ){ last = "TurnOff"; state = Idle; }
transition( On, "Activate" ){ last = "Activate"; state = Active; }
transition( Active, "Walk" ){ last = "Walk"; }
transition( Active, "Run" ){ last = "Run"; }
transition( Active, "Talk" ){ last = "Talk"; }
transition( Active, "Stop" ){ last = "Stop"; state = On;}
}
Pizza:
- Code: Select all
static <A,B> List<C> mapPairs((A,B)->C f, List<A> xs, List<B> ys) {
List<C> result;
switch (Pair.Pair(xs, ys)) {
case Pair(List.Nil, _):
case Pair(_, List.Nil):
result = List.Nil;
break;
case Pair(List.Cons(A x, List<A> xs1),
List.Cons(B y, List<B> ys1)):
result = List.Cons(f(x, y), mapPairs(f, xs1, ys1));
break;
case _: System.out.println("case 2"); break;
}
return result;
}
What I had in mind was a mix of nice's and pizza's approaches with some syntax sugar on top for basic data types (arrays, lists, tuples(?)), something more like Lisp's macros params or Haskell's patterns:
- Code: Select all
sig MapFunction(a as int);
def map( [] ) = 0
def map( [a : rest ] as List<int>, c as MapFunction):
return [ c(a) : map(rest, c) ]
- hack.augusto
- Posts: 2
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 43 guests