-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunc.jl
42 lines (33 loc) · 1.39 KB
/
Func.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
"""
A function operation that applies an arbitrary function to the input stream
and stores the result as the last value.
"""
mutable struct Func{T,TFunc,TIsValid} <: StreamOperation
const func::TFunc
const is_valid::TIsValid
last_value::T
function Func(func::TFunc, init::T; is_valid::TIsValid=!isnothing) where {T,TFunc,TIsValid}
new{T,TFunc,TIsValid}(func, is_valid, init)
end
function Func{T}(func::TFunc, init::T; is_valid::TIsValid=!isnothing) where {T,TFunc,TIsValid}
new{T,TFunc,TIsValid}(func, is_valid, init)
end
# function Func(func::TFunc) where {TFunc}
# new{Nothing,TFunc}(func, nothing)
# end
end
@inline has_output(op::Func{Nothing}) = false
@inline has_output(op::Func{T}) where {T} = true
# no functor call overload needed, func is directly called, see StreamGraph.jl: _gen_execute_call!
# @inline function (op::Func{Nothing,TFunc})(args...; kwargs...) where {TFunc}
# op.func(args...; kwargs...)
# nothing
# end
# @inline function (op::Func{T,TFunc})(args...; kwargs...) where {T,TFunc}
# op.last_value = op.func(args...; kwargs...)
# nothing
# end
@inline is_valid(op::Func{Nothing}) = true
# @inline is_valid(op::Func{T}) where {T} = !isnothing(op.last_value)
@inline is_valid(op::Func{T,TFunc,TIsValid}) where {T,TFunc,TIsValid} = op.is_valid(op.last_value)
@inline get_state(op::Func{T}) where {T} = op.last_value