開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の3章(型を定義し、関数を単純化する)、3.13(ガードの条件節の評価)、練習問題 11.をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
練習問題 11.
コード
sample.dart
import 'dart:html';
void main(){
run.onClick.listen((MouseEvent){
pre.text = window.navigator.userAgent + '\n';
for(var points in [[a, b, c, d, e], [a, b, c, d, new Point(3, 1)], [a, b]]){
points.forEach((Point p) => pre.text += '(${p.x}, ${p.y}) ');
pre.text += '\n';
pre.text += '${getDirections(points)}\n';
}
});
clear.onClick.listen((MouseEvent event) => pre.text = '');
}
ButtonElement run = querySelector('#run_dart');
ButtonElement clear = querySelector('#clear');
PreElement pre = querySelector('#pre0');
var a = new Point(0, 0);
var b = new Point(1, 0);
var c = new Point(1, 1);
var d = new Point(2, 1);
var e = new Point(2, 0);
List<Direction> getDirections(List<Point> points){
if(points.length < 3){
return [];
}
var result = [];
result.add(new Direction(points[0], points[1], points[2]));
result.addAll(getDirections(points.sublist(1)));
return result;
}
class Point{
num x;
num y;
Point(this.x, this.y);
}
class Direction{
var direction;
Direction(Point a, Point b, Point c){
var cow = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
direction = cow < 0 ?
'Clockwise' : cow > 0 ?
'Counterclockwise' : 'Collinear';
}
String toString(){
return direction;
}
}
0 コメント:
コメントを投稿