3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Elixirの対話モードで文字列の動作を確認してみた

Posted at

概要

Elixirの対話モードで文字列の動作を確認してみました。以下のページを参考にしました。

対話モードで実行

以下のコマンドを実行しました。

$ iex
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> string = <<104,101,108,108,111>>
"hello"
iex(2)> string <> <<0>>
<<104, 101, 108, 108, 111, 0>>
iex(3)> 'hełło'
[104, 101, 322, 322, 111]
iex(4)> "hełło" <> <<0>>
<<104, 101, 197, 130, 197, 130, 111, 0>>
iex(5)> ?Z
90
iex(6)> string = "\u0061\u0301"
"á"
iex(7)> String.codepoints string
["a", "́"]
iex(8)> String.graphemes string
["á"]
iex(9)> String.length "Hello"
5
iex(10)> String.replace("Hello", "e", "a")
"Hallo"
iex(11)> String.duplicate("Oh my ", 3)
"Oh my Oh my Oh my "
iex(12)> String.split("Hello World", " ")
["Hello", "World"]
iex(13)> defmodule Anagram do
...(13)> def anagrams?(a, b) when is_binary(a) and is_binary(b) do
...(13)> sort_string(a) == sort_string(b)
...(13)> end
...(13)> def sort_string(string) do
...(13)> string
...(13)> |> String.downcase()
...(13)> |> String.graphemes()
...(13)> |> Enum.sort()
...(13)> end
...(13)> end
{:module, Anagram,
 <<70, 79, 82, 49, 0, 0, 6, 172, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 208,
   0, 0, 0, 21, 14, 69, 108, 105, 120, 105, 114, 46, 65, 110, 97, 103, 114, 97,
   109, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:sort_string, 1}}
iex(14)> Anagram.anagrams?("Hello", "ohell")
true
iex(15)> Anagram.anagrams?("María", "íMara")
true
iex(16)> Anagram.anagrams?(3, 5)
** (FunctionClauseError) no function clause matching in Anagram.anagrams?/2    
    
    The following arguments were given to Anagram.anagrams?/2:
    
        # 1
        3
    
        # 2
        5
    
    iex:14: Anagram.anagrams?/2
iex(16)> 

まとめ

何かの役に立てばと。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?