Haskellã®åå¼·ããã¦ããããElixirã®ãã¤ãã©ã¤ã³æ¼ç®åãF#ç±æ¥ã§ãããã¨ãç¥ã£ã
Elixirã¨ããããã°ã©ãã³ã°è¨èªã«ããã¤ãã©ã¤ã³ãã¯ãæ¼ç®åã¨ããã®ããããé¢ç½ããªã¼ã¨æã£ã¦ããããåãElixirã¨ãç¥ããªãã ããããããããã©ããªãã®ãå¾ã§æ¸ãã¨ãã¦ãHaskellã®åå¼·ããã¦ãããåçã®ãã®ãå®è£
ãã¦ããè¨äºãã¿ã¤ããã©ããããã¯F#ç±æ¥ã§ãããã¨ãç¥ã£ãã
ãã¦ããã ããªãä¾ã ãã©ãæ°ã®ãªã¹ãã«ä½ã(map)ãã¦æ¡ä»¶ãæºãããã®ãæãåºãã¦(filter)ãæå¾ã«è¶³ã(fold)ã¿ãããªãã¨ã£ã¦ãHaskellã§ã¯æ®éãã¨ãã°æ¬¡ã®ããã«æ¸ãï¼ã¨æãï¼
Prelude> sum . filter even . map (+1) $ [1..10] 30
Haskellãªãã¦ç¥ããªã人ãããã¨æãã®ã§ãã»ã¼åçã®ã³ã¼ããPerlã§æ¸ãã¨ãããªã
use strict; use warnings; use List::Util qw/sum/; my $ans = sum grep { $_ % 2 == 0 } map { $_ + 1 } (1 .. 10); print( $ans );
ç´°ããªææ³ã®éãã¯ããããã£ã¦ããã¨ã¯åãã ã
Rubyã®äººãªãããæ¸ããããããããªã
irb> (1..10).map{|x| x + 1 }.select{|n| n.even? }.reduce(0){|a, b| a + b } => 30
ã©ã¡ããããã¨ãããã¨ã¯ãªãã®ã ãã©ãè¡ãå¦çã®å
容ã«ãã£ã¦ã¯Rubyã®ãããªé çªã§æ¸ããã»ãããããããããã¨ããããï¼Unixã®ã³ãã³ãããã¤ãã©ã¤ã³ã§ã¤ãªãã§ãããã¨ãæãåºãã¦ã»ããï¼
F#ã«ã¯ãã¤ãã©ã¤ã³æ¼ç®å |> ã¨ããããããããã¨ãå®ç¾ããæ¼ç®åãããããããHaskellã§ã¯ãããå®ç¾ããæ¼ç®åï¼ã¨ããããã ã®é¢æ°ï¼ãç°¡åã«æ¸ããã¨ãã§ãã次ã®ããã«æ¸ã
-- |pipeline operator -- >>> sum . filter even . map (+1) $ [1..10] -- 30 -- -- >>> [1 .. 10] |> map (+1) |> filter even |> sum -- 30 infixl 0 |> (|>) :: a -> (a -> b) -> b x |> f = f x
Elixirã®ãã¤ãã©ã¤ã³ã¯æã¯ãã¯ãã§å®è£ ããã¦ãã¦ååã /> ã ã£ãããæè¿ã®ãªãªã¼ã¹(0.7.2)ã§çµã¿è¾¼ã¿ã®æ¼ç®åã«ãªã£ã¦ååã |> ã«ãªã£ãããã ã
iex(4)> [1,2,3,4] |> Enum.map(&1 + 2) |> Enum.filter(fn(x) -> rem(x, 2) == 0 end) |> List.foldl(0, &1 + &2) 10
ãªãElixirã®é¢æ°ã¯Haskellã¿ããã«ã«ãªã¼åããã¦ãããã¯ããªãã®ã§ãæ®éã¯ãããªãã®ä½ããªããçµå±çµã¿è¾¼ã¿æ¼ç®åã«ãªã£ãããããããããã®ãã¦ã¼ã¶ãä½ããã¨ããããã¯ãï¼Exlirã®ãã¯ãã¯æ½è±¡æ§ææ¨ãæä½ããæ¬ç©ã®ãã¯ãã ï¼ã§ã使ããããªãã¨æããã¨ãããã¨ã¯æ¬ç©ã®ãã¯ãããªãè¨èªã ã¨å¾ããããããã®ãä½ãã®ã¯é£ããã
ãã¦ãããã ãã 㨠id:kazu-yamamoto ããã®è¨äºãèªãã°ããã£ã¦ãã話ã§ã¤ã¾ããªãã®ã§ãF#ã®é¢æ°åææ¼ç®å >> ã¨åçã®ãã®ãHaskellã§ä½ã£ã¦ã¿ãããããä½ã£ã¦ã¿ãã¨ãã£ã¦ããããç³ç°¡åãªã®ã ãã
Haskellã«ã¯ãã¡ããé¢æ°åææ¼ç®å . ããããä¸ã§ã使ã£ã¦ããããF#ã®ããã¨ã¯éé ã§ããã |> ã§å¤ã渡ãã¦ããã®ã¯æ¥½ããããå¼æ°ãæåã«æ¸¡ããªããã°ãããªãã®ã§ãçµæã¨ãã¦é¢æ°ã欲ããæã¯ã©ã ãå¼ã«ãããããªãã
Prelude> (\s -> s |> map (+1) |> filter even |> sum) [1 .. 10] 30
ã§ããã¶ãHaskellã ã¨ããã®ãããªé¢æ°ãä½ãåºããããã¨ãããã ãããéé ã®é¢æ°åææ¼ç®å |>> ã¯ä»¥ä¸ã
-- |pipelined compose operator -- >>> (map (+1) |>> filter even |>> sum |>> show |>> putStr) [1 .. 10] -- 30 -- -- >>> :m + Data.List -- >>> ( words |>> sort |>> group |>> map (\ls -> (head ls, length ls)) ) "ab a b a a b" -- [("a",3),("ab",1),("b",2)] infixl 0 |>> (|>>) :: (a -> b) -> (b -> c) -> a -> c g |>> f = f . g
ããã½ã¼ã¹ã³ã¼ãã¯githubã«ããããdoctestå½¢å¼ã®ä½¿ç¨ä¾å
¼åä½ãã¹ããããã