LoginSignup
1
1

More than 5 years have passed since last update.

Applicative でデータ変換

Last updated at Posted at 2013-03-05

フィールドを持つ、似たようなデータ構造を変換しつつ構築するとき、((->) a) の Applicative インスタンスを好んで使う。この例の計算には全く意味は無いが。

import Control.Applicative

data Point = Point {x :: Int, y :: Int} deriving Show
data Rect = Rect { p1 :: Point, p2 :: Point } deriving Show

main = print $ Rect
  <$> (Point
    <$> (2*) . y
    <*> (3*) . x
    ) . p2
  <*> (Point
    <$> x
    <*> y
    ) . p1
  $ Rect (Point 1 2) (Point 3 4)
1
1
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
1
1