-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create `construct` function Method ensure compatibility with types which do not define constructing instances of themselves. * Move lift functions to separate file * Attach docstring to more general `lift` method * Ensure curried-function only accepts types Co-authored-by: Jarrett Revels <[email protected]> Co-authored-by: Alex Arslan <[email protected]>
- Loading branch information
1 parent
1be43f7
commit f3bee00
Showing
4 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
lift(f, x) | ||
Return `f(x)` unless `x isa Union{Nothing,Missing}`, in which case return `missing`. | ||
This is particularly useful when handling values from `Arrow.Table`, whose null values | ||
may present as either `missing` or `nothing` depending on how the table itself was | ||
originally constructed. | ||
See also: [`construct`](@ref) | ||
""" | ||
lift(f, x) = f(x) | ||
lift(::Any, ::Union{Nothing,Missing}) = missing | ||
|
||
""" | ||
lift(f) | ||
Returns a curried function, `x -> lift(f,x)` | ||
""" | ||
lift(f) = Base.Fix1(lift, f) | ||
|
||
|
||
""" | ||
construct(T::Type, x) | ||
Construct `T(x)` unless `x` is of type `T`, in which case return `x` itself. Useful in | ||
conjunction with the [`lift`](@ref) function for types which don't have a constructor which | ||
accepts instances of itself (e.g. `T(::T)`). | ||
## Examples | ||
```jldoctest | ||
julia> using Legolas: construct | ||
julia> construct(Float64, 1) | ||
1.0 | ||
julia> Some(Some(1)) | ||
Some(Some(1)) | ||
julia> construct(Some, Some(1)) | ||
Some(1) | ||
``` | ||
Use the curried form when using `lift`: | ||
```jldoctest | ||
julia> using Legolas: lift, construct | ||
julia> lift(Some, Some(1)) | ||
Some(Some(1)) | ||
julia> lift(construct(Some), Some(1)) | ||
Some(1) | ||
``` | ||
""" | ||
construct(T::Type, x) = T(x) | ||
construct(::Type{T}, x::T) where {T} = x | ||
construct(T::Type) = Base.Fix1(construct, T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f3bee00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
f3bee00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/60666
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: