-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPythonInterop.fs
780 lines (683 loc) · 31.6 KB
/
PythonInterop.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
module GMusicAPI.PythonInterop
open FSharp.Interop.Dynamic
type PythonData<'a> =
private {
Delayed : (unit -> 'a)
mutable Cache : 'a option
}
let pythonFunc f = { Delayed = f; Cache = None }
let internal unsafeExecute (f:PythonData<_>) =
match f.Cache with
| Some d -> d
| None ->
let res = f.Delayed()
f.Cache <- Some res
res
let private getPythonData = unsafeExecute
let runInPython f =
use __ = Python.Runtime.Py.GIL()
f |> getPythonData
type PythonBuilder() =
member x.Bind((d : PythonData<_>), f) =
(fun () ->
let r = d |> getPythonData
f r |> getPythonData
) |> pythonFunc
member x.Return(d) =
(fun () -> d) |> pythonFunc
member x.ReturnFrom (d) = d
member x.Delay (f : unit -> PythonData<_>) =
(fun () -> f() |> getPythonData) |> pythonFunc
member x.Combine (v, next) = x.Bind(v, fun () -> next)
member x.Run (f) = f
member x.Zero () = (fun () -> ()) |> pythonFunc
member x.TryWith (d, recover) =
(fun () ->
try
d |> getPythonData
with e -> recover e |> getPythonData) |> pythonFunc
member x.TryFinally (d, final) =
(fun () ->
try
d |> getPythonData
finally final ()) |> pythonFunc
member x.While (condF, body) =
(fun () ->
while condF() do
body |> getPythonData) |> pythonFunc
member x.Using (var, block) =
(fun () ->
use v = var
block v |> getPythonData) |> pythonFunc
member x.For (seq:seq<'T>, action:'T -> PythonData<_>) =
(fun () ->
for item in seq do
action item |> getPythonData) |> pythonFunc
let python = PythonBuilder()
module PythonData =
let map f d =
python {
let! data = d
return f data
}
type IPythonEnumerator<'T> =
abstract MoveNext : unit -> PythonData<'T option>
inherit System.IDisposable
type IPythonEnumerable<'T> =
abstract GetEnumerator : unit -> IPythonEnumerator<'T>
type PythonSeq<'T> = IPythonEnumerable<'T>
module PythonSeq =
let private dispose (d:System.IDisposable) = match d with null -> () | _ -> d.Dispose()
[<GeneralizableValue>]
let empty<'T> : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
{ new IPythonEnumerator<'T> with
member x.MoveNext() = python { return None }
member x.Dispose() = () } }
let singleton (v:'T) : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
let state = ref 0
{ new IPythonEnumerator<'T> with
member x.MoveNext() = python { let res = state.Value = 0
incr state;
return (if res then Some v else None) }
member x.Dispose() = () } }
[<RequireQualifiedAccess>]
type AppendState<'T> =
| NotStarted1 of PythonSeq<'T> * PythonSeq<'T>
| HaveEnumerator1 of IPythonEnumerator<'T> * PythonSeq<'T>
| NotStarted2 of PythonSeq<'T>
| HaveEnumerator2 of IPythonEnumerator<'T>
| Finished
let append (inp1: PythonSeq<'T>) (inp2: PythonSeq<'T>) : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
let state = ref (AppendState.NotStarted1 (inp1, inp2) )
{ new IPythonEnumerator<'T> with
member x.MoveNext() =
python { match !state with
| AppendState.NotStarted1 (inp1, inp2) ->
return!
(let enum1 = inp1.GetEnumerator()
state := AppendState.HaveEnumerator1 (enum1, inp2)
x.MoveNext())
| AppendState.HaveEnumerator1 (enum1, inp2) ->
let! res = enum1.MoveNext()
match res with
| None ->
return!
(state := AppendState.NotStarted2 inp2
dispose enum1
x.MoveNext())
| Some _ ->
return res
| AppendState.NotStarted2 inp2 ->
return!
(let enum2 = inp2.GetEnumerator()
state := AppendState.HaveEnumerator2 enum2
x.MoveNext())
| AppendState.HaveEnumerator2 enum2 ->
let! res = enum2.MoveNext()
return (match res with
| None ->
state := AppendState.Finished
dispose enum2
None
| Some _ ->
res)
| _ ->
return None }
member x.Dispose() =
match !state with
| AppendState.HaveEnumerator1 (enum, _)
| AppendState.HaveEnumerator2 enum ->
state := AppendState.Finished
dispose enum
| _ -> () } }
let delay (f: unit -> PythonSeq<'T>) : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() = f().GetEnumerator() }
[<RequireQualifiedAccess>]
type BindState<'T,'U> =
| NotStarted of PythonData<'T>
| HaveEnumerator of IPythonEnumerator<'U>
| Finished
let bindAsync (f: 'T -> PythonSeq<'U>) (inp : PythonData<'T>) : PythonSeq<'U> =
{ new IPythonEnumerable<'U> with
member x.GetEnumerator() =
let state = ref (BindState.NotStarted inp)
{ new IPythonEnumerator<'U> with
member x.MoveNext() =
python { match !state with
| BindState.NotStarted inp ->
let! v = inp
return!
(let s = f v
let e = s.GetEnumerator()
state := BindState.HaveEnumerator e
x.MoveNext())
| BindState.HaveEnumerator e ->
let! res = e.MoveNext()
return (match res with
| None -> x.Dispose()
| Some _ -> ()
res)
| _ ->
return None }
member x.Dispose() =
match !state with
| BindState.HaveEnumerator e ->
state := BindState.Finished
dispose e
| _ -> () } }
type PythonSeqBuilder() =
member x.Yield(v) = singleton v
// This looks weird, but it is needed to allow:
//
// while foo do
// do! something
//
// because F# translates body as Bind(something, fun () -> Return())
member x.Return _ = empty
member x.YieldFrom(s:PythonSeq<'T>) = s
member x.Zero () = empty
member x.Bind (inp:PythonData<'T>, body : 'T -> PythonSeq<'U>) : PythonSeq<'U> = bindAsync body inp
member x.Combine (seq1:PythonSeq<'T>,seq2:PythonSeq<'T>) = append seq1 seq2
member x.While (guard, body:PythonSeq<'T>) =
// Use F#'s support for Landin's knot for a low-allocation fixed-point
let rec fix = delay (fun () -> if guard() then append body fix else empty)
fix
member x.Delay (f:unit -> PythonSeq<'T>) =
delay f
let pythonSeq = new PythonSeqBuilder()
let emitEnumerator (ie: IPythonEnumerator<'T>) = pythonSeq {
let! moven = ie.MoveNext()
let b = ref moven
while b.Value.IsSome do
yield b.Value.Value
let! moven = ie.MoveNext()
b := moven }
[<RequireQualifiedAccess>]
type TryWithState<'T> =
| NotStarted of PythonSeq<'T>
| HaveBodyEnumerator of IPythonEnumerator<'T>
| HaveHandlerEnumerator of IPythonEnumerator<'T>
| Finished
/// Implements the 'TryWith' functionality for computation builder
let tryWith (inp: PythonSeq<'T>) (handler : exn -> PythonSeq<'T>) : PythonSeq<'T> =
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
let state = ref (TryWithState.NotStarted inp)
{ new IPythonEnumerator<'T> with
member x.MoveNext() =
python { match !state with
| TryWithState.NotStarted inp ->
let res = ref Unchecked.defaultof<_>
try
res := Choice1Of2 (inp.GetEnumerator())
with exn ->
res := Choice2Of2 exn
match res.Value with
| Choice1Of2 r ->
return!
(state := TryWithState.HaveBodyEnumerator r
x.MoveNext())
| Choice2Of2 exn ->
return!
(x.Dispose()
let enum = (handler exn).GetEnumerator()
state := TryWithState.HaveHandlerEnumerator enum
x.MoveNext())
| TryWithState.HaveBodyEnumerator e ->
let res = ref Unchecked.defaultof<_>
try
let! r = e.MoveNext()
res := Choice1Of2 r
with exn ->
res := Choice2Of2 exn
match res.Value with
| Choice1Of2 res ->
return
(match res with
| None -> x.Dispose()
| _ -> ()
res)
| Choice2Of2 exn ->
return!
(x.Dispose()
let e = (handler exn).GetEnumerator()
state := TryWithState.HaveHandlerEnumerator e
x.MoveNext())
| TryWithState.HaveHandlerEnumerator e ->
let! res = e.MoveNext()
return (match res with
| Some _ -> res
| None -> x.Dispose(); None)
| _ ->
return None }
member x.Dispose() =
match !state with
| TryWithState.HaveBodyEnumerator e | TryWithState.HaveHandlerEnumerator e ->
state := TryWithState.Finished
dispose e
| _ -> () } }
[<RequireQualifiedAccess>]
type TryFinallyState<'T> =
| NotStarted of PythonSeq<'T>
| HaveBodyEnumerator of IPythonEnumerator<'T>
| Finished
// This pushes the handler through all the async computations
// The (synchronous) compensation is run when the Dispose() is called
let tryFinally (inp: PythonSeq<'T>) (compensation : unit -> unit) : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
let state = ref (TryFinallyState.NotStarted inp)
{ new IPythonEnumerator<'T> with
member x.MoveNext() =
python { match !state with
| TryFinallyState.NotStarted inp ->
return!
(let e = inp.GetEnumerator()
state := TryFinallyState.HaveBodyEnumerator e
x.MoveNext())
| TryFinallyState.HaveBodyEnumerator e ->
let! res = e.MoveNext()
return
(match res with
| None -> x.Dispose()
| Some _ -> ()
res)
| _ ->
return None }
member x.Dispose() =
match !state with
| TryFinallyState.HaveBodyEnumerator e->
state := TryFinallyState.Finished
dispose e
compensation()
| _ -> () } }
[<RequireQualifiedAccess>]
type CollectState<'T,'U> =
| NotStarted of PythonSeq<'T>
| HaveInputEnumerator of IPythonEnumerator<'T>
| HaveInnerEnumerator of IPythonEnumerator<'T> * IPythonEnumerator<'U>
| Finished
let collect (f: 'T -> PythonSeq<'U>) (inp: PythonSeq<'T>) : PythonSeq<'U> =
{ new IPythonEnumerable<'U> with
member x.GetEnumerator() =
let state = ref (CollectState.NotStarted inp)
{ new IPythonEnumerator<'U> with
member x.MoveNext() =
python { match !state with
| CollectState.NotStarted inp ->
return!
(let e1 = inp.GetEnumerator()
state := CollectState.HaveInputEnumerator e1
x.MoveNext())
| CollectState.HaveInputEnumerator e1 ->
let! res1 = e1.MoveNext()
return!
(match res1 with
| Some v1 ->
let e2 = (f v1).GetEnumerator()
state := CollectState.HaveInnerEnumerator (e1, e2)
| None ->
x.Dispose()
x.MoveNext())
| CollectState.HaveInnerEnumerator (e1, e2) ->
let! res2 = e2.MoveNext()
match res2 with
| None ->
state := CollectState.HaveInputEnumerator e1
dispose e2
return! x.MoveNext()
| Some _ ->
return res2
| _ ->
return None }
member x.Dispose() =
match !state with
| CollectState.HaveInputEnumerator e1 ->
state := CollectState.Finished
dispose e1
| CollectState.HaveInnerEnumerator (e1, e2) ->
state := CollectState.Finished
dispose e2
dispose e1
| _ -> () } }
[<RequireQualifiedAccess>]
type CollectSeqState<'T,'U> =
| NotStarted of seq<'T>
| HaveInputEnumerator of System.Collections.Generic.IEnumerator<'T>
| HaveInnerEnumerator of System.Collections.Generic.IEnumerator<'T> * IPythonEnumerator<'U>
| Finished
// Like collect, but the input is a sequence, where no bind is required on each step of the enumeration
let collectSeq (f: 'T -> PythonSeq<'U>) (inp: seq<'T>) : PythonSeq<'U> =
{ new IPythonEnumerable<'U> with
member x.GetEnumerator() =
let state = ref (CollectSeqState.NotStarted inp)
{ new IPythonEnumerator<'U> with
member x.MoveNext() =
python { match !state with
| CollectSeqState.NotStarted inp ->
return!
(let e1 = inp.GetEnumerator()
state := CollectSeqState.HaveInputEnumerator e1
x.MoveNext())
| CollectSeqState.HaveInputEnumerator e1 ->
return!
(if e1.MoveNext() then
let e2 = (f e1.Current).GetEnumerator()
state := CollectSeqState.HaveInnerEnumerator (e1, e2)
else
x.Dispose()
x.MoveNext())
| CollectSeqState.HaveInnerEnumerator (e1, e2)->
let! res2 = e2.MoveNext()
match res2 with
| None ->
return!
(state := CollectSeqState.HaveInputEnumerator e1
dispose e2
x.MoveNext())
| Some _ ->
return res2
| _ -> return None}
member x.Dispose() =
match !state with
| CollectSeqState.HaveInputEnumerator e1 ->
state := CollectSeqState.Finished
dispose e1
| CollectSeqState.HaveInnerEnumerator (e1, e2) ->
state := CollectSeqState.Finished
dispose e2
dispose e1
x.Dispose()
| _ -> () } }
[<RequireQualifiedAccess>]
type MapState<'T> =
| NotStarted of seq<'T>
| HaveEnumerator of System.Collections.Generic.IEnumerator<'T>
| Finished
let ofSeq (inp: seq<'T>) : PythonSeq<'T> =
{ new IPythonEnumerable<'T> with
member x.GetEnumerator() =
let state = ref (MapState.NotStarted inp)
{ new IPythonEnumerator<'T> with
member x.MoveNext() =
python { match !state with
| MapState.NotStarted inp ->
let e = inp.GetEnumerator()
state := MapState.HaveEnumerator e
return! x.MoveNext()
| MapState.HaveEnumerator e ->
return
(if e.MoveNext() then
Some e.Current
else
x.Dispose()
None)
| _ -> return None }
member x.Dispose() =
match !state with
| MapState.HaveEnumerator e ->
state := MapState.Finished
dispose e
| _ -> () } }
let iteriPythonData f (source : PythonSeq<_>) =
python {
use ie = source.GetEnumerator()
let count = ref 0
let! move = ie.MoveNext()
let b = ref move
while b.Value.IsSome do
do! f !count b.Value.Value
let! moven = ie.MoveNext()
do incr count
b := moven
}
let iterPythonData (f: 'T -> PythonData<unit>) (inp: PythonSeq<'T>) = iteriPythonData (fun i x -> f x) inp
let iteri (f: int -> 'T -> unit) (inp: PythonSeq<'T>) = iteriPythonData (fun i x -> python.Return (f i x)) inp
// Add additional methods to the 'asyncSeq' computation builder
type PythonSeqBuilder with
member x.TryFinally (body: PythonSeq<'T>, compensation) =
tryFinally body compensation
member x.TryWith (body: PythonSeq<_>, handler: (exn -> PythonSeq<_>)) =
tryWith body handler
member x.Using (resource: 'T, binder: 'T -> PythonSeq<'U>) =
tryFinally (binder resource) (fun () ->
if box resource <> null then dispose resource)
member x.For (seq:seq<'T>, action:'T -> PythonSeq<'TResult>) =
collectSeq action seq
member x.For (seq:PythonSeq<'T>, action:'T -> PythonSeq<'TResult>) =
collect action seq
// Add asynchronous for loop to the 'async' computation builder
type PythonBuilder with
member internal x.For (seq:PythonSeq<'T>, action:'T -> PythonData<unit>) =
seq |> iterPythonData action
let rec unfoldPythonData (f:'State -> PythonData<('T * 'State) option>) (s:'State) : PythonSeq<'T> =
pythonSeq {
let s = ref s
let fin = ref false
while not !fin do
let! next = f !s
match next with
| None ->
fin := true
| Some (a,s') ->
yield a
s := s' }
let replicateInfinite (v:'T) : PythonSeq<'T> =
pythonSeq {
while true do
yield v }
let replicateInfinitePythonData (v:PythonData<'T>) : PythonSeq<'T> =
pythonSeq {
while true do
let! v = v
yield v }
let replicate (count:int) (v:'T) : PythonSeq<'T> =
pythonSeq {
for i in 1 .. count do
yield v }
// --------------------------------------------------------------------------
// Additional combinators (implemented as python/PythonSeq computations)
let mapPythonData f (source : PythonSeq<'T>) : PythonSeq<'TResult> = pythonSeq {
for itm in source do
let! v = f itm
yield v }
let mapiPythonData f (source : PythonSeq<'T>) : PythonSeq<'TResult> = pythonSeq {
let i = ref 0L
for itm in source do
let! v = f i.Value itm
i := i.Value + 1L
yield v }
let choosePythonData f (source : PythonSeq<'T>) : PythonSeq<'R> = pythonSeq {
for itm in source do
let! v = f itm
match v with
| Some v -> yield v
| _ -> () }
let filterPythonData f (source : PythonSeq<'T>) = pythonSeq {
for v in source do
let! b = f v
if b then yield v }
let tryLast (source : PythonSeq<'T>) = python {
use ie = source.GetEnumerator()
let! v = ie.MoveNext()
let b = ref v
let res = ref None
while b.Value.IsSome do
res := b.Value
let! moven = ie.MoveNext()
b := moven
return res.Value }
let lastOrDefault def (source : PythonSeq<'T>) = python {
let! v = tryLast source
match v with
| None -> return def
| Some v -> return v }
let tryFirst (source : PythonSeq<'T>) = python {
use ie = source.GetEnumerator()
let! v = ie.MoveNext()
let b = ref v
if b.Value.IsSome then
return b.Value
else
return None }
let firstOrDefault def (source : PythonSeq<'T>) = python {
let! v = tryFirst source
match v with
| None -> return def
| Some v -> return v }
let scanPythonData f (state:'TState) (source : PythonSeq<'T>) = pythonSeq {
yield state
let z = ref state
use ie = source.GetEnumerator()
let! moveRes0 = ie.MoveNext()
let b = ref moveRes0
while b.Value.IsSome do
let! zNext = f z.Value b.Value.Value
z := zNext
yield z.Value
let! moveResNext = ie.MoveNext()
b := moveResNext }
let pairwise (source : PythonSeq<'T>) = pythonSeq {
use ie = source.GetEnumerator()
let! v = ie.MoveNext()
let b = ref v
let prev = ref None
while b.Value.IsSome do
let v = b.Value.Value
match prev.Value with
| None -> ()
| Some p -> yield (p, v)
prev := Some v
let! moven = ie.MoveNext()
b := moven }
let pickPythonData (f:'T -> PythonData<'U option>) (source:PythonSeq<'T>) = python {
use ie = source.GetEnumerator()
let! v = ie.MoveNext()
let b = ref v
let res = ref None
while b.Value.IsSome && not res.Value.IsSome do
let! fv = f b.Value.Value
match fv with
| None ->
let! moven = ie.MoveNext()
b := moven
| Some _ as r ->
res := r
match res.Value with
| Some _ -> return res.Value.Value
| None -> return raise(System.Collections.Generic.KeyNotFoundException()) }
let pick f (source:PythonSeq<'T>) =
pickPythonData (f >> python.Return) source
let tryPickPythonData f (source : PythonSeq<'T>) = python {
use ie = source.GetEnumerator()
let! v = ie.MoveNext()
let b = ref v
let res = ref None
while b.Value.IsSome && not res.Value.IsSome do
let! fv = f b.Value.Value
match fv with
| None ->
let! moven = ie.MoveNext()
b := moven
| Some _ as r ->
res := r
return res.Value }
let tryPick f (source : PythonSeq<'T>) =
tryPickPythonData (f >> python.Return) source
let contains value (source : PythonSeq<'T>) =
source |> tryPick (fun v -> if v = value then Some () else None) |> PythonData.map Option.isSome
let tryFind f (source : PythonSeq<'T>) =
source |> tryPick (fun v -> if f v then Some v else None)
let exists f (source : PythonSeq<'T>) =
source |> tryFind f |> PythonData.map Option.isSome
let forall f (source : PythonSeq<'T>) =
source |> exists (f >> not) |> PythonData.map not
let foldPythonData f (state:'State) (source : PythonSeq<'T>) =
source |> scanPythonData f state |> lastOrDefault state
let fold f (state:'State) (source : PythonSeq<'T>) =
foldPythonData (fun st v -> f st v |> python.Return) state source
let length (source : PythonSeq<'T>) =
fold (fun st _ -> st + 1L) 0L source
let inline sum (source : PythonSeq<'T>) : PythonData<'T> =
(LanguagePrimitives.GenericZero, source) ||> fold (+)
let scan f (state:'State) (source : PythonSeq<'T>) =
scanPythonData (fun st v -> f st v |> python.Return) state source
let unfold f (state:'State) =
unfoldPythonData (f >> python.Return) state
let initInfinitePythonData f =
0L |> unfoldPythonData (fun n ->
python { let! x = f n
return Some (x,n+1L) })
let initPythonData (count:int64) f =
0L |> unfoldPythonData (fun n ->
python {
if n >= count then return None
else
let! x = f n
return Some (x,n+1L) })
let init count f =
initPythonData count (f >> python.Return)
let initInfinite f =
initInfinitePythonData (f >> python.Return)
let mapi f (source : PythonSeq<'T>) =
mapiPythonData (fun i x -> f i x |> python.Return) source
let map f (source : PythonSeq<'T>) =
mapPythonData (f >> python.Return) source
let indexed (source : PythonSeq<'T>) =
mapi (fun i x -> (i,x)) source
let iter f (source : PythonSeq<'T>) =
iterPythonData (f >> python.Return) source
let choose f (source : PythonSeq<'T>) =
choosePythonData (f >> python.Return) source
let filter f (source : PythonSeq<'T>) =
filterPythonData (f >> python.Return) source
let pythonSeq = new PythonSeq.PythonSeqBuilder()
[<AutoOpen>]
module PythonSeqExtensions =
// Add asynchronous for loop to the 'async' computation builder
type PythonBuilder with
member x.For (seq:PythonSeq<'T>, action:'T -> PythonData<unit>) =
seq |> PythonSeq.iterPythonData action
let asType<'t> item = (item >>?>> typeof<'t>) : 't
open Python.Runtime
let internal asEnumerable (p : PyObject) =
{ new System.Collections.Generic.IEnumerable<obj> with
override x.GetEnumerator() = x.GetEnumerator() :> System.Collections.IEnumerator
override x.GetEnumerator () = new PyIter(p) :> System.Collections.Generic.IEnumerator<obj> }
|> Seq.cast<PyObject>
let internal ofPyList (p : PyObject) =
PyList.AsList(p)
|> Seq.cast<PyObject>
|> Seq.toList
let internal ofPyDict (p : PyObject) =
let pyDict = new PyDict(p)
let d =
asEnumerable (pyDict.Keys())
|> Seq.map (fun k ->
let str = k.ToString()
str,
pyDict.[str])
|> dict
{ new System.Collections.Generic.IReadOnlyDictionary<string, PyObject> with
member x.TryGetValue(s, v) =
match d.TryGetValue(s) with
| true, m -> v <- m; true
| _ -> false
member x.ContainsKey k = d.ContainsKey k
member x.get_Item k =
match d.TryGetValue k with
| true, v -> v
| _ -> raise <| System.Collections.Generic.KeyNotFoundException(sprintf "Key '%s' was not found in object" k)
member x.Keys = d.Keys :> System.Collections.Generic.IEnumerable<_>
member x.Values = d.Values :> System.Collections.Generic.IEnumerable<_>
member x.Count = d.Count
member x.GetEnumerator() = d.GetEnumerator() :> System.Collections.IEnumerator
member x.GetEnumerator () = d.GetEnumerator() }