Skip to content

Commit 0cc4702

Browse files
Stephan SahmStephan Sahm
authored andcommitted
adds docstrings everywhere
plus improves API index
1 parent d8ce2a2 commit 0cc4702

10 files changed

Lines changed: 375 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ For more details check out the [documentation](https://schlichtanders.github.io/
1515
The package is soon going to be registered at General, until then you can use it by adding a custom registry.
1616
```julia
1717
using Pkg
18-
pkg"registry add https://github.com/JuliaRegistries/General" # central julia repository
19-
pkg"registry add https://github.com/schlichtanders/SchlichtandersJuliaRegistry.jl" # custom repository
18+
pkg"registry add https://github.com/JuliaRegistries/General" # central julia registry
19+
pkg"registry add https://github.com/schlichtanders/SchlichtandersJuliaRegistry.jl" # custom registry
2020
pkg"add DataTypesBasic"
2121
```
2222

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This package defines julia implementations for the common types `Option` (aka `M
77

88
```julia
99
using Pkg
10-
pkg"registry add https://github.com/JuliaRegistries/General" # central julia repository
11-
pkg"registry add https://github.com/schlichtanders/SchlichtandersJuliaRegistry.jl" # custom repository
10+
pkg"registry add https://github.com/JuliaRegistries/General" # central julia registry
11+
pkg"registry add https://github.com/schlichtanders/SchlichtandersJuliaRegistry.jl" # custom registry
1212
pkg"add DataTypesBasic"
1313
```
1414

docs/src/library.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,62 @@ CurrentModule = DataTypesBasic
77
```@index
88
```
99

10-
```@autodocs
11-
Modules = [DataTypesBasic]
10+
## Identity
11+
12+
```@docs
13+
Identity
14+
isidentity
15+
```
16+
17+
## Const
18+
19+
```@docs
20+
Const
21+
Base.isconst(::Const)
22+
```
23+
24+
## Option
25+
26+
```@docs
27+
Option
28+
isoption
29+
issome
30+
isnone
31+
iftrue
32+
iffalse
33+
```
34+
35+
## Either
36+
37+
```@docs
38+
Either
39+
iseither
40+
isleft
41+
isright
42+
either
43+
@either
44+
getleft
45+
getright
46+
getleftOption
47+
getrightOption
48+
getOption
49+
```
50+
51+
## Try
52+
53+
```@docs
54+
Try
55+
@Try
56+
@TryCatch
57+
istry
58+
issuccess
59+
isfailure
60+
getOption
61+
```
62+
63+
## ContextManager
64+
```@docs
65+
ContextManager
66+
@ContextManager
67+
Base.run(::ContextManager)
1268
```

src/Const.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Base.isconst(other) = false
2626

2727
Base.length(::Const) = 0
2828

29+
# as the length of Const is 0, we intentionally do not support `Base.get`
30+
2931
# const just does nothing, i.e. leaves everything constant
3032
Base.iterate(c::Const) = nothing
3133
Base.foreach(f, c::Const) = nothing

src/ContextManager.jl

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""
2-
As ContextManager we denote a computation which has a pre-computation and possibly a cleaning up step
3-
when run with x->x it is supposed to return x.
4-
"""
1+
@doc raw"""
2+
ContextManager(func)
53
6-
"""
7-
function which expects one argument, which itself is a function. Think of it like the following:
8-
```
4+
As ContextManager we denote a computation which has a pre-computation and possibly a cleaning up step.
5+
6+
The single argument is supposed to be a function which expects one argument, the continuation function.
7+
Think of it like the following:
8+
```julia
99
function contextmanagerready(cont)
1010
# ... do something before
1111
value = ... # create some value to work on later
@@ -15,13 +15,57 @@ function contextmanagerready(cont)
1515
end
1616
```
1717
Now you can wrap it into `ContextManager(contextmanagerready)` and you can use all the context manager
18-
functionalities right away
18+
functionalities right away.
19+
20+
There is a simple `@ContextManager` for writing less parentheses
21+
```julia
22+
mycontextmanager(value) = @ContextManager function(cont)
23+
println("got value = $value")
24+
result = cont(value)
25+
println("finished value = $value")
26+
result
27+
end
28+
```
29+
30+
----------------
31+
32+
You can run it in two ways, either by just passing `Base.identity` as the continuation function
33+
```julia
34+
julia> mycontextmanager(4)(x -> x)
35+
got value = 4
36+
finished value = 4
37+
```
38+
or for convenience we also overload `Base.run`
39+
```julia
40+
# without any extra arguments runs the contextmanager with Base.identity
41+
run(mycontextmanager(4))
42+
# also works with a given continuation, which makes for a nice do-syntax
43+
run(x -> x, mycontextmanager(4))
44+
run(mycontextmanager(4)) do x
45+
x
46+
end
47+
```
1948
"""
2049
struct ContextManager{F}
2150
f::F
2251
end
2352

24-
# one pair of parantheses less
53+
54+
55+
"""
56+
@ContextManager function(cont); ...; end
57+
58+
There is a simple `@ContextManager` for writing less parentheses
59+
60+
```julia
61+
mycontextmanager(value) = @ContextManager function(cont)
62+
println("got value = $value")
63+
result = cont(value)
64+
println("finished value = $value")
65+
result
66+
end
67+
```
68+
"""
2569
macro ContextManager(func)
2670
quote
2771
ContextManager($(esc(func)))

src/DataTypesBasic.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ higher code-reuse and datatype-reuse.
3434
module DataTypesBasic
3535

3636
export Identity, isidentity, Const,
37-
Option, isoption, iftrue, iffalse, getOption, # isnothing, Nothing, Some comes from Base
38-
Either, either, @either, iseither, isleft, isright, getleft, getright, getleftOption, getrightOption, flip_left_right,
39-
Try, Thrown, @Try, @TryCatch, issuccess, isexception, MultipleExceptions,
37+
Option, isoption, issome, isnone, iftrue, iffalse, getOption,
38+
Either, either, @either, iseither, isleft, isright, getleft, getright, getOption, getleftOption, getrightOption, flip_left_right,
39+
Try, @Try, @TryCatch, istry, issuccess, isexception, Thrown, MultipleExceptions,
4040
ContextManager, @ContextManager
4141

4242
using Compat

src/Either.jl

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
Either{L, R} = Union{Const{L}, Identity{R}}
3+
Either{Int, Bool}(true) == Identity(true)
4+
Either{Int, Bool}(1) == Const(1)
5+
Either{String}("left") == Const("left")
6+
Either{String}(:anythingelse) == Identity(:anythingelse)
7+
8+
A very common type to represent Result or Failure.
9+
We reuse [`Identity`](@ref) as representing "Success" and [`Const`](@ref) for "Failure".
10+
"""
111
const Either{L, R} = Union{Const{L}, Identity{R}}
212
Either{L, R}(x::L) where {L, R} = Const(x)
313
Either{L, R}(x::R) where {L, R} = Identity(x)
@@ -12,6 +22,18 @@ Base.eltype(::Type{Either}) = Any
1222
# Helpers for Either
1323
# ------------------
1424

25+
"""
26+
either(:left, bool_condition, "right")
27+
28+
If `bool_condition` is true, it returns the right value, wrapped into [Identity](@ref).
29+
Else returns left side, wrapped into [Const](@ref).
30+
31+
Example
32+
-------
33+
```jldoctest
34+
either(:left, 1 < 2, "right") == Identity("right")
35+
```
36+
"""
1537
function either(left_false, comparison::Bool, right_true)
1638
comparison ? Identity(right_true) : Const(left_false)
1739
end
@@ -36,27 +58,91 @@ macro either(expr::Expr)
3658
end)
3759
end
3860

61+
62+
"""
63+
flip_left_right(Const(1)) == Identity(1)
64+
flip_left_right(Identity(:whatever)) == Const(:whatever)
65+
66+
exchanges left and right, i.e. what was Const, becomes an Identity and the other way around.
67+
"""
3968
flip_left_right(x::Const) = Identity(x.value)
4069
flip_left_right(x::Identity) = Const(x.value)
4170

71+
"""
72+
iseither(::Const) = true
73+
iseither(::Identity) = true
74+
iseither(other) = false
75+
76+
check whether something is an [`Either`](@ref)
77+
"""
4278
iseither(::Const) = true
4379
iseither(::Identity) = true
4480
iseither(other) = false
4581

82+
"""
83+
isleft(::Const) = true
84+
isleft(::Identity) = false
85+
86+
Identical to [`isconst`](@ref), but might be easier to read when working with `Either`.
87+
"""
4688
isleft(::Const) = true
4789
isleft(::Identity) = false
4890

91+
92+
"""
93+
isright(::Const) = false
94+
isright(::Identity) = true
95+
96+
Identical to [`isidentity`](@ref), but might be easier to read when working with `Either`.
97+
"""
4998
isright(::Const) = false
5099
isright(::Identity) = true
51100

101+
"""
102+
getleft(Const(:something)) == :something
103+
getleft(Identity(23)) # throws MethodError
104+
105+
Extract a value from a "left" `Const` value. Will result in loud error when used on anything else.
106+
"""
52107
getleft(e::Const) = e.value
53-
# getleft(e::Identity) = nothing # throw an error if getleft is called on Identity
54108

55-
# getright(e::Const) = nothing # throw an error if getright is called on Const
109+
"""
110+
getright(Identity(23)) == 23
111+
getright(Const(:something)) # throws MethodError
112+
113+
Extract a value from a "right" `Identity` value. Will result in loud error when used on anything else.
114+
Identical to `Base.get` but explicit about the site (and not defined for other things)
115+
"""
56116
getright(e::Identity) = e.value
57117

118+
119+
"""
120+
getleftOption(Identity(23)) == Option()
121+
getleftOption(Const(:something)) == Option(:something)
122+
123+
Convert to option, assuming you want to have the left value be preserved.
124+
"""
58125
getleftOption(e::Const) = Option(e.value)
59126
getleftOption(e::Identity) = Option()
60127

128+
"""
129+
getrightOption(Identity(23)) == Option(23)
130+
getrightOption(Const(:something)) == Option()
131+
132+
Convert to option, assuming you want to have the right value be preserved.
133+
Identical to `getOption`, just explicit about the site.
134+
"""
61135
getrightOption(e::Const) = Option()
62136
getrightOption(e::Identity) = e
137+
138+
139+
140+
"""
141+
getOption(Identity(23)) == Option(23)
142+
getOption(Const(:something)) == Option()
143+
144+
Convert to option, assuming you want to have the right value be preserved, and the left value represented as
145+
`Option()`.
146+
"""
147+
getOption(e::Const) = Option()
148+
getOption(e::Identity) = e

src/Identity.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
Identity(:anything)
3+
4+
Identity is a simple wrapper, which works as a single-element container.
5+
6+
It can be used as the trivial Monad, and as such can be helpful in monadic
7+
abstractions. For those who don't know about Monads, just think of it like
8+
container-abstractions.
9+
"""
110
struct Identity{T}
211
value::T
312
end
@@ -8,6 +17,12 @@ function Base.show(io::IO, x::Identity)
817
print(io, "Identity($(repr(x.value)))")
918
end
1019

20+
"""
21+
isidentity(Identity(3)) -> true
22+
isidentity("anythingelse") -> false
23+
24+
returns true only if given an instance of [Identity](@ref)
25+
"""
1126
isidentity(::Identity) = true
1227
isidentity(other) = false
1328

0 commit comments

Comments
 (0)