Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
aggiunto codice mutable, spec e benchmark, ottimizzato esercizio K in
Browse files Browse the repository at this point in the history
tutte le librerie
  • Loading branch information
korsmakolnikov committed Sep 28, 2017
1 parent 9d009dd commit ec612f8
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 24 deletions.
137 changes: 137 additions & 0 deletions php/src/LambdaRoma/KataZero/KataZeroFP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace LambdaRoma\KataZero;

/**
* Class KataZeroFP
* @package LambdaRoma\KataZero
*/
class KataZeroFP
{
/**
* @return array
*/
public function kataZeroA(): array
{
return range(1, 3);
}

/**
* @return array
*/
public function kataZeroB(): array
{
return range(0, 10, 2);
}

/**
* @return array
*/
public function kataZeroC(): array
{
return range(7, 100, 7);
}

/**
* @param array $menNames
* @return array
*/
public function kataZeroD(array $menNames): array
{
return array_values(array_filter($menNames, function ($item) {
return strtolower($item[0]) === 'c';
}));
}

/**
* @return int
*/
public function kataZeroE(): int
{
$range = range(8, 100, 8);
return array_sum($range) / count($range);
}

/**
* @return int
*/
public function kataZeroF(): int
{
return array_sum(range(6, 1000, 6));
}

/**
* @param array $menNames
* @return array
*/
public function kataZeroG(array $menNames): array
{
$menNamesToSort = $menNames;
sort($menNamesToSort);
return $menNamesToSort;
}

/**
* @return int
*/
public function kataZeroH(): int
{
$range = range(41, 1000, 41);
return $range[array_rand($range)];
}

/**
* @param array $menNames
* @return string
*/
public function kataZeroI(array $menNames): string
{
return implode(', ', $menNames);
}

/**
* @param array $menNames
* @return array
*/
public function kataZeroJ(array $menNames): array
{
return array_unique($menNames);
}

/**
* @param array $womenNames
* @return array
*/
public function kataZeroK(array $womenNames): array
{
return array_reduce($womenNames, function ($carry, $item) {
$index = strlen($item);
$carry[$index] = isset($carry[$index])
? array_merge($carry[$index], [$item])
: [$item];
return $carry;
}, []);
}

/**
* @param array $womenNames
* @return array
*/
public function kataZeroL(array $womenNames): array
{
return array_map(function ($item) {
return strlen($item);
}, $womenNames);
}

/**
* @param $womenNames
* @return array
*/
public function kataZeroM($womenNames): array
{
return array_map(function ($item) {
return $item[0];
}, $womenNames);
}
}
19 changes: 18 additions & 1 deletion php/src/LambdaRoma/KataZero/KataZeroLaravelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function kataZeroC(): Collection
* @param array $menNames
* @return Collection
*/
public function kataZeroD($menNames): Collection
public function kataZeroD(array $menNames): Collection
{
return collect($menNames)->filter(function ($i) {
return strtolower($i[0]) === 'c';
Expand Down Expand Up @@ -107,6 +107,23 @@ public function kataZeroK(array $womenNames): Collection
});
}

/**
* @param array $womenNames
* @return Collection
*/
public function kataZeroK2(array $womenNames): Collection
{
$transient = collect($womenNames)->reduce(function ($carry, $item) {
$index = strlen($item);
$carry[$index] = isset($carry[$index])
? array_merge($carry[$index], [$item])
: [$item];
return $carry;
}, []);

return collect($transient);
}

/**
* @param array $womenNames
* @return Collection
Expand Down
44 changes: 21 additions & 23 deletions php/src/LambdaRoma/KataZero/KataZeroQaribu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Qaribou\Collection\ImmArray;

/**
* Class KataZeroQaribu
* @package LambdaRoma\KataZero
*/
class KataZeroQaribu
{
/**
Expand Down Expand Up @@ -31,22 +35,25 @@ public function kataZeroC(): ImmArray
}

/**
* @param $menNames
* @return ImmArray
* @param array $menNames
* @return ImmArray ImmArray
*/
public function kataZeroD($menNames): ImmArray
public function kataZeroD(array $menNames): ImmArray
{
return ImmArray::fromArray($menNames)->filter(function ($i) {
return strtolower($i[0]) === 'c';
});
}

/**
* @return int
*/
public function kataZeroE(): int
{
$range = ImmArray::fromArray(range(8, 100, 8));
$count = $range->count();
$sum = $range->reduce(function ($item, $curry) {
return $curry + $item;
$sum = $range->reduce(function ($item, $carry) {
return $carry + $item;
}, 0);
return $sum / $count;
}
Expand All @@ -57,8 +64,8 @@ public function kataZeroE(): int
public function kataZeroF(): int
{
$range = ImmArray::fromArray(range(6, 1000, 6));
return $range->reduce(function ($item, $curry) {
return $curry + $item;
return $range->reduce(function ($item, $carry) {
return $carry + $item;
}, 0);
}

Expand Down Expand Up @@ -99,27 +106,18 @@ public function kataZeroJ(array $menNames): ImmArray
}

/**
* TODO REWORK
* @param array $womenNames
* @return array
*/
public function kataZeroK(array $womenNames): array
{
$immutableWN = ImmArray::fromArray($womenNames);
$ids = $immutableWN->map(function ($i) {
return strlen($i);
});
$indexedWomenNames = ImmArray::fromArray(array_map(null, $ids->toArray(), $immutableWN->toArray()));

return $indexedWomenNames->reduce(function ($last, $curr) {
$key = array_shift($curr);
$val = array_shift($curr);
if(isset($last[$key]) && is_array($last[$key])) {
array_push($last[$key], $val);
return $last;
}
$last[$key] = [$val];
return $last;
$immutableNames = ImmArray::fromArray($womenNames);
return $immutableNames->reduce(function ($carry, $item) {
$index = strlen($item);
$carry[$index] = isset($carry[$index])
? array_merge($carry[$index], [$item])
: [$item];
return $carry;
}, []);
}

Expand Down
83 changes: 83 additions & 0 deletions php/src/benchmarks/LambdaRoma/KataZeroFPBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace benchmarks\LambdaRoma\KataZero;

use LambdaRoma\KataZero\KataZeroFP;

/**
* Class KataZeroQaribuBench
* @Groups({"functional", "mutable"})
* @BeforeMethods({"init"})
* @Revs(1000)
* @Iterations(2)
*/
class KataZeroFPBench
{
private $subject;
private $men = ["Tizio", "Caio", "Sempronio", "Mevio", "Filano", "Calpurnio"];
private $women = ["Anna", "Carla", "Angela", "Chiara", "Emma", "Maria", "Sara"];

public function init()
{
$this->subject = new KataZeroFP();
}

public function benchKataZeroA()
{
$this->subject->kataZeroA();
}

public function benchKataZeroB()
{
$this->subject->kataZeroB();
}

public function benchKataZeroC()
{
$this->subject->kataZeroC();
}

public function benchKataZeroD()
{
$this->subject->kataZeroD($this->men);
}

public function benchKataZeroE()
{
$this->subject->kataZeroE();
}

public function benchKataZeroF()
{
$this->subject->kataZeroF();
}

public function benchKataZeroG()
{
$this->subject->kataZeroG($this->men);
}

public function benchKataZeroI()
{
$this->subject->kataZeroI($this->men);
}

public function benchKataZeroJ()
{
$this->subject->kataZeroJ($this->men);
}

public function benchKataZeroK()
{
$this->subject->kataZeroK($this->women);
}

public function benchKataZeroL()
{
$this->subject->kataZeroL($this->women);
}

public function benchKataZeroM()
{
$this->subject->kataZeroM($this->women);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function benchKataZeroK()
$this->subject->kataZeroK($this->women);
}

public function benchKataZeroK2()
{
$this->subject->kataZeroK2($this->women);
}

public function benchKataZeroL()
{
$this->subject->kataZeroL($this->women);
Expand Down
Loading

0 comments on commit ec612f8

Please sign in to comment.