LoginSignup
11
4

More than 5 years have passed since last update.

ElmとJavaScriptのシンタックス比較表

Last updated at Posted at 2017-02-26

Elm公式ドキュメントにある比較表を紹介します。

ElmTypeScriptの次に注目を浴びているAltJSです。1
Elmは他のほとんどのAltJSとはプログラミングパラダイムが異なり、純粋な関数型プログラミングにのみ対応した言語です。2

オブジェクト指向では書けないと聞くと身構えてしまいそうですが、構文はJavaScriptと似ているのですぐ慣れると思います。3

リテラル

JavaScript Elm
3 3
3.1415 3.1415
"Hello world!" "Hello world!"
複数行文字列はほとんど非対応4 """multiline string"""
'Hello world!' 文字列にシングルクォートは使用不可
文字と文字列を区別しない 'a'
true True
[1,2,3] [1,2,3]

オブジェクト / レコード

JavaScript Elm
{ x: 3, y: 4 } { x = 3, y = 4 }
point.x point.x
point.x = 42 { point | x = 42 }

関数

JavaScript Elm
function(x, y) { return x + y; } \x y -> x + y
Math.max(3, 4) max 3 4
Math.min(1, Math.pow(2, 4)) min 1 (2^4)
numbers.map(Math.sqrt) List.map sqrt numbers
points.map(function(p) { return p.x }) List.map .x points

制御フロー

JavaScript Elm
3 > 2 ? 'cat' : 'dog' if 3 > 2 then "cat" else "dog"
var x = 42; ... let x = 42 in ...
return 42 すべてが式であり、returnは不要

文字列

JavaScript Elm
'abc' + '123' "abc" ++ "123"
'abc'.length String.length "abc"
'abc'.toUpperCase() String.toUpper "abc"
'abc' + 123 "abc" ++ toString 123

  1. フロントエンド開発者ハンドブック2017の2016年のフロントエンド開発の総括には、「少数だがそれなりの数の開発者がJavaScriptよりElmを選択しはじめている。」とまとめられています。 

  2. 他にもPureScriptや、HaskellをJavaScriptにコンパイルするツールがいくつかあります。 

  3. 私のような浅学のエンジニアは、オブジェクト指向でしか書いたことがないと思い込んでいたんですが、JavaScriptは一般的にオブジェクト指向言語にカテゴライズされているものの、関数型言語の性質も持ち合わせているマルチパラダイム言語だったので、何気なくやってることが実は関数型プログラミングだったりしました。ので、あまり恐くありませんでした。 

  4. ES6ならテンプレートリテラルで対応しています。 

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