Skip to content

Commit 1400852

Browse files
committed
create stream mode for sapply
1 parent f1b9f52 commit 1400852

File tree

3 files changed

+91
-64
lines changed

3 files changed

+91
-64
lines changed

R#/Runtime/Internal/internalInvokes/applys.vb

+80-62
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
#Region "Microsoft.VisualBasic::4adbfc2b363ce57d089db6a59ee7342d, R#\Runtime\Internal\internalInvokes\applys.vb"
22

3-
' Author:
4-
'
5-
6-
7-
' xieguigang ([email protected])
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-
35-
' Code Statistics:
36-
37-
' Total Lines: 649
38-
' Code Lines: 428 (65.95%)
39-
' Comment Lines: 135 (20.80%)
40-
' - Xml Docs: 82.96%
41-
'
42-
' Blank Lines: 86 (13.25%)
43-
' File Size: 27.80 KB
44-
45-
46-
' Module applys
47-
'
48-
' Function: apply, checkInternal, lapply, (+3 Overloads) lapplyGeneralIDictionary, lapplyGeneralSequence
49-
' lapplyPipelineStream, lapplyRNameIndex, parLapply, parSapply, sapply
50-
' sapplyList, sapplySequence
51-
'
52-
'
53-
' /********************************************************************************/
3+
' Author:
4+
'
5+
6+
7+
' xieguigang ([email protected])
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+
35+
' Code Statistics:
36+
37+
' Total Lines: 649
38+
' Code Lines: 428 (65.95%)
39+
' Comment Lines: 135 (20.80%)
40+
' - Xml Docs: 82.96%
41+
'
42+
' Blank Lines: 86 (13.25%)
43+
' File Size: 27.80 KB
44+
45+
46+
' Module applys
47+
'
48+
' Function: apply, checkInternal, lapply, (+3 Overloads) lapplyGeneralIDictionary, lapplyGeneralSequence
49+
' lapplyPipelineStream, lapplyRNameIndex, parLapply, parSapply, sapply
50+
' sapplyList, sapplySequence
51+
'
52+
'
53+
' /********************************************************************************/
5454

5555
#End Region
5656

@@ -340,11 +340,17 @@ Namespace Runtime.Internal.Invokes
340340
''' In the case of functions like +, %*%, the function name must be
341341
''' backquoted or quoted.
342342
''' </param>
343+
''' <param name="stream">
344+
''' run in lazy stream mode? default is not.
345+
''' </param>
343346
''' <param name="envir"></param>
344347
''' <returns></returns>
345348
<ExportAPI("sapply")>
346349
<RApiReturn(GetType(vector))>
347-
Public Function sapply(<RRawVectorArgument> X As Object, FUN As Object, envir As Environment) As Object
350+
Public Function sapply(<RRawVectorArgument> X As Object, FUN As Object,
351+
Optional stream As Boolean = False,
352+
Optional envir As Environment = Nothing) As Object
353+
348354
If X Is Nothing Then
349355
Return New Object() {}
350356
End If
@@ -366,10 +372,10 @@ Namespace Runtime.Internal.Invokes
366372
If X.GetType.ImplementInterface(GetType(IDictionary)) Then
367373
check = DirectCast(X, IDictionary).sapplyList(apply, nameVec, envir)
368374
Else
369-
check = sapplySequence(X, apply, envir)
375+
check = sapplySequence(X, apply, stream, envir)
370376
End If
371377

372-
If TypeOf check Is Message Then
378+
If TypeOf check Is Message OrElse stream Then
373379
Return check
374380
Else
375381
arrayVec = check
@@ -384,9 +390,8 @@ Namespace Runtime.Internal.Invokes
384390
End If
385391
End Function
386392

387-
Private Function sapplySequence(x As Object, apply As RFunction, envir As Environment) As Object
393+
Private Function sapplySequence(x As Object, apply As RFunction, stream As Boolean, envir As Environment) As Object
388394
Dim seq As New List(Of Object)
389-
Dim value As Object
390395
Dim argsPreviews As InvokeParameter()
391396
Dim i As i32 = 1
392397
Dim pull As IEnumerable(Of Object)
@@ -399,14 +404,27 @@ Namespace Runtime.Internal.Invokes
399404
pull = REnv.asVector(Of Object)(x).AsObjectEnumerator
400405
End If
401406

402-
For Each d As Object In pull
403-
argsPreviews = invokeArgument(d, ++i)
404-
value = apply.Invoke(envir, argsPreviews)
407+
Dim populateOut As Func(Of IEnumerable(Of Object)) =
408+
Iterator Function() As IEnumerable(Of Object)
409+
Dim value As Object
405410

406-
If TypeOf value Is ReturnValue Then
407-
value = DirectCast(value, ReturnValue).value.Evaluate(envir)
408-
End If
411+
For Each d As Object In pull
412+
argsPreviews = invokeArgument(d, ++i)
413+
value = apply.Invoke(envir, argsPreviews)
414+
415+
If TypeOf value Is ReturnValue Then
416+
value = DirectCast(value, ReturnValue).value.Evaluate(envir)
417+
End If
418+
419+
Yield value
420+
Next
421+
End Function
422+
423+
If stream Then
424+
Return pipeline.CreateFromPopulator(populateOut())
425+
End If
409426

427+
For Each value As Object In populateOut()
410428
If Program.isException(value) Then
411429
Return value
412430
Else

R#/Runtime/Internal/objects/dataset/pipeline.vb

+10-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ Namespace Runtime.Internal.Object
136136
elementType = type
137137
End Sub
138138

139+
Sub New(input As IEnumerable(Of Object))
140+
pipeline = input
141+
elementType = RType.any
142+
End Sub
143+
139144
''' <summary>
140145
''' Gets the R# error message data
141146
''' </summary>
@@ -226,7 +231,11 @@ Namespace Runtime.Internal.Object
226231
''' <typeparam name="T"></typeparam>
227232
''' <param name="upstream">the upstream data</param>
228233
''' <param name="finalize"></param>
229-
''' <returns></returns>
234+
''' <returns>
235+
''' </returns>
236+
''' <remarks>
237+
''' just a simple wrapper to the <see cref="pipeline"/> object constructor function.
238+
''' </remarks>
230239
<DebuggerStepThrough>
231240
Public Shared Function CreateFromPopulator(Of T)(upstream As IEnumerable(Of T), Optional finalize As Action = Nothing) As pipeline
232241
Return New pipeline(upstream, GetType(T)) With {

R#/Runtime/Interop/RType.vb

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Namespace Runtime.Interop
231231
Public Shared ReadOnly Property list As RType = RType.GetRSharpType(GetType(list))
232232

233233
''' <summary>
234-
''' <see cref="Object"/>
234+
''' mapping to the clr <see cref="Object"/> type.
235235
''' </summary>
236236
''' <returns></returns>
237237
Friend Shared ReadOnly Property any As RType = GetRSharpType(GetType(Object))

0 commit comments

Comments
 (0)