/
core.jl
1201 lines (935 loc) · 31.8 KB
/
core.jl
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --- Types
"""
Reduced
The type signaling transducible processes to abort.
!!! note
Call [`reduced`](@ref) function for aborting the transducible
process since [`reduced`](@ref) makes sure `x` is not doubly
wrapped. `Reduced` is meant to be used as `x isa Reduced` for
checking if the result from [`transduce`](@ref) is due to early
termination.
See [`reduced`](@ref), [`unreduced`](@ref).
# Examples
```jldoctest
julia> using Transducers
julia> function step_demo(y, x)
if x > 5
return reduced(y)
else
return y + x
end
end;
julia> result = transduce(Map(identity), Completing(step_demo), 0, 1:10)
Reduced(15)
julia> result isa Reduced
true
julia> unreduced(result)
15
julia> result = transduce(Map(identity), Completing(step_demo), 0, 1:4)
10
julia> result isa Reduced
false
julia> unreduced(result)
10
```
"""
struct Reduced{T}
value::T
end
Base.:(==)(x::Reduced, y::Reduced) = x.value == y.value
Base.show(io::IO, x::Reduced) = _default_show(io, x)
isreduced(::Reduced) = true
isreduced(::Any) = false
"""
reduced([x = nothing])
Stop transducible process with the final value `x` (default:
`nothing`). Return `x` as-is if it already is a `reduced` value.
See [`Reduced`](@ref), [`unreduced`](@ref).
$(_thx_clj("ensure-reduced"))
# Examples
```jldoctest
julia> using Transducers
julia> foldl(Enumerate(), "abcdef"; init=0) do y, (i, x)
if x == 'd'
return reduced(y)
end
return y + i
end
6
julia> foreach(Enumerate(), "abc") do (i, x)
println(i, ' ', x)
if x == 'b'
return reduced()
end
end;
1 a
2 b
```
"""
reduced(x::Reduced) = x
reduced(x) = Reduced(x)
reduced() = reduced(nothing)
"""
unreduced(x)
Unwrap `x` if it is a `Reduced`; do nothing otherwise.
See [`Reduced`](@ref), [`reduced`](@ref).
$(_thx_clj("unreduced"))
"""
unreduced(x::Reduced) = x.value
unreduced(x) = x
"""
@return_if_reduced expr
It transforms the given expression to:
```julia
val = expr
val isa Reduced && return val
val
```
See also [`@next`](@ref).
!!! compat "Transducers.jl 0.3"
In v0.2, the calling convention was `@return_if_reduced
complete(rf, val)` and it was transformed to `val isa Reduced &&
return reduced(complete(rf, unreduced(val)))`. For the rationale
behind the change, see [this commit
message](https://github.com/JuliaFolds/Transducers.jl/commit/74f8961fea97b746cb097b27aa5a5761e9bf4dae).
# Examples
```jldoctest; filter = [r"(var\\")?#[0-9]+#val(\\")?", r"#=.*?=#"]
julia> using Transducers: @return_if_reduced
julia> @macroexpand @return_if_reduced f(x)
quote
#158#val = f(x)
#= ... =#
begin
#158#val isa Transducers.Reduced && return #158#val
#= ... =#
#158#val
end
end
```
"""
macro return_if_reduced(ex)
code = :(val isa Reduced && return val; val)
if ex isa Expr && ex.head == :call && length(ex.args) == 3
val = esc(ex)
return quote
if $(esc(ex.args[1])) === complete
error("""
Calling `@return_if_reduced complete(rf, val)` is now an error.
Please use `@return_if_reduced val`.
""")
end
val = $val
$code
end
end
val = esc(ex)
:(val = $val; $code)
end
"""
@next(rf, state, input)
It is expanded to
```julia
result = next(rf, state, input)
result isa Reduced && return result
result
```
This is usually the best way to call `next` as checking for `Reduced`
is required to support early termination.
See also: [`next`](@ref), [`Reduced`](@ref), [`@return_if_reduced`](@ref).
"""
macro next(rf, state, input)
quote
result = next($(esc.((rf, state, input))...))
result isa Reduced && return result
result
end
end
abstract type Transducer end
abstract type AbstractFilter <: Transducer end
struct Composition{XO <: Transducer, XI <: Transducer} <: Transducer
outer::XO
inner::XI
end
struct IdentityTransducer <: Transducer end
has(xf::Transducer, T::Type) = xf isa T
has(xf::Composition, T::Type) = has(xf.outer, T) || has(xf.inner, T)
Base.broadcastable(xf::Transducer) = Ref(xf)
"""
Transducer
The abstract type for transducers.
A transducer `xf` can be used as both iterator transformation
`xf(itr)` and reducing function transformation `xf'(rf)`.
See also [`adjoint`](@ref) for `xf'(rf)`.
!!! compat "Transducers.jl 0.4.39"
The call overload `xf(rf)` requires Transducers.jl 0.4.39 or later.
!!! note
The call overload `xf(rf)` requires Julia 1.3 or later.
For older Julia versions, use [`eduction`](@ref).
# Examples
```jldoctest
julia> using Transducers
julia> xs = Map(inv)(2:2:4)
2-element StepRange{Int64,Int64} |>
Map(inv)
julia> collect(xs)
2-element Array{Float64,1}:
0.5
0.25
julia> rf = Map(inv)'(+)
Reduction(
Map(inv),
BottomRF(
+))
julia> rf(1, 4) # +(1, inv(4))
1.25
```
"""
Transducer
"""
AbstractFilter <: Transducer
The abstract type for filter-like transducers.
"""
AbstractFilter
abstract type AbstractReduction{innertype} <: Function end
if VERSION >= v"1.3" # post https://github.com/JuliaLang/julia/pull/31916
@inline (rf::AbstractReduction)(state, input) = next(rf, state, input)
end
InnerType(::Type{<:AbstractReduction{T}}) where T = T
ConstructionBase.constructorof(::Type{T}) where {T <: AbstractReduction} = T
"""
Transducers.inner(rf::R_)
Return the inner reducing function of `rf`.
"""
inner(rf::AbstractReduction) = rf.inner
"""
Transducers.xform(rf::R_{X}) -> xf :: X
Return the transducer `xf` associated with `rf`. Returned transducer
`xf` is "atomic"; i.e., it is not a `Composition` transducer type.
"""
xform(rf::AbstractReduction) = rf.xform
has(rf::AbstractReduction, T::Type{<:Transducer}) = has(Transducer(rf), T)
struct BottomRF{F} <: AbstractReduction{F}
inner::F
end
ensurerf(rf::AbstractReduction) = rf
ensurerf(f) = BottomRF(f)
# Not calling rf.inner(result, input) etc. directly since it can be
# `Completing` etc.
start(rf::BottomRF, result) = start(inner(rf), result)
@inline next(rf::BottomRF, result, input) = next(inner(rf), result, input)
complete(rf::BottomRF, result) = complete(inner(rf), result)
combine(rf::BottomRF, a, b) = combine(inner(rf), a, b)
Transducer(::BottomRF) = IdentityTransducer()
as(rf::T, ::Type{T}) where T = rf
as(rf, T::Type) = as(inner(rf), T)
# In clojure a reduction function is one of signature
# whatever, input -> whatever
#
# The `Reduction` type corresponds to such a function, but keeps extra information:
# * `xform` and `inner` are a decomposition of the reduction function into
# a transducer `xform` and an inner reduction function `inner`.
# `inner` can be either a `Reduction` or a function with arity-2 and arity-1 methods
#
struct Reduction{X <: Transducer, I} <: AbstractReduction{I}
xform::X
inner::I
Reduction{X, I}(xf, inner) where {X, I} = new{X, I}(xf, inner)
Reduction(xf::X, inner::I) where {X <: Transducer, I} =
if I <: AbstractReduction
new{X, I}(xf, inner)
else
rf = ensurerf(inner)
new{X, typeof(rf)}(xf, rf)
end
end
if VERSION < v"1.3" # pre https://github.com/JuliaLang/julia/pull/31916
@inline (rf::Reduction)(state, input) = next(rf, state, input)
end
prependxf(rf::AbstractReduction, xf) = Reduction(xf, rf)
setinner(rf::Reduction, inner) = Reduction(xform(rf), inner)
setxform(rf::Reduction, xform) = Reduction(xform, inner(rf))
Transducer(rf::Reduction) =
if inner(rf) isa BottomRF
xform(rf)
else
Composition(xform(rf), Transducer(inner(rf)))
end
# This is a non-ideal definition as it may not return a `Reduction`.
# Making this less non-ideal requires to replace all call/overloads of
# `Reduction` to `AbstractReduction`.
Reduction(::IdentityTransducer, inner) = ensurerf(inner)
"""
Transducers.R_{X}
When defining a transducer type `X`, it is often required to dispatch
on type `rf::R_{X}` (Reducing Function) which bundles the current
transducer `xform(rf)::X` and the inner reducing function
`inner(rf)::R_`.
"""
const R_{X} = Reduction{<:X}
@inline function Reduction(xf_::Composition, f)
xf = _normalize(xf_)
# @assert !(xf.outer isa Composition)
return Reduction(xf.outer, Reduction(xf.inner, f))
end
@inline _normalize(xf) = xf
@inline _normalize(xf::Composition{<:Composition}) = xf.outer |> xf.inner
"""
f ⨟ g
g ∘ f
opcompose(f, g)
compose(g, f)
Composition of transducers.
!!! compat "Transducers.jl 0.4.39"
Transducers.jl 0.4.39 or later is required for composing
transducers with `∘` and other operators and functions derived
from it.
Transducers written as `f |> g |> h` in previous versions of
Transducers.jl can now be written as `f ⨟ g ⨟ h` (in Julia 1.5 or
later) or `opcompose(f, g, h)`.
!!! note
"op" in `opcompose` does not stand for _operator_; it stands for
_opposite_.
"""
@inline Base.:∘(g::Transducer, f::Transducer) = Composition(f, g)
@inline Base.:∘(g::Transducer, f::Composition) = g ∘ f.inner ∘ f.outer
@inline Base.:∘(f::Transducer, ::IdentityTransducer) = f
@inline Base.:∘(::IdentityTransducer, f::Transducer) = f
@inline Base.:∘(f::IdentityTransducer, ::IdentityTransducer) = f
@inline Base.:∘(::IdentityTransducer, f::Composition) = f # disambiguation
if VERSION >= v"1.3"
(xf::Transducer)(itr) = eduction(xf, itr)
else
Base.:|>(itr, xf::Transducer) = eduction(xf, itr)
end
"""
ReducingFunctionTransform(xf)
The "true" transducer.
"""
struct ReducingFunctionTransform{T <: Transducer} <: Function
xf::T
end
"""
xf'
`xf'(rf₁)` is a shortcut for calling `reducingfunction(xf, rf₁)`.
More precisely, adjoint `xf′` of a transducer `xf` is a _reducing
function transform_ `rf₁ -> rf₂`. That is to say, `xf'` a function
that maps a reducing function `rf₁` to another reducing function
`rf₂`.
# Examples
```jldoctest
julia> using Transducers
julia> y = Map(inv)'(+)(10, 2)
10.5
julia> y == +(10, inv(2))
true
```
"""
Base.adjoint(xf::Transducer) = ReducingFunctionTransform(xf)
Base.adjoint(rxf::ReducingFunctionTransform) = rxf.xf
(f::ReducingFunctionTransform)(rf; kwargs...) =
reducingfunction(f.xf, rf; kwargs...)
@inline Base.:∘(f::ReducingFunctionTransform, g::ReducingFunctionTransform) = (g' ∘ f')'
"""
reform(rf, f)
Reset "bottom" reducing function of `rf` to `f`.
"""
reform(rf::Reduction, f) = Reduction(xform(rf), reform(inner(rf), f))
reform(rf::BottomRF, f) = BottomRF(reform(inner(rf), f))
reform(::Any, f) = f
"""
Transducers.start(rf::R_{X}, state)
This is an optional interface for a transducer. Default
implementation just calls `start` of the inner reducing function; i.e.,
```julia
start(rf::Reduction, result) = start(inner(rf), result)
```
If the transducer `X` is stateful, it can "bundle" its private state
with `wrap`:
```julia
start(rf::R_{X}, result) = wrap(rf, PRIVATE_STATE, start(inner(rf), result))
```
where `PRIVATE_STATE` is an initial value for the private state that
can be used inside [`next`](@ref) via [`wrapping`](@ref).
See [`Take`](@ref), [`PartitionBy`](@ref), etc. for real-world examples.
Side notes: There is no related API in Clojure's Transducers.
Transducers.jl uses it to implement stateful transducers using "pure"
functions. The idea is based on a slightly different approach taken
in C++ Transducer library [atria](https://github.com/AbletonAG/atria).
"""
start(rf, init) = initialize(init, rf)
start(rf::Reduction, result) = start(inner(rf), result)
start(rf::R_{AbstractFilter}, result) = start(inner(rf), result)
"""
Transducers.next(rf::R_{X}, state, input)
This is the only required interface. It takes the following form
(if `start` is not defined):
```julia
next(rf::R_{X}, result, input) =
# code calling next(inner(rf), result, possibly_modified_input)
```
When calling `next`, it is almost always a better idea to use the
macro form [`@next`](@ref). See the details in its documentation.
See [`Map`](@ref), [`Filter`](@ref), [`Cat`](@ref), etc. for
real-world examples.
"""
@inline next(f, result, input) = f(result, input)
# done(rf, result)
"""
Transducers.complete(rf::R_{X}, state)
This is an optional interface for a transducer. If transducer `X` has
some internal state, this is the last chance to "flush" the result.
See [`PartitionBy`](@ref), etc. for real-world examples.
If `start(rf::R_{X}, state)` is defined, `complete` **must** unwarp
`state` before returning `state` to the outer reducing function.
!!! compat "Transducers.jl 0.3"
In Transducers.jl 0.2, `complete` had a fallback implementation
to automatically call `unwrap` when `wrap` is called in `start`.
Relying on this fallback implementation is now deprecated.
"""
complete(f, result) = f(result)
complete(rf::AbstractReduction, result) =
# Not using dispatch to avoid ambiguity
if ownsstate(rf, result)
Base.depwarn(
string(
"`complete` for ", typeof(xform(rf)), " is not defined.",
" Automatic implementation of `complete` method will be",
" disabled in the future."
),
:complete)
complete(inner(rf), unwrap(rf, result)[2])
else
complete(inner(rf), result)
end
"""
Transducers.combine(rf::R_{X}, state_left, state_right)
This is an optional interface for a transducer. If transducer `X` is
stateful (i.e., [`wrap`](@ref) is used in [`start`](@ref)), it has to
be able to combine the private states to support fold functions that
require an associative reducing function such as [`foldxt`](@ref).
Typical implementation takes the following form:
```julia
function combine(rf::R_{X}, a, b)
# ,---- `ua` and `ub` are the private state of the transducer `X`
# / ,-- `ira` and `irb` are the states of inner reducing functions
# / /
ua, ira = unwrap(rf, a)
ub, irb = unwrap(rf, b)
irc = combine(inner(rf), ira, irb)
uc = # somehow combine private states `ua` and `ub`
return wrap(rf, uc, irc)
end
```
See [`ScanEmit`](@ref), etc. for real-world examples.
"""
combine(f, a, b) = f(a, b)
combine(rf::Reduction, a, b) =
# Not using dispatch to avoid ambiguity
if ownsstate(rf, a)
# TODO: make sure this branch is compiled out
error("Stateful transducer ", xform(rf), " does not support `combine`")
elseif ownsstate(rf, b)
error("""
Some thing went wrong in two ways:
* `combine(rf, a, b)` is called but type of `a` and `b` are different.
* `xform(rf) = $(xform(rf))` is stateful and does not support `combine`.
""")
else
combine(inner(rf), a, b)
end
is_prelude(_) = false
is_prelude(::InitialValues.InitialValue) = true
is_prelude(xs::Tuple) = any(map(is_prelude, xs))
is_prelude(xs::NamedTuple) = is_prelude(Tuple(xs))
privatestate(::T, state, result) where {T <: AbstractReduction} =
privatestate(T, state, result)
struct PrivateState{T, S, R}
state::S
result::R
# Rename constructor to make sure that it is always constructed
# through the factory function:
global privatestate(::Type{T}, state::S, result::R) where {
T <: AbstractReduction,
S,
R,
} =
new{T, S, R}(state, result)
end
# TODO: make it a tuple-like so that I can return it as-is
ConstructionBase.constructorof(::Type{<:PrivateState{T}}) where T =
(state, result) -> privatestate(T, state, result)
@inline psstate(ps) = ps.state
@inline psresult(ps) = ps.result
@inline setpsstate(ps, x) = @set ps.state = x
@inline setpsresult(ps, x) = @set ps.result = x
ownsstate(::Any, ::Any) = false
ownsstate(::R, ::PrivateState{T}) where {R, T} = R === T
# Using `result isa PrivateState{typeof(rf)}` makes it impossible to
# compile Extrema examples in ../examples/tutorial_missings.jl (it
# took more than 10 min). See also:
# https://github.com/JuliaLang/julia/issues/30125
@inline is_prelude(ps::PrivateState) = is_prelude(psstate(ps)) || is_prelude(psresult(ps))
"""
unwrap(rf, result)
Unwrap [`wrap`](@ref)ed `result` to a private state and inner result.
Following identity holds:
```julia
unwrap(rf, wrap(rf, state, iresult)) == (state, iresult)
```
This is intended to be used only in [`complete`](@ref). Inside
[`next`](@ref), use [`wrapping`](@ref).
"""
unwrap(::T, ps::PrivateState{T}) where {T} = (psstate(ps), psresult(ps))
unwrap(::T1, ::PrivateState{T2}) where {T1, T2} =
error("""
`unwrap(rf1, ps)` is used for
typeof(rf1) = $T1
while `ps` is created by wrap(rf2, ...) where
typeof(rf2) = $T2
""")
# TODO: better error message with unmatched `T`
"""
wrap(rf::R_{X}, state, iresult)
Pack private `state` for reducing function `rf` (or rather the
transducer `X`) with the result `iresult` returned from the inner
reducing function `inner(rf)`. This packed result is typically passed
to the outer reducing function.
This is intended to be used only in [`start`](@ref). Inside
[`next`](@ref), use [`wrapping`](@ref).
!!! note "Implementation detail"
If `iresult` is a [`Reduced`](@ref), `wrap` actually _un_wraps all
internal state `iresult` recursively. However, this is an
implementation detail that should not matter when writing
transducers.
Consider a reducing step constructed as
rf = opcompose(xf₁, xf₂, xf₃)'(f)
where each `xfₙ` is a stateful transducer and hence needs a private
state `stateₙ` and this `stateₙ` is constructed in each
`start(::R_{typeof(xfₙ)}, result)`. Then, calling `start(rf,
result))` is equivalent to
```julia
wrap(rf,
state₁, # private state for xf₁
wrap(inner(rf),
state₂, # private state for xf₂
wrap(inner(inner(rf)),
state₃, # private state for xf₃
result)))
```
or equivalently
```julia
result₃ = result
result₂ = wrap(inner(inner(rf)), state₃, result₃)
result₁ = wrap(inner(rf), state₂, result₂)
result₀ = wrap(rf, state₁, result₁)
```
The inner most step function receives the original `result` as the
first argument while transducible processes such as [`foldl`](@ref)
only sees the outer-most "tree" `result₀` during the reduction.
See [`wrapping`](@ref), [`unwrap`](@ref), and [`start`](@ref).
"""
wrap(rf::T, state, iresult) where {T} = privatestate(rf, state, iresult)
wrap(rf, state, iresult::Reduced) = iresult
"""
wrapping(f, rf, result)
Function `f` must take two argument `state` and `iresult`, and return
a tuple `(state, iresult)`. This is intended to be used only in
[`next`](@ref), possibly with a `do` block.
```julia
next(rf::R_{MyTransducer}, result, input) =
wrapping(rf, result) do my_state, iresult
# code calling `next(inner(rf), iresult, possibly_modified_input)`
return my_state, iresult # possibly modified
end
```
See [`wrap`](@ref), [`unwrap`](@ref), and [`next`](@ref).
"""
@inline function wrapping(f, rf, result)
state0, iresult0 = unwrap(rf, result)
state1, iresult1 = f(state0, iresult0)
return wrap(rf, state1, iresult1)
end
unwrap_all(ps::PrivateState) = unwrap_all(psresult(ps))
unwrap_all(result) = result
unwrap_all(ps::Reduced) = Reduced(unwrap_all(unreduced(ps)))
# isexpansive(::Any) = true
isexpansive(::Transducer) = true
isexpansive(::AbstractFilter) = false
# isexpansive(rf::Reduction) = isexpansive(xform(rf)) || isexpansive(inner(rf))
isexpansive(xf::Composition) = isexpansive(xf.outer) || isexpansive(xf.inner)
# Should it be a type-level trait?
#=
iscontractive(::Any) = false
iscontractive(::AbstractFilter) = true
iscontractive(rf::Reduction) = iscontractive(xform(rf)) && iscontractive(inner(rf))
=#
struct NoComplete <: Transducer end
next(rf::R_{NoComplete}, result, input) = next(inner(rf), result, input)
complete(::R_{NoComplete}, result) = result # don't call inner complete
"""
Completing(function)
Wrap a `function` to add a no-op [`complete`](@ref) protocol. Use it
when passing a `function` without unary method to [`transduce`](@ref)
etc.
$(_thx_clj("completing"))
"""
struct Completing{F} <: _Function # Note: not a Transducer
f::F
end
start(rf::Completing, result) = start(rf.f, result)
@inline next(rf::Completing, result, input) = next(rf.f, result, input)
complete(::Completing, result) = result
combine(rf::Completing, a, b) = combine(rf.f, a, b)
# Apply `Completing` only on the inner-most reducing function so that
# `complete` on transducers are still called. This is very ugly as it
# does not return a `Completing` object. But this is required for
# allowing `foldl(reducingfunction(...), ...)` etc.:
Completing(rf::AbstractReduction) = setinner(rf, Completing(inner(rf)))
Completing(rf::BottomRF) = BottomRF(Completing(rf.inner))
Completing(f::Completing) = f
# Currently, `Completing` is recursive while `skipcomplete` is
# non-recursive. `Completing` only skips `complete` on the inner-most
# (bottom) reducing function (i.e., _not_ including the
# transducers). `skipcomplete` skips `complete` of the outer-most
# reducing function (i.e., including all transducers).
# TODOs for `Completing` and `skipcomplete`:
# 1. Get rid of 1-arg `complete` fallback definition for bottom functions.
# 2. Deprecate `Completing` (after making it a no-op).
skipcomplete(rf::Reduction) = Reduction(NoComplete(), rf)
skipcomplete(f) = Completing(f)
# Since `Completing <: Function`, the default `show` is a bit ugly.
function Base.show(io::IO, rf::Completing)
@nospecialize
if rf === Completing(rf.f)
print(io, Completing, '(', rf.f, ')')
else
invoke(show, Tuple{IO,Any}, io, rf)
end
end
struct SideEffect{F} # Note: not a Transducer
f::F
end
# Completing(rf::SideEffect) = rf
start(rf::SideEffect, result) = start(rf.f, result)
complete(::SideEffect, result) = result
@inline next(rf::SideEffect, _, input) = rf.f(input)
"""
right([l, ]r) -> r
It is simply defined as
```julia
right(l, r) = r
right(r) = r
```
This function is meant to be used as `step` argument for
[`foldl`](@ref) etc. for extracting the last output of the
transducers.
!!! compat "Transducers.jl 0.3"
Initial value must be manually specified. In 0.2, it was
automatically set to `nothing`.
# Examples
```jldoctest
julia> using Transducers
julia> foldl(right, Take(5), 1:10)
5
julia> foldl(right, Drop(5), 1:3; init=0) # using `init` as the default value
0
```
"""
right(l, r) = r
right(r) = r
InitialValues.@def_monoid right
identityof(::typeof(right), ::Any) = nothing
# This is just a right identity but `right` is useful for left-fold
# context anyway so I guess it's fine.
abstract type Reducible end
abstract type Foldable <: Reducible end
asfoldable(x) = x
"""
initvalue(initializer::AbstractInitializer) -> init
initvalue(init) -> init
Materialize the initial value if the input is an `AbstractInitializer`.
Return the input as-is if not.
"""
initvalue(x) = x
_initvalue(rf::Reduction) = initvalue(xform(rf).init)
abstract type AbstractInitializer end
# TODO: Merge `InitOf` to `AbstractInitializer`
# For `Init`, `DefaultInit` and `OptInit`
struct InitOf{IV <: SpecificInitialValue} end
(::InitOf{IV})(::OP) where {IV, OP} = IV{OP}()
# For `Broadcasting`:
Broadcast.broadcastable(f::AbstractInitializer) = Ref(f)
Broadcast.broadcastable(f::InitOf) = Ref(f)
"""
initialize(initializer, op) -> init
initialize(init, _) -> init
Return an initial value for `op`. Throw an error if `initializer`
(e.g., `Init`) creates unknown initial value.
# Examples
```jldoctest
julia> using Transducers
using Transducers: initialize
julia> initialize(Init, +)
InitialValue(+)
julia> initialize(123, +)
123
julia> unknown_op(x, y) = x + 2y;
julia> initialize(Init, unknown_op)
ERROR: IdentityNotDefinedError: `init = Transducers.Init` is specified but the identity element `InitialValue(op)` is not defined for
op = unknown_op
[...]
```
"""
initialize(init, op) = init
initialize(f::InitOf, op) = check_init(f(op), f, op)
initialize(init::AbstractInitializer, _) = initvalue(init)
function check_init(init::SpecificInitialValue, f, op)
InitialValues.isknown(init) || throw(IdentityNotDefinedError(op, f))
return init
end
"""
OnInit(f)
Call a callable `f` to create an initial value.
See also [`CopyInit`](@ref).
`OnInit` or `CopyInit` must be used whenever using in-place reduction
with [`foldxt`](@ref) etc.
# Examples
```jldoctest OnInit
julia> using Transducers
julia> xf1 = Scan(push!, [])
Scan(push!, Any[])
julia> foldl(right, xf1, 1:3)
3-element Array{Any,1}:
1
2
3
julia> xf1
Scan(push!, Any[1, 2, 3])
```
Notice that the array is stored in `xf1` and mutated in-place. As a
result, second run of `foldl` contains the results from the first
run:
```jldoctest OnInit
julia> foldl(right, xf1, 10:11)
5-element Array{Any,1}:
1
2
3
10
11
```
This may not be desired. To avoid this behavior, create an `OnInit`
object which takes a factory function to create a new initial value.
```jldoctest OnInit; filter = r"#+[0-9]+(\\(\\))?"
julia> xf2 = Scan(push!, OnInit(() -> []))
Scan(push!, OnInit(##9#10()))
julia> foldl(right, xf2, 1:3)
3-element Array{Any,1}:
1
2
3
julia> foldl(right, xf2, [10.0, 11.0])
2-element Array{Any,1}:
10.0
11.0
```
Keyword argument `init` for transducible processes also accept an
`OnInit`:
```jldoctest OnInit
julia> foldl(push!, Map(identity), "abc"; init=OnInit(() -> []))
3-element Array{Any,1}:
'a'
'b'
'c'
```
To create a copy of a mutable object, [`CopyInit`](@ref) is easier to
use.
However, more powerful and generic pattern is to use `push!!` from
BangBang.jl and initialize `init` with `Union{}[]` so that it
automatically finds the minimal element type.
```jldoctest OnInit
julia> using BangBang
julia> foldl(push!!, Map(identity), "abc"; init=Union{}[])
3-element Array{Char,1}:
'a'
'b'
'c'
```
"""
struct OnInit{F} <: AbstractInitializer
f::F
end
initvalue(init::OnInit) = init.f()
Base.show(io::IO, init::OnInit) = _default_show(io, init)
"""
CopyInit(value)
This is equivalent to `OnInit(() -> deepcopy(value))`.
!!! compat "Transducers.jl 0.3"
New in version 0.3.
# Examples
```jldoctest
julia> using Transducers
julia> init = CopyInit([]);
julia> foldl(push!, Map(identity), 1:3; init=init)