https://github.com/jochasinga/ava
Collection of Python-flavor mapping of commonly used functions.
https://github.com/jochasinga/ava
Last synced: 5 months ago
JSON representation
Collection of Python-flavor mapping of commonly used functions.
- Host: GitHub
- URL: https://github.com/jochasinga/ava
- Owner: jochasinga
- Created: 2019-09-05T00:11:59.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-05T08:51:38.000Z (over 6 years ago)
- Last Synced: 2024-10-18T15:18:58.245Z (over 1 year ago)
- Language: Julia
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ava
Ava is a collection of useful utilities in Julia Language that maps closely to the Python counterparts.
For a quick example, in Python, a [Counter](https://docs.python.org/3.5/library/collections.html#collections.Counter) object can be used to form a bag of words.
```python
>>> words = 'Two roads diverged in a yellow wood'.split()
>>> Counter(words).most_common()
```
With `Ava.Collections` you could write something similar:
```julia
> import Ava.Strings: Str
> import Ava.Collections: Counter
> words = Str('Two roads diverged in a yellow wood').split()
> Counter(words).most_common()
```
## Examples
Testing a string for alphanumeric:
```julia
using Ava
# Do this to avoid namespace clash with `Strings` module.
const Str = Ava.Strings
if !Str.isalpha("10.9")
println("It's number")
end
```
List the files in a directory.
```julia
> import Ava: Os
> parent = Os.listdir("..")
# listdir has multiple dispatch which defaults empty parameter to "."
> current = Os.listdir()
```
## Case of Module-level Functions
Because Julia is not Object-oriented language, instance methods aren't very idiomatic.
In Python, the conventions were mixed between module-level functions and instance methods. This is a source of confusion for many.
Ava provides convenient wrapper constructors to create an illusion of instance methods. For example:
```julia
> using Ava
> const Str = Ava.Strings.Str
> pi = Str("3.1416")
> pi.isalpha()
# false
> Str("Hello, world!").split()
#=
2-element Array{String,1}:
"Hello,"
"world!"
=#
```
Find the ten most common words in Hamlet:
```julia
> import Ava.Collections: Counter
> open("hamlet.txt") do f
> words = matchall(r"\w+", read(f, String))
> Counter(words).most_common(10)
> end
#=
10-element Array{Pair{Any,Int64},1}:
"the" => 1143
"and" => 966
"to" => 762
...
=#
```
However, module-level functions should be considered better practice.
```julia
> import DataStructures: counter
> import Ava.Collections: most_common
> counter(words) |> most_common
```