A profile is defined for a user named lagénorhynque who is interested in programming languages, linguistics, law, politics, and mathematics. They are fluent in several languages including French and Russian. The document also discusses forming effective teams by focusing on communication, clear responsibilities, flexibility, addressing issues promptly, and continual learning and improvement.
This document defines a macro called my_macro in Clojure that takes a predicate and list of arguments and conditionally executes the arguments if the predicate returns true. It demonstrates calling my_macro with (= 1 2) which returns false and doesn't print "hello", but with (= 1 1) which returns true and prints "hello". It also shows compiling my_macro into a function that can be called directly later.
This document provides information about various languages including their linguistic classification, typology, word order, writing systems, and examples sentences. It compares the structures of English, French, German, Russian, Arabic, Chinese, and Turkish by noting their language family, typical word order, and how a similar sentence requesting a glass of white wine would be expressed in each.
7. クラス・メソッド/関数・変数の定義
/* Scala */
scala> case class Person(
| val name: String,
| val birthDate: LocalDate,
| ):
| def age(now: LocalDate): Int = ChronoUnit.YEARS
.between(birthDate, now).toInt
// defined case class Person
scala> val 🐬 = Person("lagénorhynque", LocalDate.of(1990, 1,
18))
val 🐬: Person = Person(lagénorhynque,1990-01-18)
scala> 🐬.name
val res0: String = lagénorhynque
scala> 🐬.age(LocalDate.now)
val res1: Int = 35
7
8. 基本的な構文は酷似している
Scala 3では も導入された
Scalaでは0引数メソッド呼び出しで括弧が省略可能
/* Kotlin */
>>> data class Person(
... val name: String,
... val birthDate: LocalDate,
... ) {
... fun age(now: LocalDate): Int = ChronoUnit.YEARS
.between(birthDate, now).toInt()
... }
>>> val `🐬` = Person("lagénorhynque", LocalDate.of(1990, 1,
18))
>>> `🐬`.name
res4: kotlin.String = lagénorhynque
>>> `🐬`.age(LocalDate.now())
res5: kotlin.Int = 35
オフサイドルール
8
9. 関数リテラル(ラムダ式)と高階関数
丸括弧や矢印、destructuring (分配束縛)の差異に
よく戸惑う
/* Scala */
scala> (1 to 10). // REPLでのメソッドチェーンのため . が末尾にある
| filter(_ % 2 != 0).
| map(x => x * x).
| foldLeft(0)((acc, x) => acc + x)
val res0: Int = 165
/* Kotlin */
>>> (1..10).
... filter { it % 2 != 0 }.
... map { x -> x * x }.
... fold(0) { acc, x -> acc + x }
res0: kotlin.Int = 165
9
10. メソッドの関数としての利用
Scalaではメソッド名そのままで関数オブジェクト
として参照できる(ref. )
/* Scala */
scala> def factorial(n: Long): Long = (1L to n).product
def factorial(n: Long): Long
scala> (0L to 9L).map(factorial)
val res0: IndexedSeq[Long] = Vector(1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880)
/* Kotlin */
>>> fun factorial(n: Long): Long = (1L..n).fold(1L) { acc, x
-> acc * x}
>>> (0L..9L).map(::factorial)
res1: kotlin.collections.List<kotlin.Long> = [1, 1, 2, 6, 24,
120, 720, 5040, 40320, 362880]
eta-expansion
10
14. for式(a.k.a. for内包表記)
モナド(的な構造)を簡潔に扱うためのシンタックス
シュガーとして非常に便利
に相当するもの
scala> for // Range (Seq)型の場合
| x <- 1 to 3
| y <- 4 to 5
| yield x * y // => 1*4, 1*5, 2*4, 2*5, 3*4, 3*5
val res0: IndexedSeq[Int] = Vector(4, 5, 8, 10, 12, 15)
scala> for // Option型(Kotlinではnullable typeで表すもの)の場合
| x <- Some(2)
| y <- Some(3)
| yield x * y // => Some(2*3)
val res1: Option[Int] = Some(6)
scala> for
| x <- Some(2)
| y <- None: Option[Int]
| yield x * y // => 全体としてNoneに
val res2: Option[Int] = None
Haskellのdo記法 14
15. パターンマッチング(主にmatch式)
構造に基づいて場合分けしながら分解できる
代数的データ型を扱う上で常にセットでほしい存在
コンパイラによる網羅性チェックも行われる
scala> enum Tree[+A]: // 書籍FP in Scalaのサンプルコードより引用
| case Leaf(value: A)
| case Branch(left: Tree[A], right: Tree[A])
|
| def depth: Int = this match
| case Leaf(_) => 0
| case Branch(l, r) => 1 + (l.depth.max(r.depth))
// defined class Tree
scala> import Tree._
scala> Branch(Leaf("a"), Branch(Branch(Leaf("b"), Leaf("c"))
, Leaf("d"))).depth
val res0: Int = 3
15