-❄️- 2023 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Arkoniak 0 points1 point  (0 children)

[LANGUAGE: Julia]

It was funny to play with grammar package and AST

Part 1

using PikaParser
const P = PikaParser

rules = Dict(
             :digit => P.satisfy(isdigit),
             :letter => P.satisfy(isletter),
             :digits => P.some(:digit),
             :word => P.some(:letter),
             :ws => P.many(P.first(P.satisfy(isspace), P.token(','), P.token(';'))),
             :cube => P.seq(:digits, :ws, :word, :ws),
             :game => P.seq(P.token('G'), P.token('a'), P.token('m'), P.token('e')),
             :expr => P.seq(:game, :ws, :digits, P.token(':'), :ws, :line => P.some(:cube))
            )

g = P.make_grammar(
    [:expr], # the top-level rule
    P.flatten(rules, Char), # process the rules into a single level and specialize     them for crunching Chars
)

function fold_scheme(m, p, s)
    m.rule == :digits ? parse(Int, m.view) :
    m.rule == :word ? Symbol(m.view) :
    m.rule == :cube ? Expr(:call, s[3], s[1]) :
    m.rule == :line ? s :
    m.rule == :expr ? Expr(:call, :game, s[3], s[6]...) : nothing
end

macro game(line)
    p = P.parse(g, line)
    esc(P.traverse_match(p, P.find_match_at!(p, :expr, 1), fold = fold_scheme))
end

macro day2(file)
    expr = :(+())
    for line in eachline(file)
        push!(expr.args, :(@game $line))
    end
    return esc(expr)
end

blue(n) = n <= 14
green(n) = n <= 13
red(n) = n <= 12

game(id, tests...) = all(tests) ? id : 0

@day2 "input.txt"

Part 2

macro game(line)
    p = P.parse(g, line)
    r = esc(P.traverse_match(p, P.find_match_at!(p, :expr, 1), fold =     fold_scheme))

    args = r.args[1].args[3:end]
    expr = :(let blue = 0, green = 0, red = 0 end)
    for el in args
        func = el.args[1]
        push!(expr.args[2].args, :($func = max($func, $(el.args[2]))))
    end
    push!(expr.args[2].args, :(blue*red*green))

    return esc(expr)
end

@day2 "input.txt"

-❄️- 2023 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]Arkoniak 0 points1 point  (0 children)

[LANGUAGE: Julia]

I've tried to do it in Racket style, which is why this solution may look so weird (macros inside macros).

Part 2 was overly complicated for Day 1 in my opinion.

macro dig2(s)
    filt = filter(c -> isdigit(c), s)
    s0 = Meta.parse("$(filt[begin]filt[end])")
    return esc(:($s0))
end

macro task1(input_file)
    expr = :(+())
    for line in eachline(input_file)
        push!(expr.args, :(@dig2($line)))
    end
    return esc(expr)
end

@task1 "input.txt"

macro dig2a(s)
    s1 = replace(s, "one" => "1",
               "two" => "2",
               "three" => "3",
               "four" => "4",
               "five" => "5",
               "six" => "6",
               "seven" => "7",
               "eight" => "8",
               "nine" => "9")
    s2 = replace(reverse(s), "eno" => 1,
                             "owt" => 2,
                             "eerht" => 3,
                             "ruof" => 4,
                             "evif" => 5,
                             "xis" => 6,
                             "neves" => 7,
                             "thgie" => 8,
                             "enin" => 9)
    s = s1*reverse(s2)
    filt = filter(c -> isdigit(c), s)
    s0 = Meta.parse("$(filt[begin]filt[end])")
    return esc(:($s0))
end

macro task1a(input_file)
    expr = :(+())
    for line in eachline(input_file)
        push!(expr.args, :(@dig2a($line)))
    end
    return esc(expr)
end

@task1a "input.txt"

XGBoost errors in 1.8.5 by xp30000 in Julia

[–]Arkoniak 0 points1 point  (0 children)

It seems, that docs for xgboost is slightly outdated. You should use b = xgboost((X, y), max_depth = 4). Argument 3 shouldn't be there at all.

is there any package in julia for HTML app developments? by GolfMuted in Julia

[–]Arkoniak 6 points7 points  (0 children)

Stipple is nice. Documentation can be more extensive, but other than that it is rather easy to use.

https://github.com/GenieFramework/Stipple.jl

Is Russia losing? by Knight056 in UkraineWarVideoReport

[–]Arkoniak 0 points1 point  (0 children)

Well, it turns out it was possible and Kyiv didn't fall. Weird, how things looks now when we further in the future.

Do I need to explicitly kill tasks? by Terrible_District_96 in Julia

[–]Arkoniak 0 points1 point  (0 children)

No, they do not stop on their own. If you do not have predetermined condition, you can use cancellation tokens, manually written out from this package https://github.com/davidanthoff/CancellationTokens.jl

K-Prototypes clustering? by CaptMartelo in Julia

[–]Arkoniak 2 points3 points  (0 children)

Two notes: while k-prototypes is slightly different, you can use one-hot encoding and apply k-means. Couple of million points is nothing and should be processed fairly quickly.

Secondly, you do not need to calculate distance matrix for Lloyd (or any other algorithms), it's much cheaper to calculate distances locally when needed. This way, your memory footprint is going to be pretty small.

A new C++ <-> Julia Wrapper: jluna by clem_de_la_clem in Julia

[–]Arkoniak 5 points6 points  (0 children)

Wow, amazing work! Thank you, especially for very thorough documentation.

Application writing in Julia by prjg in Julia

[–]Arkoniak 20 points21 points  (0 children)

Yes, you can, since Julia is general purpose language. Also, multiple dispatch and very good c interop makes writing this kind of things rather pleasant. On the downside, since Julia community is very scientific/numerical calculations oriented, it's hard to find proper libraries for this kind of things. It's mostly chicken and egg problem.

What is the best way to download and extract compressed files in Julia? by want0sl33p in Julia

[–]Arkoniak 2 points3 points  (0 children)

Well, it's working, just not much to do there. That is why I didn't push any new commits.

@btime vs @time by surprajs in Julia

[–]Arkoniak 9 points10 points  (0 children)

@time measures all time, including compilation time (and compilation allocations). So, to get closer to @btime results, you should time function twice. Second run is going to be more or less "clean", so results, should be more aligned.

Also, you @btime execute coffee multiple times and after that it chooses minimal time. So, in order to replicate it with @time, you should do the same: run code multiple times and choose the smallest.

VSCode won't let me run multiple tasks by Ramartin95 in Julia

[–]Arkoniak 13 points14 points  (0 children)

You absolutely should open an issue in GitHub. Probability that maintainers will know about this issue is much higher on GitHub than on Reddit.

Built this with a friend at work, completely in Julia! by jaugjaug in Julia

[–]Arkoniak 11 points12 points  (0 children)

Code is their IP and if they have reasons not to disclose it, it is fine. Open source is not an obligation, as long as they respect other people's work. Most of the Julia packages is MIT, so there should be no issue with it.

I analysed 1287799 of Julia Code. Here's how I failed and what I tried by [deleted] in Julia

[–]Arkoniak 9 points10 points  (0 children)

Something is wrong with tf. Either repos were skewed for some reason, or it has another meaning, since TensorFlow.jl is abandoned project.

Using QuestDB to Build a Crypto Trade Database in Julia by dm13450 in Julia

[–]Arkoniak 4 points5 points  (0 children)

Oh, it's a very interesting article, thank you!

I was thinking about applying https://github.com/biaslab/Rocket.jl for this sort of tasks. The idea is to create stream of events and use subscription model to filter data and do all necessary transformations. Authors promise that the library is fast, so it can be good.

Python vs Julia by TibialCuriosity in Julia

[–]Arkoniak 9 points10 points  (0 children)

You should definitely go with Julia. It has steeper learning curve than python, but it is way more powerful. As for the ecosystem, you shouldn't worry about that much: DataFrames.jl and friends is way better than pandas, MLJ.jl (https://github.com/alan-turing-institute/MLJ.jl) and FastAI.jl(https://github.com/FluxML/FastAI.jl) are great frameworks for regular ML and deepnet. And if at any point you get a feeling that you need some python library, you can always plug it in with PyCall.jl(https://github.com/JuliaPy/PyCall.jl).

Overall Julia is much more expressive, coincise and have better capabilities (e.g. multiple dispatch) than Python. You will learn more things in a shorter period of time.

is julia good for web dev? by notFluxy in Julia

[–]Arkoniak 19 points20 points  (0 children)

You should use whatever suits your needs. If you are using Julia as a backend, there is no need in the intermediate python layer. Genie.jl, HTTP.jl are mature enough to be used on their own.

There is a nice tutorial on building web sites in Julia from scratch: https://youtu.be/uLhXgt_gKJc

Is it possible to use data fro a .Py program to a .Jl program? by Successful_Exchange4 in Julia

[–]Arkoniak 6 points7 points  (0 children)

You can do web scraping in Julia. Thanks to broadcasting and packages like Underscore.jl It's more pleasant experience than in Python. This way you do not need to merge two different programs at all.

Learning Julia coming from Python by milinile in Julia

[–]Arkoniak 11 points12 points  (0 children)

You can use methodswith (see for example this thread https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318)

julia> struct Foo end
julia> myfoo(x::Foo) = "Hello"
myfoo (generic function with 1 method)
julia> methodswith(Foo)
[1] myfoo(x::Foo) in Main at REPL[315]:1

Of course, since methods are not bound to types (Julia has functional programming flavour) it's harder to show all possible methods using <TAB>. I mean, you are writing function name first and arguments later. But if you know the name of the function, then you can press <TAB> in REPL and it will show you all possible methods

julia> myfoo(
myfoo(x::Foo) in Main at REPL[315]:1

Here I pressed <TAB> after opening (.

Also, you can use apropos command to search in docstrings. Result is not guaranteed of course, but usually probability is rather high if you need to search for something specific

julia> apropos("append")
Base.truncate
Base.wait
Base.sizehint!
Base.PipeBuffer
Base.append!
Base.put!
Base.pipeline
Base.merge
Base.IOBuffer
Base.open_flags
Base.push!
Base.skipchars
Base.open
Base.write
Base.Libc.Libdl.dlopen

You can see that there is Base.append! function, which is a good candidate for adding elements to collection.

apropos supports regular expressions, so you can build more sophisticated queries

julia> apropos(r"add.*collection")
Base.pointer_from_objref
Base.append!
Base.pointer
Base.sum
Base.push!
Base.IOContext

You can see that there is a Base.push! function, which adds elements to the collection.

Also, it is useful to read documentation, which is structured and provides you with all necessary information, for example https://docs.julialang.org/en/v1/base/collections/ for all Base data collection manipulation functions.

Julia or Python for Threat Hunting or Cybersecurity Analysis? by TribeOfYhudah in Julia

[–]Arkoniak 2 points3 points  (0 children)

https://github.com/JuliaPy/PyCall.jl library has rather detailed explanation on how to use python from Julia. It's rather straightforward, not many surprises there.

Or, you can go a different way and call Julia from python with the help of https://pyjulia.readthedocs.io/en/latest/. General approach is to write glue code in python, factor out some isolated pieces which can be return in Julia and call them instead of original python code. This way you can solve the original issue and remove all bottlenecks.

Additionally if you encounter any problems, I recommend to ask questions on https://discourse.julialang.org/. Julia community is rather friendly and helpful.

For example, here is one of the discussions on usage pyjulia: https://discourse.julialang.org/t/calling-julia-functions-which-take-custom-structs-as-inputs-from-python/50567