|
7 | 7 |
|
8 | 8 | 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. |
9 | 9 |
|
10 | | -Use it like |
11 | | -```julia |
12 | | -using DataTypesBasic |
13 | | -``` |
14 | | - |
15 | | -## Installation |
16 | | - |
17 | | -```julia |
18 | | -using Pkg |
19 | | -pkg"registry add https://github.com/JuliaRegistries/General" # central julia repository |
20 | | -pkg"registry add https://github.com/schlichtanders/SchlichtandersJuliaRegistry.jl" # custom repository |
21 | | -pkg"add DataTypesBasic" |
22 | | -``` |
23 | | - |
24 | | -## Option |
25 | | - |
26 | | -`Option{T} = Union{Identity{T}, Nothing}` |
27 | | - |
28 | | -Use it like |
29 | | -```julia |
30 | | -using DataTypesBasic |
31 | | - |
32 | | -fo(a::Identity{String}) = a.value * "!" |
33 | | -fo(a::Nothing) = "fallback behaviour" |
34 | | - |
35 | | -fo(Option("hi")) # "hi!" |
36 | | -fo(Option(nothing)) # "fallback behaviour" |
37 | | -fo(Option()) # "fallback behaviour" |
38 | | -``` |
39 | | - |
40 | | -The real power of `Option` comes from generic functionalities which you can define on it. `DataTypesBasic` already defines the following: |
41 | | -`Base.iterate`, `Base.foreach`, `Base.map`, `Base.get`, `DataTypesBasic.iftrue`, `DataTypesBasic.iffalse`, `Base.isnothing`, `DataTypesBasic.issomething`. Please |
42 | | -consult the respective function definition for details. |
43 | | - |
44 | | -Here an example for such a higher level perspective |
45 | | -```julia |
46 | | -using DataTypesBasic |
47 | | - |
48 | | -flatten(a::Identity) = a.value |
49 | | -flatten(a::Nothing) = a |
50 | | - |
51 | | -function map2(f, a::Option{S}, b::Option{T}) where {S, T} # this comes |
52 | | - nested_option = map(a) do a′ |
53 | | - map(b) do b′ |
54 | | - f(a′, b′) |
55 | | - end |
56 | | - end |
57 | | - flatten(nested_option) |
58 | | -end |
59 | | - |
60 | | -map2(Option("hi"), Option("there")) do a, b |
61 | | - "$a $b" |
62 | | -end # Identity("hi there") |
63 | | - |
64 | | -map2(Option(1), Option()) do a, b |
65 | | - a + b |
66 | | -end # nothing |
67 | | -``` |
68 | | - |
69 | | -The package `TypeClasses.jl` (soon to come) implements a couple of such higher level concepts of immense use |
70 | | -(like Functors, Applicatives and Monads). |
71 | | - |
72 | | - |
73 | | -## [TODO] Try |
74 | | - |
75 | | -Please see the tests `test/Try.jl`. |
76 | | - |
77 | | -## [TODO] Either |
78 | | - |
79 | | -Please see the tests `test/Either.jl`. |
80 | | - |
81 | | -## [TODO] ContextManager |
82 | | - |
83 | | -Please see the tests `test/ContextManager.jl`. |
84 | | - |
85 | | -## [TODO] Other |
86 | | - |
87 | | -For abstraction purposes, there is also `Const` and `Identity` defined. |
88 | | - |
89 | | -Please see the tests `test/Const.jl` and `test/Identity.jl` respectively. |
| 10 | +For more details check out the [documentation](https://schlichtanders.github.io/DataTypesBasic.jl/dev/). |
0 commit comments