You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package defines julia implementations for the common types ``Option`` (aka ``Maybe``), ``Either`` and ``Try``, as well as one extra type `ContextManager` which mimics behaviour realized in Python with python contextmanagers.
This package defines julia implementations for the common types `Option` (aka `Maybe`), `Either` and `Try`, as well as one extra type `ContextManager` which mimics Python's `with`-ContextManager.
4
9
5
10
Use it like
6
11
```julia
7
12
using DataTypesBasic
8
-
DataTypesBasic.@overwrite_Base
9
13
```
10
-
The macro `@overwrite_Base` assigns ``Some`` to `DataTypesBasic.Some` (in order to have consistent naming [following Scala](https://www.scala-lang.org/api/current/scala/Option.html)).
11
14
12
15
## Installation
13
16
@@ -20,30 +23,18 @@ pkg"add DataTypesBasic"
20
23
21
24
## Option
22
25
23
-
``Option{T}`` Type is like ``Union{T, Nothing}``, plus that you can dispatch on it more easily.
24
-
25
-
It is literally defined like an abstract type with two instances ``None`` and ``Some``
26
-
```julia
27
-
abstract type Option{T} end
28
-
struct None{T} <:Option{T}end
29
-
struct Some{T} <:Option{T}
30
-
value::T
31
-
end
32
-
```
33
-
as you can see, ``None{T}`` captures the information ``"no value"`` of Type `T`, and ``Some{T}`` guarantees to have
34
-
a value of Type `T`.
26
+
`Option{T} = Union{Identity{T}, Nothing}`
35
27
36
28
Use it like
37
29
```julia
38
30
using DataTypesBasic
39
-
DataTypesBasic.@overwrite_Base
40
31
41
-
fo(a::Some{String}) = a.value *"!"
42
-
fo(a::None{String}) ="fallback behaviour"
32
+
fo(a::Identity{String}) = a.value *"!"
33
+
fo(a::Nothing) ="fallback behaviour"
43
34
44
35
fo(Option("hi")) # "hi!"
45
-
fo(Option{String}()) # "fallback behaviour"
46
-
# f(Option{Int}()) # not implemented
36
+
fo(Option(nothing)) # "fallback behaviour"
37
+
fo(Option()) #"fallback behaviour"
47
38
```
48
39
49
40
The real power of `Option` comes from generic functionalities which you can define on it. `DataTypesBasic` already defines the following:
@@ -53,10 +44,9 @@ consult the respective function definition for details.
53
44
Here an example for such a higher level perspective
54
45
```julia
55
46
using DataTypesBasic
56
-
DataTypesBasic.@overwrite_Base
57
47
58
-
flatten(a::Some) = a.value
59
-
flatten(a::None) = a
48
+
flatten(a::Identity) = a.value
49
+
flatten(a::Nothing) = a
60
50
61
51
functionmap2(f, a::Option{S}, b::Option{T}) where {S, T} # this comes
62
52
nested_option =map(a) do a′
@@ -69,14 +59,14 @@ end
69
59
70
60
map2(Option("hi"), Option("there")) do a, b
71
61
"$a$b"
72
-
end#Some{String}("hi there")
62
+
end#Identity("hi there")
73
63
74
64
map2(Option(1), Option()) do a, b
75
65
a + b
76
-
end#None{Any}()
66
+
end#nothing
77
67
```
78
68
79
-
The package ``TypeClasses.jl`` (soon to come) implements a couple of such higher level concepts of immense use
69
+
The package `TypeClasses.jl` (soon to come) implements a couple of such higher level concepts of immense use
80
70
(like Functors, Applicatives and Monads).
81
71
82
72
@@ -94,6 +84,6 @@ Please see the tests `test/ContextManager.jl`.
94
84
95
85
## [TODO] Other
96
86
97
-
For abstraction purposes, there is also ``Const`` and ``Identity`` defined.
87
+
For abstraction purposes, there is also `Const` and `Identity` defined.
98
88
99
89
Please see the tests `test/Const.jl` and `test/Identity.jl` respectively.
0 commit comments