julia on raspbian buster – git source repository update

I continue to work with the tutorial examples on Julia By Example (https://juliabyexample.helpmanual.io/). Rather than publish them here and talk about them, I’ve pushed everything I’ve updated to my modest spot on GitHub (https://github.com/wbeebe/julia). As I update more and bring it into compliance with Julia 1.2, I’ll continue to push it all there.

If you read the README, you’ll see I grew disillusioned with Julia September 2018. Coming back nearly a year later and a lot has changed, and working with Julia is now (for me) a lot more enjoyable. I also see the potential of Julia.

Advertisements

julia on raspian buster – string manipulations

In between today’s yard and house chores, I’ve continued to pull down example code from Julia By Example. This post’s code example has begun to diverge even more than the earlier examples I downloaded and reported about in the last post. There’s so much that’s changed here I don’t even know where to start.

I draw you attention to the highlighted lines below, 39-43. Lines 39-40 are commented original Julia command and output. There is no matchall() anymore; I tested with version 1.0.4 as well as 1.2.0 RC3. I tried to recreate the functionality with collect(eachmatch(…)). And as you’ll note the new output structure doesn’t even come close to the output from matchall(). I guess that’s something I can figure out as I dive deeper into Julia.

One other thing I’ve discovered, and that’s all the links in the code as comments are broken. None of them go to where they’re supposed to. The websites are still there but the specific pages that correspond to specific functions in the example do not. I’ve checked all of them so far and they’re all broken.

I don’t know why they leave that site up. It’s misleading unless you take it as a personal challenge to fix it all and bring it all up to date. I’m doing that right now because it’s helping me to find the real up-to-date sources, and it’s beginning to stick. My latest sources for correct Julia (so far) are Introducing Julia (https://en.wikibooks.org/wiki/Introducing_Julia) and Julia 1.1 Documentation (https://pkg.julialang.org/docs/julia/THl1k/1.1.1/index.html). I’m sure there are others, and I’ll eventually find them. but for now these seem to be sufficient.

#!/usr/bin/env julia
#
s1 = "The quick brown fox jumps over the lazy dog α,β,γ"

# find the first instance in the string of a given character.
i = findfirst(isequal('b'), s1)
println(i)
#> 11

# or a range if called with another string
r = findfirst("brown", s1)
println(r)
#> 11:15

# replace one substring with another.
r = replace(s1, "brown" => "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"

# findfirst can also take a regular expressions by preceding the string with 'r'.
r = findfirst(r"b[\w]*n", s1)
println(r)
#> 11:15

# again with a regular expression
r = replace(s1, r"b[\w]*n" => "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"

# there are also functions for regular expressions that return RegexMatch types
r = match(r"b[\w]*n", s1)
println(r)
#> RegexMatch("brown")

# RegexMatch types have a property match that holds the matched string
show(r.match); println()
#> "brown"

# r = matchall(r"[\w]{4,}", s1)
#> SubString{UTF8String}["quick","brown","jumps","over","lazy"]
r = collect(eachmatch(r"[\w]{4,}", s1))
println(r)
#> RegexMatch[RegexMatch("quick"), RegexMatch("brown"), RegexMatch("jumps"), RegexMatch("over"), RegexMatch("lazy")]
r = eachmatch(r"[\w]{4,}", s1)
for i in r print("\"$(i.match)\" ") end
println()
#> "quick" "brown" "jumps" "over" "lazy" 

r = "hello "^3
show(r); println() #> "hello hello hello "

# e.g., with one argument it strips the outer whitespace
r = strip("hello ")
show(r); println() #> "hello"
# or with a second argument of an array of chars it strips any of them;
r = strip("hello ", ['h', ' '])
show(r); println() #> "ello"
# (note the array is of chars and not strings)

r = split("hello, there,bob", ',')
show(r); println() #> ["hello"," there","bob"]
r = split("hello, there,bob", ", ")
show(r); println() #> ["hello","there,bob"]
r = split("hello, there,bob", [',', ' '], limit=0, keepempty=false)
show(r); println() #> ["hello","there","bob"]

r = join(collect(1:10), ", ")
println(r) #> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10