Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite36641b

Browse files
khmylovlatkin
authored andcommitted
Implement Array.map3, Seq.map3
commit cf8e955a90502cc4d64f3b87269729640c03acafAuthor: latkin <latkin@microsoft.com>Date: Sun Oct 12 16:00:01 2014 -0700 Simplify array length check, add portable surface area testcommit b24338d37046b85e6aaac20366fe84e354d3f0c6Merge:321dcde 87e2dbfAuthor: latkin <latkin@microsoft.com>Date: Sun Oct 12 15:15:10 2014 -0700 Merge branch 'fsharp4' ofhttps://git01.codeplex.com/forks/andrew_khmylov/fsharp into PRcommit 87e2dbf594afcba7b09f6f401eb89197a874a6bbAuthor: Andrew Khmylov <andrew.khmylov@gmail.com>Date: Sat Jul 12 18:53:02 2014 +0300 Add Array.map3 and Seq.map3 functions
1 parent321dcde commite36641b

File tree

8 files changed

+147
-3
lines changed

8 files changed

+147
-3
lines changed

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ type ArrayModule2() =
9494

9595
()
9696

97+
[<Test>]
98+
memberthis.Map3()=
99+
// Integer array
100+
letfuncInt a b c=(a+ b)* c
101+
letresultInt= Array.map3 funcInt[|1..8|][|2..9|][|3..10|]
102+
if resultInt<>[|9;20;35;54;77;104;135;170|]then Assert.Fail()
103+
104+
// First array is shorter
105+
CheckThrowsArgumentException(fun()-> Array.map3 funcInt[|1..2|][|2..9|][|3..10|]|> ignore)
106+
// Second array is shorter
107+
CheckThrowsArgumentException(fun()-> Array.map3 funcInt[|1..8|][|2..6|][|3..10|]|> ignore)
108+
// Third array is shorter
109+
CheckThrowsArgumentException(fun()-> Array.map3 funcInt[|1..8|][|2..9|][|3..6|]|> ignore)
110+
111+
// String array
112+
letfuncStr a b c= a+ b+ c
113+
letresultStr= Array.map3 funcStr[|"A";"B";"C";"D"|][|"a";"b";"c";"d"|][|"1";"2";"3";"4"|]
114+
if resultStr<>[|"Aa1";"Bb2";"Cc3";"Dd4"|]then Assert.Fail()
115+
116+
// Empty array
117+
letresultEmpty= Array.map3 funcStr[||][||][||]
118+
if resultEmpty<>[||]then Assert.Fail()
119+
120+
// Null array
121+
letnullArray=null: int[]
122+
letnonNullArray=[|1|]
123+
CheckThrowsArgumentNullException(fun()-> Array.map3 funcInt nullArray nonNullArray nonNullArray|> ignore)
124+
CheckThrowsArgumentNullException(fun()-> Array.map3 funcInt nonNullArray nullArray nonNullArray|> ignore)
125+
CheckThrowsArgumentNullException(fun()-> Array.map3 funcInt nonNullArray nonNullArray nullArray|> ignore)
126+
127+
()
128+
97129
[<Test>]
98130
memberthis.Mapi()=
99131
// integer array

‎src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,41 @@ type SeqModule2() =
346346
CheckThrowsArgumentNullException(fun()-> Seq.map2 funcInt nullSeq validSeq|> ignore)
347347

348348
()
349-
350-
349+
350+
351+
[<Test>]
352+
memberthis.Map3()=
353+
// Integer seq
354+
letfuncInt a b c=(a+ b)* c
355+
letresultInt= Seq.map3 funcInt{1..8}{2..9}{3..10}
356+
letexpectedInt= seq[9;20;35;54;77;104;135;170]
357+
VerifySeqsEqual expectedInt resultInt
358+
359+
// First seq is shorter
360+
VerifySeqsEqual(seq[9;20])(Seq.map3 funcInt{1..2}{2..9}{3..10})
361+
// Second seq is shorter
362+
VerifySeqsEqual(seq[9;20;35])(Seq.map3 funcInt{1..8}{2..4}{3..10})
363+
// Third seq is shorter
364+
VerifySeqsEqual(seq[9;20;35;54])(Seq.map3 funcInt{1..8}{2..6}{3..6})
365+
366+
// String seq
367+
letfuncStr a b c= a+ b+ c
368+
letresultStr= Seq.map3 funcStr["A";"B";"C";"D"]["a";"b";"c";"d"]["1";"2";"3";"4"]
369+
letexpectedStr= seq["Aa1";"Bb2";"Cc3";"Dd4"]
370+
VerifySeqsEqual expectedStr resultStr
371+
372+
// Empty seq
373+
letresultEmpty= Seq.map3 funcStr Seq.empty Seq.empty Seq.empty
374+
VerifySeqsEqual Seq.empty resultEmpty
375+
376+
// Null seq
377+
letnullSeq=null: seq<_>
378+
letnonNullSeq= seq[1]
379+
CheckThrowsArgumentNullException(fun()-> Seq.map3 funcInt nullSeq nonNullSeq nullSeq|> ignore)
380+
381+
()
382+
383+
351384
memberprivatethis.MapWithSideEffectsTester(map:(int-> int)->seq<int>->seq<int>)expectExceptions=
352385
leti= ref0
353386
letf x= i:=!i+1; x*x

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Microsoft.FSharp.Collections.ArrayModule: TResult SumBy[T,TResult](Microsoft.FSh
140140
Microsoft.FSharp.Collections.ArrayModule: TResult[] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], T[])
141141
Microsoft.FSharp.Collections.ArrayModule: TResult[] Collect[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult[]], T[])
142142
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], T1[], T2[])
143+
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], T1[], T2[], T3[])
143144
Microsoft.FSharp.Collections.ArrayModule: TResult[] MapIndexed2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]]], T1[], T2[])
144145
Microsoft.FSharp.Collections.ArrayModule: TResult[] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], T[])
145146
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
@@ -405,6 +406,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
405406
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
406407
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Collect[T,TCollection,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TCollection], System.Collections.Generic.IEnumerable`1[T])
407408
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
409+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], System.Collections.Generic.IEnumerable`1[T3])
408410
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], System.Collections.Generic.IEnumerable`1[T])
409411
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
410412
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] Scan[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])

‎src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Microsoft.FSharp.Collections.ArrayModule: TResult SumBy[T,TResult](Microsoft.FSh
134134
Microsoft.FSharp.Collections.ArrayModule: TResult[] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], T[])
135135
Microsoft.FSharp.Collections.ArrayModule: TResult[] Collect[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult[]], T[])
136136
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], T1[], T2[])
137+
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], T1[], T2[], T3[])
137138
Microsoft.FSharp.Collections.ArrayModule: TResult[] MapIndexed2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]]], T1[], T2[])
138139
Microsoft.FSharp.Collections.ArrayModule: TResult[] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], T[])
139140
Microsoft.FSharp.Collections.ArrayModule: TResult[] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], T[])
@@ -399,6 +400,7 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
399400
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Choose[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], System.Collections.Generic.IEnumerable`1[T])
400401
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Collect[T,TCollection,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TCollection], System.Collections.Generic.IEnumerable`1[T])
401402
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
403+
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2], System.Collections.Generic.IEnumerable`1[T3])
402404
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] MapIndexed[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[System.Int32,Microsoft.FSharp.Core.FSharpFunc`2[T,TResult]], System.Collections.Generic.IEnumerable`1[T])
403405
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], System.Collections.Generic.IEnumerable`1[T])
404406
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[TState] Scan[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,TState]], TState, System.Collections.Generic.IEnumerable`1[T])

‎src/fsharp/FSharp.Core/array.fs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,20 @@ namespace Microsoft.FSharp.Collections
292292
res.[i]<- f.Invoke(array1.[i], array2.[i])
293293
res
294294

295+
[<CompiledName("Map3")>]
296+
letmap3 f(array1:'T1[])(array2:'T2[])(array3:'T3[])=
297+
checkNonNull"array1" array1
298+
checkNonNull"array2" array2
299+
checkNonNull"array3" array3
300+
letf= OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
301+
letlen1= array1.Length
302+
ifnot(len1= array2.Length&& len1= array3.Length)then invalidArg""(SR.GetString(SR.arraysHadDifferentLengths))
303+
304+
letres= Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1
305+
for i=0to len1-1do
306+
res.[i]<- f.Invoke(array1.[i], array2.[i], array3.[i])
307+
res
308+
295309
[<CompiledName("MapIndexed2")>]
296310
letmapi2 f(array1:'T[])(array2:'U[])=
297311
checkNonNull"array1" array1

‎src/fsharp/FSharp.Core/array.fsi‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,25 @@ namespace Microsoft.FSharp.Collections
428428
///<param name="array1">The first input array.</param>
429429
///<param name="array2">The second input array.</param>
430430
///<exception cref="System.ArgumentException">Thrown when the input arrays differ in length.</exception>
431+
///<exception cref="System.ArgumentNullException">Thrown when any of the input arrays is null.</exception>
431432
///<returns>The array of transformed elements.</returns>
432433
[<CompiledName("Map2")>]
433434
val map2:mapping:('T1-> 'T2-> 'U)->array1:'T1[]->array2:'T2[]-> 'U[]
434435

436+
///<summary>Builds a new collection whose elements are the results of applying the given function
437+
///to the corresponding triples from the three collections. The three input
438+
///arrays must have the same length,otherwise an <c>ArgumentException</c> is
439+
///raised.</summary>
440+
///<param name="mapping">The function to transform the pairs of the input elements.</param>
441+
///<param name="array1">The first input array.</param>
442+
///<param name="array2">The second input array.</param>
443+
///<param name="array3">The third input array.</param>
444+
///<exception cref="System.ArgumentException">Thrown when the input arrays differ in length.</exception>
445+
///<exception cref="System.ArgumentNullException">Thrown when any of the input arrays is null.</exception>
446+
///<returns>The array of transformed elements.</returns>
447+
[<CompiledName("Map3")>]
448+
val map3:mapping:('T1-> 'T2-> 'T3-> 'U)->array1:'T1[]->array2:'T2[]->array3:'T3[]-> 'U[]
449+
435450
///<summary>Builds a new collection whose elements are the results of applying the given function
436451
///to the corresponding elements of the two collections pairwise,also passing the index of
437452
///the elements. The two input arrays must have the same lengths,otherwise an <c>ArgumentException</c> is

‎src/fsharp/FSharp.Core/seq.fs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ namespace Microsoft.FSharp.Collections
146146
e2.Dispose()
147147
}
148148

149+
letmap3 f(e1:IEnumerator<_>)(e2:IEnumerator<_>)(e3:IEnumerator<_>):IEnumerator<_>=
150+
upcast
151+
{new MapEnumerator<_>()with
152+
memberthis.DoMoveNext curr=
153+
letn1= e1.MoveNext()
154+
letn2= e2.MoveNext()
155+
letn3= e3.MoveNext()
156+
157+
if n1&& n2&& n3then
158+
curr<- f e1.Current e2.Current e3.Current
159+
true
160+
else
161+
false
162+
memberthis.Dispose()=
163+
try
164+
e1.Dispose()
165+
finally
166+
try
167+
e2.Dispose()
168+
finally
169+
e3.Dispose()
170+
}
149171

150172
letchoose f(e:IEnumerator<'T>)=
151173
letstarted= reffalse
@@ -914,6 +936,8 @@ namespace Microsoft.FSharp.Collections
914936
letrevamp f(ie:seq<_>)= mkSeq(fun()-> f(ie.GetEnumerator()))
915937
letrevamp2 f(ie1:seq<_>)(source2:seq<_>)=
916938
mkSeq(fun()-> f(ie1.GetEnumerator())(source2.GetEnumerator()))
939+
letrevamp3 f(ie1:seq<_>)(source2:seq<_>)(source3:seq<_>)=
940+
mkSeq(fun()-> f(ie1.GetEnumerator())(source2.GetEnumerator())(source3.GetEnumerator()))
917941

918942
[<CompiledName("Filter")>]
919943
letfilter f source=
@@ -939,6 +963,13 @@ namespace Microsoft.FSharp.Collections
939963
checkNonNull"source2" source2
940964
revamp2(IEnumerator.map2 f) source1 source2
941965

966+
[<CompiledName("Map3")>]
967+
letmap3 f source1 source2 source3=
968+
checkNonNull"source1" source1
969+
checkNonNull"source2" source2
970+
checkNonNull"source3" source3
971+
revamp3(IEnumerator.map3 f) source1 source2 source3
972+
942973
[<CompiledName("Choose")>]
943974
letchoose f source=
944975
checkNonNull"source" source

‎src/fsharp/FSharp.Core/seq.fsi‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ namespace Microsoft.FSharp.Collections
554554
///the other then the remaining elements of the longer sequence are ignored.</summary>
555555
///
556556
///<param name="mapping">A function to transform pairs of items from the input sequences.</param>
557-
///<param name="source">The first input sequence.</param>
557+
///<param name="source1">The first input sequence.</param>
558558
///<param name="source2">The second input sequence.</param>
559559
///
560560
///<returns>The result sequence.</returns>
@@ -563,6 +563,21 @@ namespace Microsoft.FSharp.Collections
563563
[<CompiledName("Map2")>]
564564
val map2:mapping:('T1-> 'T2-> 'U)->source1:seq<'T1>->source2:seq<'T2>->seq<'U>
565565

566+
///<summary>Builds a new collection whose elements are the results of applying the given function
567+
///to the corresponding triples of elements from the three sequences. If one input sequence if shorter than
568+
///the others then the remaining elements of the longer sequences are ignored.</summary>
569+
///
570+
///<param name="mapping">The function to transform triples of elements from the input sequences.</param>
571+
///<param name="source1">The first input sequence.</param>
572+
///<param name="source2">The second input sequence.</param>
573+
///<param name="source3">The third input sequence.</param>
574+
///
575+
///<returns>The result sequence.</returns>
576+
///
577+
///<exception cref="System.ArgumentNullException">Thrown when any of the input sequences is null.</exception>
578+
[<CompiledName("Map3")>]
579+
val map3:mapping:('T1-> 'T2-> 'T3-> 'U)->source1:seq<'T1>->source2:seq<'T2>->source3:seq<'T3>->seq<'U>
580+
566581
///<summary>Builds a new collection whose elements are the results of applying the given function
567582
///to each of the elements of the collection. The integer index passed to the
568583
///function indicates the index(from 0)of element being transformed.</summary>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp