-
Notifications
You must be signed in to change notification settings - Fork 20
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
Reverse selection and slice operators #518
Comments
Not 100% sure about the syntax but definitely something useful, I was wondering if I could use negative values and it results in out of bound exceptions. Are there references to other languages supporting this feature for comparison? |
I know that this feature exists in Cryptol only. It particularly useful for developing cryptographic algorithms. You take the needed items from a collection in a reverse order (in a single-step operation), and do not bother about how to achieve that step-by-step. |
Some form of this would be useful I think. It begs for a custom little indexing DSL. I've also often wanted to be able to index with a collection of indices (like in matlab) and have functions that return sorted index arrays instead of just a sorted array itself (I've had to paste this into my projects so many times). I work with index arrays a lot as they are very efficient. I'm not a bit fan of the !, it seems like it would be best if the description of what's happening could be maintained inside the indexer. |
When working with arrays, it is also frequent to use |
@Rickasaurus In my opinion, the reverse selection and slice operators have:
No new keyword is needed (something like
Which symbols are available on a keyboard for this goal? Alphabet letters have to be excluded entirely for an obvious reason. Please look at your keyboard. The following symbols ( |
I'm used to this from Python where However, the overall idea is one I definitely support. It's a nice utility that I could see people using. |
Agreed |
This is a good and valuable discussion. I'm merging this with this issue: #358 |
Reverse selection and slice operators
I suggest adding the
![]
reverse selection operator,![ .. ]
reverse slice operators for lists, arrays and user-defined types representing a collection of values. These language features will be in great demand and open a much more intuitive and simple way to interact with the data encapsulated in a type if values have to be accessed in a reverse order.The
.[]
forward selection operator and.[ .. ]
forward slice operator, starts counting from the beginning, while the![]
reverse selection operator and![ .. ]
reverse slice operator should start from the end. Indexing always starts at zero (except for non-zero-based arrays).For example,
let array1D : int[] = Array.init 10 (fun i -> i)
. Therefore,array1D = [| 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 |]
,array1D.[0]
is the first item ofarray1D
, whilearray1D![0]
is the last item, that isarray1D.[9]
.The forward and reverse slice operators are intended to concisely select multiple items from a collection. So,
array1D.[0 .. 3]
selects the first four items[| 0; 1; 2; 3 |]
, whilearray1D![0 .. 3]
selects the last four items in a reverse order[| 9; 8; 7; 6 |]
.Language users should be able to add support for reverse selection and slicing by implementing the reverse indexer (as the
ReverseItem
propertywith get, set
) and reverse slicer (as theGetReverseSlice
method) similar to theItem
property,GetSlice
method used for implementing forward selection and slicing for user-defined types in F#.For example,
The existing way of approaching this problem in F# is writing boilerplate helper functions to achieve the same result.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are
!
for reference cells and the!.[]
,!.[ .. ]
operators for reverse selection and slicing. But in modern F# nobody needs to use reference cells as much as with previous versions of F#, as a singlelet mutable
is used as an alternative. The syntax for mutable values does not include the!
symbol. So, the disadvantage of making this adjustment to F# is neglectable.Extra information
The
!
reverse selection operator exists in Cryptol, a programming language for cryptography used by some aerospace and defense contractors in the United States.Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: no
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: