8
4

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 5 years have passed since last update.

F#を横目にElixirで「こんにちは世界」

Last updated at Posted at 2018-12-15

(この記事は「Elixir Advent Calendar 2018」の15日目です)


Summary

F#Elixirのちょっとしたコード比較をしてみました

Motivation

BashScriptで書いたスクリプトの速度を改善したい
Elixirで書いたら早くなるかも?

問題のBashScriptはこちら
古い写真のアルバムをスマホで見るようにしたお話 05-05 BashScriptで連番振りスクリプト書いてみた

Goal

Elixirの「こんにちは世界」レベルに最速でなる!

Way

ちょっとだけ知ってるF#と比較することで
手早くElixirの「こんにちは世界」レベルに行く

実際に書いたコード

BashScript を Elixir で書き直してみたっ(2倍速〜)

F#とElixirの簡単な比較コード

F#

// File extension 
// foo.fsx

// binding
let foo = 123

// function in pipe
"foo" |> fun s -> stdout.WriteLine( s )

Elixir

# file extension
# foo.exs

# Binding
foo = 123

# function in pipe
"foo" |> fn s -> ... end
"foo  |> &( &1  )

print

// fsharp
"hello" |> stdout.WriteLine
[1..5]  |> printfn "%A"

// hello
// [1; 2; 3; 4; 5]
# elixir
"hello" |> IO.puts
1..5    |> IO.inspect

# hello
# 1..5

iter

// fsharp
["foo";"bar";"baz"]
|> List.iter stdout.WriteLine

// foo
// bar
// baz
# elixir
["foo","bar","baz"]
|> Enum.with_index()
|> Enum.each(fn {e,_} -> IO.puts e end)

# foo
# bar
# baz

tuple

// fsharp
("foo","bar","baz")
|> fun (_,x,_) -> stdout.WriteLine x

// bar
# elixir
{"foo","bar","baz"}
|> elem(1)
|> IO.puts

# bar

type cast( int -> strings )

// fsharp
(1).ToString().GetType()
|> stdout.WriteLine

// System.String
# elixir ( typeof()がなさそう? )
to_string(1)
|> is_bitstring
|> IO.puts

# false

parallel

// fsharp
let hello1 = async { stdout.WriteLine("hello") }
let world1 = async { stdout.WriteLine("world") }

[hello1;world1]
|> Async.Parallel
|> Async.RunSynchronously

// hello world or world hello
# elixir
["hello","world"]
|> Task.async_stream( &( IO.puts &1 ) )
|> Enum.to_list()

# hello world or world hello

module and function ( elixir has no class )

// fsharp
module foo =

    let bar baz =
        stdout.WriteLine( baz )

foo.bar "aaa"

// aaa
# elixir
defmodule Foo do

  def bar( baz ) do
      IO.puts( baz )
  end

end

Foo.bar "aaa"

# aaa

いくよ?

ほむほむ
こんなところでしょうか?
では・・・

foo.png

できたーーー!
ありがとうございますっ!

8
4
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
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?