Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oppositive semigroup #46

Open
wants to merge 4 commits into
base: constant-monoid
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
'phpdoc_align' => ['align' => 'left'],
'phpdoc_to_comment' => false,
'concat_space' => ['spacing' => 'one'],
'static_lambda' => true
'static_lambda' => true,
'single_line_empty_body' => false
])->setFinder($finder);
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic versioning](http://semver.org/).

## [1.4.0] - 2023-09-27

- introduce `OppositeSemigroup` and `OppositMonoid`
- do not flip arguments on `ListL/ConcatenationMonoid`

## [1.3.0] - 2023-08-23

- introduce `Alt` and `Plus` typeclasses
Expand Down
2 changes: 1 addition & 1 deletion bin/php-cs-fixer
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

docker run --rm -v $(pwd):/app -w=/app php:8.1.0 vendor/bin/php-cs-fixer fix src --allow-risky=yes --format=checkstyle "$@"
docker run --rm -v $(pwd):/app -w=/app php:8.1.0 vendor/bin/php-cs-fixer fix src -vvv --allow-risky=yes --format=checkstyle "$@"
2 changes: 1 addition & 1 deletion src/Instances/ListL/ConcatenationMonoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function mempty(): array
*/
public function append($a, $b)
{
return array_merge($b, $a);
return array_merge($a, $b);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is. I'll write an ADR for this.

Keeping the arguments in the same order is just more natural.

Otherwise append(['a'], ['b']) would be ['b', 'a'], while it is more expected to be ['a', 'b'].

Do you agree?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely ! Thanks for the explanation :)

}
}
46 changes: 46 additions & 0 deletions src/Instances/OppositeMonoid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Marcosh\LamPHPda\Instances;

use Marcosh\LamPHPda\Typeclass\Monoid;

/**
* @template A
*
* @implements Monoid<A>
*
* @psalm-immutable
*/
final class OppositeMonoid implements Monoid
{
/** @var Monoid<A> */
private Monoid $monoid;

/**
* @param Monoid<A> $monoid
*/
public function __construct(Monoid $monoid)
{
$this->monoid = $monoid;
}

/**
* @return A
*/
public function mempty()
{
return $this->monoid->mempty();
}

/**
* @param A $a
* @param A $b
* @return A
*/
public function append($a, $b)
{
return (new OppositeSemigroup($this->monoid))->append($a, $b);
}
}
38 changes: 38 additions & 0 deletions src/Instances/OppositeSemigroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Marcosh\LamPHPda\Instances;

use Marcosh\LamPHPda\Typeclass\Semigroup;

/**
* @template A
*
* @implements Semigroup<A>
*
* @psalm-immutable
*/
final class OppositeSemigroup implements Semigroup
{
/** @var Semigroup<A> */
private Semigroup $semigroup;

/**
* @param Semigroup<A> $semigroup
*/
public function __construct(Semigroup $semigroup)
{
$this->semigroup = $semigroup;
}

/**
* @param A $a
* @param A $b
* @return A
*/
public function append($a, $b)
{
return $this->semigroup->append($b, $a);
}
}
Loading