Swiftのexercism.iosを解いている時に、正規表現を使いたい箇所に出くわしました。 Swift、ObjCのそれを使わないといけないのですね。私が扱う言語の中では一番手間隙がかかるやつでした。 メモメモ。以下は、入力文字列に対して 0-9 の数字を文字列として抜き出すメソッドです。 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters private func format(startingNumber: String) -> String { let pattern…More
Category Archives: development
[Elixir]refactor to use pattern match in function instead of case one
exercism.ioで実装した問題を、 case 使って書いてたのですが、冗長なのでメソッドのパターンマッチを使って簡略化しました。 思いの外削減できたので、メモ。 特に、 can_attack? のところのパターンマッチは他の言語で書いた後に戻って書こうとするとパッと思い浮かばないときもある。。。 case でちんたら… が初めのやつ。pattern mach in functionが修正版。 case でちんたら… defmodule Queens do @type t :: %Queens{ black: {integer, integer}, white: {integer, integer} } defstruct black: {7, 3}, white: {0, 3} @doc “”” Creates a new set of Queens “”” @spec new(nil | list) :: Queens.t() def new(positions \\ nil)…More
[Elixir][Erlang]listと、filterの使い心地
ErlangもElixirも、 filter を使う操作で簡単に特定の条件に合致した要素だけのリストを作ることができます。ただ、 filter を使わないで簡単に同様な処理を表現する方法が、Erlangだけでしか思いつかなかったです。。 ただ、ここでElixirにおける for を良い感じで使いたい問題があったのでそちらも。 generate List with for syntax と書いている箇所です。 2つ以上のリストの組み合わせを入力とした処理を書きたい時、書きやすい形で処理を表現できますね。 How to generate list with filter or other ways. Erlang > [X || X <- lists:seq(1, 5), X rem 3 == 0]. [3] > lists:filter(fun(X) -> X rem 3 == 0 end, lists:seq(1, 5)). [3] Elixir > (for x <-…More
[Erlang][Elixir]binaryの扱い
Erlangにおけるbinaryのコピー、参照の扱いに関してメモ。 64 byteまではheap binariesと言われて、メッセージを渡す時にコピーを渡してprocessのheapに保存するらしいです。 一方、それ以上は参照渡しになるらしいです。 なるほど。 参考 http://erlang.org/doc/efficiency_guide/binaryhandling.html http://stackoverflow.com/questions/3406425/does-erlang-always-copy-messages-between-processes-on-the-same-node Vさんの以下が参考になりました。 Erlang のバイナリ特別扱いについては https://t.co/JCfyVIZQhx を見れば書いてあります。日本語は @shibu_jp が翻訳したこちら。https://t.co/c9ssNKHchV — V (@voluntas) March 20, 2016 ネットワークサーバ書く場合は、 binary はコピーされないっての結構大事な話なので覚えておくと良いですよ。設計に影響する。 — V (@voluntas) March 20, 2016 なので、 atom と binary は凄く積極的に使いましょう。ただし atom を動的生成はしてはだめですよ。 — V (@voluntas) March 20, 2016 こっちも読んでいこう。 http://erlang.org/doc/efficiency_guide/introduction.htmlMore
[Elixir]正規表現のグループ化、後方一致メモ
Erlang/Elixirは、Perlの正規表現に沿っています。なので、メタ文字などの細かな箇所はそれにそう形になります。 参考: http://erlang.org/doc/man/re.html その中で、後方一致の時の挙動なんかがパッとわかってなかったので、メモ。 グループ化した ([A-Z]) に対して、同じ文字を0回以上繰り返すものを同一のグループとしてみなします。その結果はリストの [“全ての文字列”, “繰り返される値”] という形になります。 日本語でも、こちらを参考に理解を進めたので、メモ。 http://doc.mas3.net/regexp/#backreferenceMore
[Elixir][Erlang]run functions like yield
These days, I challenge exercism.io to get used some languages. Especially, I use Erlang and Swift. Then, I found some difference between Erlang and Elixir in aspect of syntax. So, I describe them as articles to remember them. This time, I attached gist which is run function defined in variables. Elixir run them with func.(),…More
[Elixir][Erlang]difference of binary
double quote and single quote have different meanings between Elixir and Erlang. In addition, “$” and “?” also have. They make me confuse a bit … 😦 Elixir > ?a 97 > [?a] == ‘a’ true > [?a] == “a” false > is_bitstring “a” true > is_bitstring ‘a’ false Erlang > $a. 97 > [$a]…More
[Elixir]愚直にズンドコキヨシ
こちらの「ズンドコキヨシ」を簡単に実装してみた。Streamとか、そういうのは使ってない。実際に処理するメソッド数は3つ。 Enum.take/2 のようなものは使っていない。 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters defmodule Znd do @z "ズン" @d "ドコ" @k "キ・ヨ・シ" def kiyoshi,…More
[Elixir]@behaviour and @callback
簡単なWebフレームワークを作ってみようと、Plugやtrotを読んでいると、いたるところで @behaviour と描かれているものがありました。 GenServer なんかを use した時にOTPとしての振る舞いを、対象モジュールに与えることができました。そこで使われている Behaviour は把握していたのですが、この @ のアノテーションはパッと思い出すことができなかったので、メモがてら。 Elixir 1.2.xの時点が対象です。結論から書いておくと、この @behaviour は Behaviour のことで、2.0では削除される Behaviour の代わりに推奨されているものです。 ドキュメント https://github.com/elixir-lang/elixir/blob/v1.2/lib/elixir/lib/module.ex https://github.com/elixir-lang/elixir/blob/v1.2.3/lib/elixir/lib/behaviour.ex#L1 Behaviorは、Elixir 1.2.3 からdepricatedになった。 自身で定義したBehaviourに関して、以下のように @callback で定義したものを、 @behaviour で読み込んで使うという用途で使われます。ここで、読み込んだ先で @callback しているものを def で定義していない場合、 warning が表示されます。 例えば、以下の MyModule や MyGenServer の直前のコメント箇所のような。 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled…More
[Elixir]defprotocol and defimpl, inheritance
trySwiftConfにて、protocolの話がかなり熱かったのでElixirのprotocolの動作を思い出し込みで確認してみました。 テストコードありなので、以下をそのままファイルに書き出して実行すると現状の動作を確認できます。 ポイントは、 継承 defprotocolとdefimpの組み合わせ です。 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters defmodule Value do defmacro __using__(_opts) do quote do…More