コード例 #1
0
ファイル: Listt.php プロジェクト: widmogrod/php-functional
 /**
  * @inheritdoc
  *
  * Example from haskell source code:
  * ``` haskell
  * traverse f = List.foldr cons_f (pure [])
  *  where cons_f x ys = (:) <$> f x <*> ys
  * ```
  */
 public function traverse(callable $transformation)
 {
     return f\foldr(function ($ys, $x) use($transformation) {
         $functor = $transformation($x);
         return $functor->map(f\append)->ap($ys ? $ys : $functor::of([]));
         // https://github.com/widmogrod/php-functional/issues/30
     }, false, $this);
 }
コード例 #2
0
 public function test_foldr()
 {
     $list = Listt::of([1, 2, 3, 4]);
     $result = f\foldr(function ($accumulator, $value) {
         return f\concatM($accumulator, Listt::of([$value + 1]));
     }, Listt::of([]), $list);
     $this->assertEquals($result, Listt::of([5, 4, 3, 2]));
 }