www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Slice patterns in Nightly Rust

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Recently published documentation Nightly Rust. I saw this:
https://doc.rust-lang.org/nightly/book/slice-patterns.html

What do you think about this: a terrible thing or a cool feature?

fn is_symmetric(list: &[u32]) -> bool {
     match list {
         [] | [_] => true,
         [x, inside.., y] if x == y => is_symmetric(inside),
         _ => false
     }
}

fn main() {
     let sym = &[0, 1, 4, 2, 4, 1, 0];
     assert!(is_symmetric(sym));

     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
     assert!(!is_symmetric(not_sym));
}

http://is.gd/TvrXSn

I see this competition slices of D. Also seen are not very clear 
design.
Jun 20 2015
next sibling parent "Israel" <tl12000 live.com> writes:
On Sunday, 21 June 2015 at 03:23:18 UTC, Dennis Ritchie wrote:
 Recently published documentation Nightly Rust. I saw this:
 https://doc.rust-lang.org/nightly/book/slice-patterns.html

 What do you think about this: a terrible thing or a cool 
 feature?

 fn is_symmetric(list: &[u32]) -> bool {
     match list {
         [] | [_] => true,
         [x, inside.., y] if x == y => is_symmetric(inside),
         _ => false
     }
 }

 fn main() {
     let sym = &[0, 1, 4, 2, 4, 1, 0];
     assert!(is_symmetric(sym));

     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
     assert!(!is_symmetric(not_sym));
 }

 http://is.gd/TvrXSn

 I see this competition slices of D. Also seen are not very 
 clear design.
Jesus Christ, that syntax...
Jun 20 2015
prev sibling next sibling parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Sunday, 21 June 2015 at 03:23:18 UTC, Dennis Ritchie wrote:
 I see this competition slices of D. Also seen are not very 
 clear design.
It functional programming using pattern matching over arrays. It is very clear, but perhaps not frequently needed.
Jun 20 2015
prev sibling next sibling parent reply "Nicholas Wilson" <iamthewilsonator hotmail.com> writes:
On Sunday, 21 June 2015 at 03:23:18 UTC, Dennis Ritchie wrote:
 Recently published documentation Nightly Rust. I saw this:
 https://doc.rust-lang.org/nightly/book/slice-patterns.html

 What do you think about this: a terrible thing or a cool 
 feature?

 fn is_symmetric(list: &[u32]) -> bool {
     match list {
         [] | [_] => true,
         [x, inside.., y] if x == y => is_symmetric(inside),
         _ => false
     }
 }

 fn main() {
     let sym = &[0, 1, 4, 2, 4, 1, 0];
     assert!(is_symmetric(sym));

     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
     assert!(!is_symmetric(not_sym));
 }

 http://is.gd/TvrXSn

 I see this competition slices of D. Also seen are not very 
 clear design.
We can do this in D: bool is_symmetric(T)(T[] arr) { if (arr.length == 0 || arr.length == 1) return true; if (arr[0] == arr[$]) return is_symmetric(arr[1..$-1]); return false; } and generally match subranges using strides: arr[stride..index] where both stride and index can be arbitrary expressions. and in higher dimensions with Ilya's and Joakim libraries. Also implicit declaration of variables. Urgh.
Jun 21 2015
parent =?UTF-8?Q?Tobias=20M=C3=BCller?= <troplin bluewin.ch> writes:
"Nicholas Wilson" <iamthewilsonator hotmail.com> wrote:
 On Sunday, 21 June 2015 at 03:23:18 UTC, Dennis Ritchie wrote:
 Recently published documentation Nightly Rust. I saw this:
 https://doc.rust-lang.org/nightly/book/slice-patterns.html
 
 What do you think about this: a terrible thing or a cool > feature?
 
 fn is_symmetric(list: &[u32]) -> bool {
     match list {
         [] | [_] => true,
         [x, inside.., y] if x == y => is_symmetric(inside),
         _ => false
     }
 } 
[...] Also implicit declaration of variables. Urgh.
Patterns are the way that variables are declared in Rust, there's nothing implicit about that. let <pattern> = <expr>; match <expr> { <pattern> = <expr>, <pattern> = <expr>, } fn myfunction(<pattern>, <pattern>) And AFAIK those are the only places where patterns appear and where variables are declared. Tobi
Jun 21 2015
prev sibling parent "QAston" <qastonx gmail.com> writes:
On Sunday, 21 June 2015 at 03:23:18 UTC, Dennis Ritchie wrote:
 Recently published documentation Nightly Rust. I saw this:
 https://doc.rust-lang.org/nightly/book/slice-patterns.html

 What do you think about this: a terrible thing or a cool 
 feature?

 fn is_symmetric(list: &[u32]) -> bool {
     match list {
         [] | [_] => true,
         [x, inside.., y] if x == y => is_symmetric(inside),
         _ => false
     }
 }

 fn main() {
     let sym = &[0, 1, 4, 2, 4, 1, 0];
     assert!(is_symmetric(sym));

     let not_sym = &[0, 1, 7, 2, 4, 1, 0];
     assert!(!is_symmetric(not_sym));
 }

 http://is.gd/TvrXSn

 I see this competition slices of D. Also seen are not very 
 clear design.
Like, every modern language has a notion of closed iteration abstraction (Ranges, Java streams, Clojure seqs, .net LINQ, etc...). Many also have slicing: go, python, rust, ml... It's time to get over thinking those are unique :) What you linked here is a tutorial for pattern matchig on slices (which is a feature unrelated to D as it doesn't have pattern matching)- the doc for slices themselves is here: http://doc.rust-lang.org/nightly/std/slice/
Jun 21 2015