Last active
October 25, 2021 17:23
-
-
Save JMLamodiere/f49dfb5834972bf07a6e3506fd16246c to your computer and use it in GitHub Desktop.
SCALA vs PHP : "exists"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* | |
// SCALA : | |
case class Picture(isMain: Boolean) | |
case class Pictures(pictures: Seq[Picture]) { | |
def hasMain: Boolean = pictures.exists(_.isMain) | |
} | |
val pictures = Pictures(Seq(Picture(false), Picture(true))) | |
println(pictures.hasMain) | |
*/// PHP 8.0 : | |
class Picture | |
{ | |
public function __construct(private bool $main) {} | |
public function isMain(): bool | |
{ | |
return $this->main; | |
} | |
} | |
class Pictures | |
{ | |
/** | |
* @param Picture[] $pictures | |
*/ | |
public function __construct(private array $pictures) {} | |
public function hasMain(): bool | |
{ | |
return count(array_filter($this->pictures, fn(Picture $picture) => $picture->isMain())) >= 1; | |
} | |
} | |
$pictures = new Pictures([new Picture(false), new Picture(true)]); | |
var_dump($pictures->hasMain()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment