{ "cells": [ { "cell_type": "markdown", "id": "8a6bd5de-f590-4222-9a16-845b5b749284", "metadata": {}, "source": [ "[quote=\"BambOoxX, post:12, topic:64568\"]\n", "you define the `SCI` package such that it patches the `COM` package is this right ?\n", "[/quote]\n", "\n", "Yes. It is a standard pattern.\n", "\n", "[quote=\"BambOoxX, post:12, topic:64568\"]\n", "If so : What would happen if `COM` is not loaded prior to `SCI` .\n", "[/quote]\n", "\n", "Assume that, unlike the MWE I have shown above, both COM and SCI are packaged and can be loaded with `using COM` and `using SCI`. Then the result of `using SCI; using COM` and the result of `using COM; using SCI` will be the same.\n", "\n", "In the following example, `COM` package has a function `prettify(x)` whose methods can be defined for user-defined types, and a function `prettyprint(x)` that uses `prettify(x)`. `SCI` package makes use of them.\n", "\n", "__src/COM.jl of COM package:__\n", "```julia\n", "\"\"\"\n", "`COM.prettyprint(x)` prints nicely the object `x` for which `COM.prettify(x)` method is defined.\n", "\"\"\"\n", "module COM\n", "prettify(x) = sprint(io -> show(io, \"text/plain\", x))\n", "prettyprint(io::IO, x) = print(io, prettify(x))\n", "prettyprint(x) = prettyprint(stdout, x)\n", "end\n", "```\n", "\n", "__Example of COM__\n", "```\n", "using COM\n", "COM.prettyprint([π, 2π])\n", "```\n", "__Output:__\n", "```\n", "2-element Vector{Float64}:\n", " 3.141592653589793\n", " 6.283185307179586\n", "```\n", "\n", "__src/SCI.jl of SCI package (with deps COM):__\n", "```julia\n", "\"\"\"A scirntific module (calculate exp(x))\"\"\"\n", "module SCI\n", "\n", "abstract type AbstractProblem end\n", "struct Problem{T} <: AbstractProblem x::T end\n", "\n", "abstract type AbstractAlgorithm end\n", "struct Builtin <: AbstractAlgorithm end\n", "Base.@kwdef struct Taylor <: AbstractAlgorithm n::Int = 10 end\n", "default_algorithm(prob::Problem) = Builtin()\n", "\n", "struct Solution{R, P<:AbstractProblem, A<:AbstractAlgorithm} result::R; prob::P; alg::A end\n", "solve(prob::AbstractProblem) = solve(prob, default_algorithm(prob))\n", "solve(prob::AbstractProblem, alg::Builtin) = Solution(exp(prob.x), prob, alg)\n", "solve(prob::AbstractProblem, alg::Taylor) = Solution(sum(prob.x^k/factorial(k) for k in 0:alg.n), prob, alg)\n", "\n", "using COM\n", "\n", "COM.prettify(sol::Solution{R, P, A}) where {R, P<:AbstractProblem, A<:Builtin} = \"\"\"\n", "Problem: x = $(sol.prob.x)\n", "Algorithm: builtin exp(x)\n", "Result: $(sol.result)\n", "\"\"\"\n", "\n", "COM.prettify(sol::Solution{R, P, A}) where {R, P<:AbstractProblem, A<:Taylor} = \"\"\"\n", "Problem: x = $(sol.prob.x)\n", "Algorithm: Taylor series of exp(x) upto degree $(sol.alg.n)\n", "Result: $(sol.result)\n", "\"\"\"\n", "\n", "end\n", "```\n", "\n", "__Code to run:__\n", "```julia\n", "# The order of the following two lines may be reversed.\n", "using SCI\n", "using COM\n", "\n", "prob = SCI.Problem(1)\n", "COM.prettyprint(SCI.solve(prob))\n", "println()\n", "COM.prettyprint(SCI.solve(prob, SCI.Taylor()))\n", "```\n", "\n", "__Output:__\n", "```\n", "Problem: x = 1\n", "Algorithm: builtin exp(x)\n", "Result: 2.718281828459045\n", "\n", "Problem: x = 1\n", "Algorithm: Taylor series of exp(x) upto degree 10\n", "Result: 2.7182818011463845\n", "```\n", "\n", "In this way, you can use the pretty printing functions provided by `COM` package in any other package without changing any code in `COM` package." ] }, { "cell_type": "code", "execution_count": 1, "id": "cc8b8c16-10e9-4e14-9fd6-f69bb5057936", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem: x = 1\n", "Algorithm: builtin exp(x)\n", "Result: 2.718281828459045\n", "\n", "Problem: x = 1\n", "Algorithm: Taylor series of exp(x) upto degree 10\n", "Result: 2.7182818011463845\n" ] } ], "source": [ "# The order of the following two lines may be reversed.\n", "using SCI\n", "using COM\n", "\n", "prob = SCI.Problem(1)\n", "COM.prettyprint(SCI.solve(prob))\n", "println()\n", "COM.prettyprint(SCI.solve(prob, SCI.Taylor()))" ] } ], "metadata": { "jupytext": { "formats": "ipynb,auto:hydrogen" }, "kernelspec": { "display_name": "Julia 1.6.1", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.1" } }, "nbformat": 4, "nbformat_minor": 5 }