Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ Julia packaging exercise: Function with external dependency #23

Open
clstaudt opened this issue Oct 30, 2024 · 0 comments
Open

+ Julia packaging exercise: Function with external dependency #23

clstaudt opened this issue Oct 30, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@clstaudt
Copy link
Collaborator

clstaudt commented Oct 30, 2024

Adding a Function with an External Package Dependency

Let's say you want to add a function that generates a sequence of prime numbers using the Primes package.

Create a new file (e.g., prime_sequence.jl) in your package's src directory and add the following code:

using Primes

function prime_sequence(n)
    primes = []
    for i in 1:n
        push!(primes, nextprime(i))
    end
    return primes
end

This function uses the nextprime function from the Primes package to generate a sequence of prime numbers.

Update Your Package's Project.toml File

Run the following command in your package's directory to update the Project.toml file:

julia --project=. -e "Pkg.resolve()"

This will update the Project.toml file to include the Primes package as a dependency.

Example Use Case

You can now use the prime_sequence function in your package:

julia> using MyPackage

julia> prime_sequence(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

To use the latest version of a dependency, you can specify the dependency without a version range. For example:

[deps]
Primes = ""

This tells Julia to use the latest version of the Primes package.

Alternatively, you can use the * wildcard to specify the latest version:

[deps]
Primes = "*"

Both of these approaches will use the latest version of the Primes package.

@clstaudt clstaudt added the enhancement New feature or request label Oct 30, 2024
@clstaudt clstaudt self-assigned this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant