Skip to content

Instantly share code, notes, and snippets.

@JMLamodiere
Last active October 25, 2021 17:23
Show Gist options
  • Save JMLamodiere/f49dfb5834972bf07a6e3506fd16246c to your computer and use it in GitHub Desktop.
Save JMLamodiere/f49dfb5834972bf07a6e3506fd16246c to your computer and use it in GitHub Desktop.
SCALA vs PHP : "exists"
<?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