Ada genericによるswap

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
   generic
     type element is private;
   procedure Swap(a, b: in out element);

   procedure Swap(a, b: in out element) is
     tmp: element;
   begin
     tmp := a;
     a := b;
     b := tmp;
   end Swap;

   procedure SwapInt is new Swap(Integer);
   procedure SwapChar is new Swap(Character);

   i1: Integer := 3;
   i2: Integer := 5;
   c1: Character := 'a';
   c2: Character := 'd';
begin
  put(i1);
  new_line;
  put(i2);
  new_line;
  put(c1);
  new_line;
  put(c2);
  new_line;
  SwapInt(i1, i2);
  SwapChar(c1, c2);
  put(i1);
  new_line;
  put(i2);
  new_line;
  put(c1);
  new_line;
  put(c2);
  new_line;
end MyApp;
[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ./myapp
          3
          5
a
d
          5
          3
d
a

Ada procedure

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
  procedure SayHi(name: in String) is
  begin
   put_line("hi!" & name);
  end SayHi;
begin
   SayHi("tom");
   SayHi("bob");
end MyApp;
[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ./myapp
hi!tom
hi!bob

Adaのloopとexit

Adaのloopは永久ループなので、exitで処理を抜けます。

-- Float
--変数
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
   count: integer;
   i: integer := 1;
begin
 get(count);
 loop
   put(i);
   new_line;
   i := i + 1;
   exit when i > count;
 end loop;
end MyApp;
[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ./myapp
4
          1
          2
          3
          4

Adaの条件分岐

-- Float
--変数
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
 score: integer;
begin
 get(score);
 if score > 80 then
    put_line("great!");
 elsif score > 60 then
 put_line("good!");
 else
 put_line("...");
    end if;
end MyApp;
[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ./myapp
25
...

戦闘機開発 Adaの定数と変数、四則演算

--定数
--変数
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
 x: Integer;
 y: constant Integer := 3;
begin
  x := 5;
  put(x);
  new_line;
  x := 10;
  put(x, 5);
  new_line;
  put(y);
  new_line;
end MyApp;

四則演算

--定数
--変数
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure MyApp is
 x: Integer;
begin
 put("your number? ");
 get(x);

 put(x + 3);
 new_line;
 put(x - 3);
 new_line;
 put(x / 3);
 new_line;
 put(x rem 3);
 new_line;
 put(x * 3);
 new_line;
 put(x ** 3);
 new_line;
end MyApp;

25を入力してみます。

[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ./myapp
your number? 25
         28
         22
          8
          1
         75
      15625

戦闘機開発 Adaで”helloworld”

Adaの拡張子は、adbです。myapp.adbファイルをつくり、早速コードを書いてみましょう。

with Ada.Text_IO;
use Ada.Text_IO;

procedure MyApp is
begin
  put_line("hello world!");
end MyApp;
[vagrant@localhost ada_lessons]$ gnatmake myapp.adb
gcc -c myapp.adb
gnatbind -x myapp.ali
gnatlink myapp.ali
[vagrant@localhost ada_lessons]$ ls
myapp  myapp.adb  myapp.ali  myapp.o
[vagrant@localhost ada_lessons]$ ./myapp
hello world!

戦闘機開発 vagrantでAdaの開発環境

Adaとは米国防総省が中心となって開発されているプログラミング言語です。戦闘機の制御などに使われています。

Ada

Adaはコンパイルしなければなりませんので、開発環境にGNATをインストールしましょう。コマンドは以下の通りです。GNATはGCCに含まれるAdaのコンパイラです。

[vagrant@localhost ada_lessons]$ sudo yum install -y fedora-gnat-project-common

実際にコンパイルする際には gnat makeとしますが、インストールが終わったら、バージョンを確認してみましょう。

[vagrant@localhost ada_lessons]$ gnat make --version
GNATMAKE 4.4.7 20120313 (Red Hat 4.4.7-17)
Copyright (C) 1995-2008, Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

さあ、ミサイル開発のスタートです。

Angular.jsによるTwitter検索

var mainCtr = function($scope, $http){
 $scope.doSearch = function(){
 	var uri = "http://search.twitter.com/search.json?q="
 	+ $scope.query
 	+ '&callback=JSON_CALLBACK';
 	$http.jsonp(uri).success(function(data){
 		console.dir(data.results);
 		$scope.results = data.results;
 	});
 }

}
<!DOCTYPE html>
<html lang="ja" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Angularの練習</title>
    <script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>
    <script src="myscript.js"></script>
</head>
<body>
    <h1>Twitter Search</h1>
    <div ng-controller="mainCtrl">
    <form ng-submit="doSearch()" name="myForm">
    <input type="text" ng-model="query" required>
    <input type="submit" value="search" ng-disabled="myForm.$invalid">
    </form>
    <h2>Result</h2>
    <ul>
    <ul ng-show="results.length">
    <li ng-repeat="result in results">
    <img ng-src="{{result.profile_image_url}}">
    <a ng-href="http://twitter.com/{{result.from_user}}/status">{{result.text}}</a>
    ({{result.from_user}})
    </li>
    </ul>
    <p ng-hide="results.length">↑から検索してください</p>
    </div>
</body>
</html>

Angular.jsによるToDoList

Angular.jsによるToDoListをつくります。挙動はブラウザで確認ください。

var mainCtrl = function($scope, $http){
	$scope.tasks = [
	{"body":"do this 1", "done":false},
	{"body":"do this 2", "done":false},
	{"body":"do this 3", "done":true},
	{"body":"do this 4", "done":false},
	];
	$scope.addNew = function(){
		$scope.tasks.push({"body":$scope.newTaskBody,"done":false});
		$scope.newTaskBody = '';
	}
	$scope.getDoneCount = function(){
		var count = 0;
		angular.forEach($scope.tasks, function(task){
			count += task.done ? 1 : 0;
		});
		return count;
	}
	$scope.deleteDone = function(){
		var oldTasks = $scope.tasks;
		$scope.tasks = [];
		angular.forEach(oldTasks, function(task){
			if (!task.done) $scope.tasks.push(task);
		})
	}
}

index.html

<!DOCTYPE html>
<html lang="ja" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Angularの練習</title>
    <script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>
    <script src="myscript.js"></script>
    <style>
        .done-true{
            color:gray;
            text-decoration:line-through;
        }
    </style>
</head>
<body>
    <h1>Todo list</h1>
    <div ng-controller="mainCtrl">
    <p>Finished Task: {{getDoneCount()}} / {{tasks.length}}
    <button ng-click="deleteDone()">Delete finished</button>
    </p>
    <ul>
        <li ng-repeat="task in tasks">
        <input type="checkbox" ng-model="task.done">
        <span class="done-{{task.done}}">{{task.body}}<span>
        <a href ng-click="tasks.splice($index,1)">[x]</a>
        </li>
    </ul>
    <form ng-submit="addNew()">
    <input type="text" ng-model="newTaskBody">
    <input type="submit" value="add">
    </form>
    </div>
</body>
</html>

%e7%84%a1%e9%a1%8c