-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pseudo pattern matching #109
Comments
Cool.
|
👍 very good idea |
+1 on that |
And we can add something more sophisticated to it when there will be static type checking http://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/ |
where:
|
@satyr if the results have side effects, that won't work |
It should accept functions as results for a real implementation. Makes for a good prelude addition I guess. |
If it does get done, this would be a good use for pure function annotations |
I added some more features. You can use it with no subject at all, or multiple arguments.
|
Dude, that's awesome. Any ETA on 0.9.13/0.10? |
Some time in the next week. |
imo it should work when var defs are located after newline after quick-sort = ([x, ...xs]:list) ->
| empty list => []
| otherwise => (quick-sort left) +++ [x] +++ (quick-sort right) where
[right, left] = partition (<= x), xs |
All hail our new match statement overlords!
also
also
etc. |
@gkz how about making this a main matching syntax in function declarations instead of current take = (n, [x, ...xs]:list) -->
| (<= 0), _ => []
| _, [] => []
| otherwise => [x] +++ take n - 1, xs |
@paulmillr: + a lot |
I was thinking about that too. I think I'll need to think more about the consequences of it before going ahead. Changes I made to make the above possible - feel free to discuss
|
Woah, woah, woah. A little harsh for me !
Really took me quite time to understand that "_" are used here to say "whatever for this value"
erm ... does the following still works ?
|
No, the multiple arguments thing was dropped instead for multiple simultaneous checks. |
not sure I completely agree with that. I think I prefer to write nested switch, because the reading of the match / the cases becomes too messy really easily. I'll have to look "ok, so the comma is here, the 4 is to test against the b, and ..." |
I don't think I'll make match the default because match is not as stable (may change in the future), is more complex, and has a messier compilation. |
Isn't it possible to add back the "multiple arguments" when match arglist's length > case arglist's length, allowing the following possible match iA, iB
| (==) => "Equal int"
| (>) => "A gt B"
| odd, even => "a odd and b even" The rule would be simple : if we have a/some missing case(s), then it's/they're argument(s) for the prev case. I.e. : match a, b, c
| test => 'called test(a, b, c)'
| test, tryndie => 'called test(a) and tryndie(b, c)'
| foo, _, _ => 'called foo(a)' I don't know if that'd be possible without creating some slow/ugly, but I think that'd bring the best of both worlds. |
Talking about #123 (yay nice number), would you consider moving this to "switch" ?
Since it isn't a call in any way |
Possible ideas to rip off https://github.com/natefaubion/matches.js |
currently, |
george would be possible do destructuring with pattern matching |
Except
Oh yeah, totally not. right. |
How to check typeof? a = "abc"
match a
| (typeof a is "string") => 1 #doesn’t work
| _ => 2 |
|
in the new prelude.ls we have |
Is there possibility to check another types, like function, date, regexp, element etc? |
yeah, to get type of anything just do |
|
How about scala style extractors? See http://www.scala-lang.org/node/112 for an example. |
Hmm, seems to have some issues with functions. This is an error num = -> match it
| \1 => \one
num \1 #=> it is not defined but this works num = -> switch it
| \1 => \one
num \1 #=> 'one' Seems the match example is compiling to this: var num;
num = function(){
var ref$;
switch (ref$ = [it], false) {
case '1' !== ref$[0]:
return 'one';
}
};
num('1'); |
It doesn't need to be pseudo pattern matching... If we use the current patterns that livescript can already use for destructuring assignments, then we could have something like this (each example is some made-up livescript followed by the JavaScript I think it should compile to. The JavaScript works):
This is much closer to how functional languages actually use match expressions, i.e. you have a pattern (that could be used as an assignment, for example) and in each branch you test against it. If the test succeeds then you destructure and execute the given code block, otherwise you continue onto the next pattern and try to match that. This could be used for some duck typing to test whether objects have a certain shape before you act on them. |
Sparkler is fairly interesting: https://github.com/natefaubion/sparkler |
Was this implemented at the end ? |
Yes. It got even updated in v.1.6.0. |
It'd be cool if we had a
match
statement, which ran its operand against the functions in itscase
s. For example:The text was updated successfully, but these errors were encountered: