scala

Scala, Ruby, Go の REPL 環境を Docker コンテナですぐに試せる Rod (REPLs on Docker) を作った

Scala の学習用に Ammonite 環境 を AWS EC2 上に構築していて、Scala Exercises の例を試せるように紹介されている cats や shapeless のライブラリを追加していた。ただ、個人的に Ruby を使う機会が圧倒的に多いので pry は必須だし、Go もやってみたいと…

bash: /dev/tty: No such device or address を script コマンドで解決する

GitHub - kyagi/rod: REPLs On Docker を作っている中で Ammonite の起動が早くなるようにあらかじめライブラリをキャッシュさせておくため .ammonite/predef.sc にライブラリを追加したあとに echo exit | amm -s を実行するようにしたところ、docker build…

Disjunction と Either の違い

Safari Books Online で検索したところ、以下の記述が見つかった。 Disjunction is conceptually similar to Either, which can be used to represent one of two possible types. Disjunction is different from Either because its operations are right-b…

better-files を使用して複数ファイルの一括処理を書き直す

会社の Scala エキスパートから、better-fs, fs2 というライブラリを教えていただいたので書き直してみる。とても短くなりました。(^q^)。 GitHub - pathikrit/better-files: Simple, safe and intuitive Scala I/O ayakumo.hatenablog.com @ import better.…

読み解き PiS3: Higher Order Functions を使って複数ファイルの一括処理を行う

9.4 WRITING NEW CONTROL STRUCTURES を読んでいて、複数ファイルに対して「ファイルを開いて特定の処理を行う」ことができるはずと思って実装した。Scala でメソッドをメソッドの引数に取るのは HOF(Higher Order Functions) というらしい。 C でいう関数ポ…

Ammonite ではじめるすくすく Scala 生活

だいたい Ammonite に書いてある。Magic Imports はとてもありがたい。 インストール $ sudo curl -L -o /usr/local/bin/amm https://git.io/vdNv2 && sudo chmod +x /usr/local/bin/amm && amm REPL として使う $ amm (... snip the greeting message ...) …

Ammonite を使用してお手軽に Scala のスクリプトを書く

sbt は優秀なツールだけれども、さくっと Scala のスクリプトを書くには Ammonite のほうがお気に入り。シェルからは -c オプションで気軽に Scala をスクリプトとして実行可能。 $ amm -c 'import scala.io.Source; val file = Source.fromFile("/home/kyag…

シングルトンオブジェクトを IntelliJ の Scala Worksheet で確認する

Singleton objects are sort of a shorthand for defining a single-use class, which can’t directly be instantiated, and a val member at the point of definition of the object, with the same name. Indeed, like vals, singleton objects can be def…

読み解き PiS3: ケースシーケンスの実体は関数リテラルである

A sequence of cases (i.e., alternatives) in curly braces can be used anywhere a function literal can be used.Essentially, a case sequence is a function literal, only more general. Instead of having a single entry point and list of paramete…

 読み解き PiS3: ケースクラスのパターンマッチング(Constructor パターン)

case class は abstract class の派生でなくてもいい。コップ本だと Expr の派生クラスとして UnOp や BinOp クラスを定義しているが別に親がいなくてもいい。 パターンマッチのブロックの中の e はパターンに束縛される一時変数。「この e はどこから出てき…

読み解き PiS3: def の後のイコールのありなし

def の後の body の前に = を置かない場合と置く場合の違いが気になっていた。 = を置かない場合はメソッド/関数の戻り値は Unit になる。 = を置く場合はメソッド/関数の戻り値を任意に指定する。 The equals sign that precedes the body of a function hi…