LoginSignup
1
2

More than 1 year has passed since last update.

Juliaと関数型プログラミング

Last updated at Posted at 2023-03-09

はじめに

Juliaで関数型 (Clojure式) プログラミングをしたいと思い調べたパッケージを、ここにメモする。パッケージ名の後の数字は、Githubでのスター数と、最終更新月。

どれもまともにメンテされておらず、機能的にも、表面的な記法を弄るものが多いように思われた。劇的にコードが見やすくなったり、コード量が短くなったりするようなものはない。

しばらくは、素のJuliaと自作関数でやっていこうと思う。

Lazy.jl (446/2020-07)

名前の通り、遅延評価用のパッケージ。しばらくメンテされていない。

Underscore.jl (76/2021-05)

Clojureの%_で利用できるようにするパッケージ。

@_ map(_+1, xs) means map(x->x+1, xs)

Chain.jl (315/2022-06)

# Normal
df |>
  dropmissing |>
  x -> filter(:id => >(6), x) |>
  x -> groupby(x, :group) |>
  x -> combine(x, :age => sum)

# Chain.jl
@chain df begin
  dropmissing
  filter(:id => >(6), _)
  groupby(:group)
  combine(:age => sum)
end

IterTools.jl 125/2021-07

中身は調べていない。

Trancducers.jl (371/2022-11)

Clojureのシーケンス処理を模倣するパッケージPartition, Drop, MapCat等があるのだが、少しクセが強そう。

下記の例だと、Map(copy) |> collectが直感的でないように思えるし、(Vectorではなく) Matrixを扱えないっぽい。

julia> 1:8 |> Partition(3) |> Map(copy) |> collect
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

LispSyntax.jl (212/2020-02)

JuliaのREPLでLispができるっぽい。

> (* 2 (reduce + (: 1 6)))
42
> (defn fib [a] 
      (if (< a 2) 
        a 
        (+ (fib (- a 1)) (fib (- a 2)))))
fib (generic function with 1 method)
> (fib 10)
55

Pipe.jl (140/2020-12)

多分、Julia純正のパイプとほぼ同じ。

参考 (英語)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2