| 1 | #Region "Microsoft.VisualBasic::258dc4860a8137aec93da96af2701d48, Microsoft.VisualBasic.Core\Language\Language\Python\Collection.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module Collection |
| 35 | ' |
| 36 | ' Function: slice |
| 37 | ' |
| 38 | ' |
| 39 | ' /********************************************************************************/ |
| 40 | |
| 41 | #End Region |
| 42 | |
| 43 | Imports System.Runtime.CompilerServices |
| 44 | |
| 45 | Namespace Language.Python |
| 46 | |
| 47 | Public Module Collection |
| 48 | |
| 49 | ''' <summary> |
| 50 | ''' 将序列之中的指定区域的内容取出来 |
| 51 | ''' </summary> |
| 52 | ''' <typeparam name="T"></typeparam> |
| 53 | ''' <param name="[set]"></param> |
| 54 | ''' <param name="start"></param> |
| 55 | ''' <param name="[stop]"> |
| 56 | ''' 可以接受负数,如果为负数,则表示终止的下标为长度将去这个stop值的结果,即从后往前数 |
| 57 | ''' </param> |
| 58 | ''' <param name="[step]"></param> |
| 59 | ''' <returns></returns> |
| 60 | <Extension> |
| 61 | Public Iterator Function slice(Of T)([set] As IEnumerable(Of T), start%, stop%, Optional step% = 1) As IEnumerable(Of T) |
| 62 | Dim array As T() = [set].Skip(start).ToArray |
| 63 | |
| 64 | If [stop] < 0 Then |
| 65 | [stop] = array.Length + [stop] |
| 66 | End If |
| 67 | |
| 68 | [stop] -= start |
| 69 | [stop] -= 1 |
| 70 | |
| 71 | For i As Integer = 0 To [stop] Step [step] |
| 72 | Yield array(i) |
| 73 | Next |
| 74 | End Function |
| 75 | End Module |
| 76 | End Namespace |