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

Pattern combinator #1105

Merged
merged 3 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
init
  • Loading branch information
Besok committed Nov 19, 2019
commit 9701b1f83eb2c3d48e8accd06ba39fa834dd9fd0
30 changes: 30 additions & 0 deletions combinator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: pattern
title: Combinator
folder: combinator
permalink: /patterns/combinator/
categories: Behavioral
tags:
- Functional
- Reactive
- Idiom
---

## Also known as
Composition pattern

## Intent
The functional pattern representing a style of organizing libraries centered around the idea of combining functions.
Putting it simply, there is some type T, some functions for constructing "primitive" values of type T,
and some "combinators" which can combine values of type T in various ways to build up more complex values of type T.


## Applicability
Use the combinator pattern when:
- You are able to create a more complex value from more plain values but having the same type(a combination of them)


## Credits
- [Example for java](https://gtrefs.github.io/code/combinator-pattern/)
- [Combinator pattern](https://wiki.haskell.org/Combinator_pattern)
- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic)
44 changes: 44 additions & 0 deletions combinator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0"?>
<!--

The MIT License
Copyright © 2014-2019 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
</parent>

<artifactId>combinator</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
46 changes: 46 additions & 0 deletions combinator/src/main/java/com/iluwatar/combinator/Finder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.iluwatar.combinator;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface Finder {

List<String> find(String text);


static Finder contains(String word) {
return txt -> Stream.of(txt.split("\n")).filter(line -> line.contains(word)).collect(Collectors.toList());
}

static Finder exact(String line) {
return txt -> Stream.of(txt.split("\n")).filter(in -> in.equals(line)).collect(Collectors.toList());
}

default Finder not(Finder notFinder) {
return txt -> {
List<String> res = this.find(txt);
res.removeAll(notFinder.find(txt));
return res;
};
}

default Finder or(Finder orFinder) {
return txt -> {
List<String> res = this.find(txt);
res.addAll(orFinder.find(txt));
return res;
};
}

default Finder and(Finder andFinder) {
return
txt ->
this
.find(txt)
.stream()
.flatMap(line -> andFinder.find(line).stream())
.collect(Collectors.toList());
}

}
10 changes: 10 additions & 0 deletions combinator/src/main/java/com/iluwatar/combinator/Finders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.iluwatar.combinator;

import java.util.List;

public class Finders {
private Finders() {
}

public static List<String> findOneOf
}
38 changes: 38 additions & 0 deletions combinator/src/test/java/com/iluwatar/combinator/FinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.iluwatar.combinator;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class FinderTest {

@Test
public void contains() {
String example =
"the first one \n"
+ "the second one \n"
+ "the second one \n"
+ "the thrid one \n";

List<String> result = Finder.contains("second").find(example);
System.out.println("");
}

@Test
public void exact() {
}

@Test
public void not() {
}

@Test
public void or() {
}

@Test
public void and() {
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
<module>double-buffer</module>
<module>sharding</module>
<module>game-loop</module>
<module>combinator</module>
</modules>

<repositories>
Expand Down