Commit e5f0f436 authored by Jan Hauer's avatar Jan Hauer 🌄

Initial-Code

parents
Pipeline #53791 failed with stages
.idea/
target/
*.iml
\ No newline at end of file
# Funktional Programmieren für Angeber
Konferenz: JAX 2019
Themen
* Paradigmen
* Currying
* Monaden
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.hauer.jax19</groupId>
<artifactId>function</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.3.31</kotlin.version>
<groovy.version>2.5.6</groovy.version>
<java.version>11</java.version>
<spock.version>1.3-groovy-2.5</spock.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<vavr.version>0.9.2</vavr.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- use a specific Groovy version rather than the one specified by spock-core -->
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- use a specific Groovy version rather than the one specified by spock-core -->
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/groovy</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<targetBytecode>1.8</targetBytecode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package io.hauer.jax19.function;
public class Duck {
final String name;
final Stomach stomach;
Duck(String name) {
this(name, new Stomach());
}
Duck(final String name, final Stomach stomach) {
this.name = name;
this.stomach = stomach;
}
void eat(Food... food) {
stomach.addFood(food);
}
Duck eatF(Food... food) {
return new Duck(name, new Stomach(stomach.getContent(), food));
}
@Override public String toString() {
return "Duck{" + "name='" + name + '\'' + ", stomach=" + stomach + '}';
}
}
package io.hauer.jax19.function;
import java.util.List;
import static io.hauer.jax19.function.Food.*;
public class DuckApp {
private static List<Duck> DUCKS = List.of(new Duck("Gustav"), new Duck("Daisy"), new Duck("Donald"));
private static Food[] FOOD = new Food[] { Apple, Oat };
public static void main(String[] args) {
System.out.println("FunO:");
DUCKS.stream().map(duck -> duck.eatF(Apple, Oat)).forEach(System.out::println);
System.out.println("\nFunktional:");
DUCKS.stream()
.map(duck -> new Duck(duck.name, new Stomach(duck.stomach.getContent(), Apple, Oat)))
.forEach(System.out::println);
System.out.println("\nProzedual:");
for (Duck duck : DUCKS) {
duck.stomach.addFood(Apple, Oat);
System.out.println(duck);
}
System.out.println("\nObjekt-Orientiert");
for (Duck duck : DUCKS) {
duck.eat(Apple, Oat);
System.out.println(duck);
}
}
}
package io.hauer.jax19.function;
public enum Food {
Apple, Bread, Oat
}
package io.hauer.jax19.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Stomach {
private final List<Food> content;
Stomach(){
this(new ArrayList<>());
}
Stomach(List<Food> content, Food... newFood){
this.content = content;
content.addAll(Arrays.asList(newFood));
checkContent(this.content);
}
void addFood(Food... food){
content.addAll(Arrays.asList(food));
checkContent(this.content);
}
List<Food> getContent() {
return new ArrayList<>(content);
}
private static void checkContent(List<Food> food){
if(food.contains(Food.Bread)){
throw new RuntimeException("Bad food exception");
}
}
@Override
public String toString() {
return content.toString();
}
}
package io.hauer.jax19.function
sealed class Expr
class Const(val value: Int) : Expr()
class Div(val left: Expr, val right: Expr) : Expr()
\ No newline at end of file
package io.hauer.jax19.function
val expression = Div(Const(12), Div(Const(36), Const(9)))
fun main() {
// eval(expression).printResult()
}
package io.hauer.jax19.function
fun Any.printResult () = println( "Ergebnis ist $this")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment