Upload
demuyan
View
2.062
Download
2
Embed Size (px)
Citation preview
Yet Another Development Language
でむやん ([email protected])2009/4/25 Android勉強会@金沢支部
package com.example.helloscala
import android.app.Activityimport android.os.Bundle
class HelloScala extends Activity { override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) setContentView(R.layout.main) }}
public static List<Integer> quicksort(List<Integer> data) {
if(data.size() < 1) return new ArrayList<Integer>(); Integer middle = data.get(data.size() / 2); List<Integer> left = new ArrayList<Integer>(); List<Integer> right = new ArrayList<Integer>(); for(int i = 0, n = data.size(); i < n; i++) { // pass through the pivot data: data[data.size() / 2] if(i != data.size() / 2) { if(data.get(i) <= middle) { left.add(data.get(i)); } else { right.add(data.get(i)); } } } List<Integer> result = new ArrayList<Integer>(); result.addAll(quicksort(left)); result.add(middle); result.addAll(quicksort(right)); return result; }
Java
def quicksort(a: List[Int]): List[Int] = { if( a.length < 2) a else { val pivot = a(a.length / 2) quicksort(a.filter(b => b < pivot)) ::: a.filter(b => b == pivot) ::: quicksort(a.filter(b => b > pivot)) } }
Scala
public class Person{ private String lastName; //姓 private String firstName; //名 private Person spouse; //配偶者のPersonオブジェクト public Person(String fn, String ln, Person s) { lastName = ln; firstName = fn; spouse = s; } public Person(String fn, String ln) { this(fn, ln, null); //未婚の場合,配偶者はいない } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Person getSpouse() { return spouse; } public void setSpouse(Person p) { spouse = p; } public String introduction() { return "私の名前は," + firstName + " " + lastName + (spouse != null ? " 相方の名前は," + spouse.firstName + " " + spouse.lastName + " 。" : " 。"); }}
Java
http://itpro.nikkeibp.co.jp/article/COLUMN/20080820/313059/?ST=develop&P=2 より引用
class Person(ln : String, fn : String, s : Person){ val lastName = ln; //private宣言なければ基本的にpublic val firstName = fn; //コンストラクタ引数からStringと型推論 var spouse = s; //コンストラクタ引数からPersonと型推論 def this(ln : String, fn : String) = { this(ln, fn, null); } def introduction() : String = return "私の名前は, " + firstName + " " + lastName + (if (spouse != null) " 相方の名前は, " + spouse.firstName + " " + spouse.lastName + "。" else "。");}
Scala
http://itpro.nikkeibp.co.jp/article/COLUMN/20080820/313059/?ST=develop&P=2 より引用