01-03
Haskellで何か書きたくなったので、上記のサイトの問題を解いていくことにした。
00. 文字列の逆順
文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
Prelude> Data.List.reverse "stressed" "desserts"
02. 「パトカー」+「タクシー」=「パタトクカシーー」
「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
Prelude> putStrLn $ concat $ zipWith (flip (\x -> flip (:) [x])) "パトカー" "タクシー" パタトクカシーー
文字と文字を連結して文字列にする関数が見つからず、変な関数を作成してしまった。また、putStrLnを使わないと、コードが表示される。
03. 円周率
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
Prelude> map length $ words "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechani cs." [3,1,4,1,6,9,2,7,5,3,5,8,9,7,10]
wordsという関数は久しぶりに使った。
04で詰まったのこれで終わりかも。
Java Se 8 実践プログラミングを読んで記憶に残った3点
Javaプログラマーなら習得しておきたい Java SE 8 実践プログラミング
- 作者: Cay S. Horstmann,柴田芳樹
- 出版社/メーカー: インプレス
- 発売日: 2014/09/22
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (8件) を見る
分かりにくい仕様だと感じたけれど、使ってみたい。
2. 実質的final
局所クラスでは、外側のスコープのfinal変数を参照することができたが、
それが緩和されて、final宣言されていない実質的にfinalな変数を参照できるようになった。
3. インターフェイスにstaticメソッドの定義
インターフェイスにstaticメソッドが定義できるようになった。
クラス名 + sや+ Utilをつけたクラスを定義してきたが、
これからはインターフェイスを作る。
インターフェイスにstaticメソッドを作るのが分かりやすいか別だが、
classに定義するよりは良い。
UTF-8で開き直す方法
たまに文字コードの判定に失敗するファイルに出会って、
このコマンドを忘れて調べるのでブログに記載
:e ++enc=utf-8
ユーザ認証があるプロキシのHTTPクライアント
Javaでプロキシを使うHTTPクライアントについて下記が参考になる。
JavaのHTTP通信でプロキシを使う
ユーザの認証がプロキシにある場合は、下記の通り。
Authenticated HTTP proxy with Java - Stack Overflow
Apache Commons HTTP Componentsではユーザ指定ができる。
HttpClient - HttpClient Authentication Guide
HTTP クライアント GETの例
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class SampleGetHttpClient { public static void main(String[] args) throws IOException { String url = "http://localhost:8080/"; HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); // HTTPSの場合はHttpsURLConnectionにキャストすれば良いらしい connection.setRequestMethod("GET"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } System.out.println(builder); } } }
キーと値の設定(ファイル)の読み書き
キーと値だけの設定(ファイル)であれば、
java.uti.Propertiesクラスで簡単に作成できる。
XMLでも入出力できるようになっている。
日本語を含む場合、XMLで出力しておけば、エディタで開いても値がわかる。
import java.io.IOException; import java.util.Properties; public class SamplePropertiesIO { public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.setProperty("key", "value"); properties.store(System.out, "Settings"); // 読み込む場合は、loadを呼ぶ // Properties p2 = new Properties(); // p2.load(stream); } }
実行結果。
#Settings #Sun Mar 15 21:02:42 JST 2015 key=value
HTTP サーバの例(POST)
HTTP サーバ。POSTの例。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class PostHttpServer { public static void main(String[] args) throws IOException { int port = 8080; HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); server.createContext("/", new HttpHandler() { @Override public void handle(HttpExchange arg0) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(arg0.getRequestBody(), StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } String s = builder.toString(); arg0.sendResponseHeaders(200, s.length()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(arg0.getResponseBody(), StandardCharsets.UTF_8)); writer.write(s); writer.flush(); } }); server.start(); System.out.println("hit any key"); System.in.read(); server.stop(0); } }