LoginSignup
0
2

More than 5 years have passed since last update.

PureScript by Example 4 章の終わりと 5 章の冒頭を読む

Last updated at Posted at 2016-12-12

この記事は (bouzuya) PureScript Advent Calendar 2016 の 12 日目。 (bouzuya) PureScript Advent Calendar 2016 は bouzuya の PureScript 学習の記録だ。

概要

PureScript by ExamplePureScript Language Guide を見ながら進めていく。今日は PureScript by Example の 4 章の残りと [PureScript by Example の 5 章][purescript-by-example-5]の前半を読む。

PureScript の 0.10.3 が出たことに合わせているのか、 Language Guide を含むドキュメントが Wiki から purescript/documentation リポジトリに移動されたようだ

※注意事項: 英語と日本語訳では大きな差異がある。0.10.x に対応している英語の側で進める。

PureScript by Example 4.16

PureScript by Example の 4 章のために用意された Data.Path モジュールを使って、仮想のファイルシステムで動く関数を作っていく。

[purescript-book の Data.Path][purescript-book-data-path]

> import Data.Path (root, ls, filename, size, isDirectory)
> :t root
Path

> :t ls
Path -> Array Path

> :t filename
Path -> String

> :t size
Path -> Maybe Int

> :t isDirectory
Path -> Boolean

> root
/

> isDirectory root
true

> ls root
[/bin/,/etc/,/home/]

確かに。

PureScript by Example 4.17

allFiles :: Path -> Array Path
allFiles file = file : concatMap allFiles (ls file)

allFiles' :: Path -> Array Path
allFiles' file = file : do
  child <- ls file
  allFiles' child

cons の operator alias である (:) を使っている。不変の array では cons のパフォーマンスに問題があるので、linked list か sequence を使え、とのこと。ふむ。実装的には Array.prototype.concat みたい

Pursuit:

これで 4 章は終わり。次は 5 章。

PureScript by Example 5.1

代数的データ型 (algebraic data type) とパターンマッチ (pattern matching) および行多相 (row polymorphism) を扱うらしい。

この章ではシンプルなベクターグラフィックスの操作ライブラリを書くようだ。

PureScript by Example 5.2

ソースコードは paf31/purescript-book の exercises/chapter5/

bower.jsondependencies は次のとおりだ。

"purescript-arrays": "^3.0.0"
"purescript-console": "^2.0.0"
"purescript-globals": "^2.0.0"
"purescript-math": "^2.0.0"

Pursuit:

src/Data/Picture.purs を見ると次のように import されている。

module Data.Picture where

import Prelude

import Data.Foldable (foldl)
import Global as Global
import Math as Math

as キーワードを使っている。

Language Guide:

Qualified Imports - Language Guide

これを使うと、名前の衝突を回避できる。

> import Global as Global
> :t isNaN
Error found:
in module $PSCI
at  line 1, column 1 - line 1, column 2

  Unknown value isNaN


See https://github.com/purescript/purescript/wiki/Error-Code-UnknownName for more information,
or to contribute content related to this error.


> :t Global.isNaN
Number -> Boolean

> :t Global.encodeURI
String -> String

ちなみに例が明示されていないが、組み合わせて使うこともできるようだ。

> import Global (isNaN) as Global
> :t Global.isNaN
Number -> Boolean

> :t Global.encodeURI
Error found:
in module $PSCI
at  line 1, column 1 - line 1, column 9

  Unknown value Global.encodeURI


See https://github.com/purescript/purescript/wiki/Error-Code-UnknownName for more information,
or to contribute content related to this error.

まとめ

すごく中途半端だけど、時間の都合でここで区切り。

4 章を終えた。 5 章に入ったけど、まだ代数的データ型もパターンマッチも出てきていない……。

参考

次回以降の TODO

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2