git.fiddlerwoaroof.com
Browse code

(init)

Ed Langley authored on 13/12/2019 18:40:13
Showing 12 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,11 @@
1
+/main/target
2
+/target
3
+*.iml
4
+/lib
5
+.idea
6
+.classpath
7
+.project
8
+.settings
9
+*~
10
+/lib
11
+/main/lib
0 12
new file mode 100644
... ...
@@ -0,0 +1,98 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <parent>
6
+        <artifactId>java_lenses</artifactId>
7
+        <groupId>groupId</groupId>
8
+        <version>1.0-SNAPSHOT</version>
9
+    </parent>
10
+    <modelVersion>4.0.0</modelVersion>
11
+
12
+    <artifactId>main</artifactId>
13
+
14
+    <properties>
15
+        <kotlin.version>1.3.21</kotlin.version>
16
+    </properties>
17
+
18
+    <dependencies>
19
+        <dependency>
20
+            <groupId>groupId</groupId>
21
+            <artifactId>lensify_processor</artifactId>
22
+            <version>1.0-SNAPSHOT</version>
23
+        </dependency>
24
+        <dependency>
25
+            <groupId>org.jetbrains.kotlin</groupId>
26
+            <artifactId>kotlin-stdlib-jdk8</artifactId>
27
+            <version>${kotlin.version}</version>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>org.jetbrains.kotlin</groupId>
31
+            <artifactId>kotlin-test</artifactId>
32
+            <version>${kotlin.version}</version>
33
+            <scope>test</scope>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>org.clojure</groupId>
37
+            <artifactId>clojure</artifactId>
38
+            <version>1.10.0</version>
39
+            <scope>test</scope>
40
+        </dependency>
41
+    </dependencies>
42
+
43
+    <build>
44
+        <plugins>
45
+            <plugin>
46
+                <groupId>org.jetbrains.kotlin</groupId>
47
+                <artifactId>kotlin-maven-plugin</artifactId>
48
+                <version>${kotlin.version}</version>
49
+                <executions>
50
+                    <execution>
51
+                        <id>compile</id>
52
+                        <phase>compile</phase>
53
+                        <goals>
54
+                            <goal>compile</goal>
55
+                        </goals>
56
+                    </execution>
57
+                    <execution>
58
+                        <id>test-compile</id>
59
+                        <phase>test-compile</phase>
60
+                        <goals>
61
+                            <goal>test-compile</goal>
62
+                        </goals>
63
+                    </execution>
64
+                </executions>
65
+                <configuration>
66
+                    <jvmTarget>1.8</jvmTarget>
67
+                </configuration>
68
+            </plugin>
69
+            <plugin>
70
+                <groupId>org.apache.maven.plugins</groupId>
71
+                <artifactId>maven-compiler-plugin</artifactId>
72
+                <version>3.7.0</version>
73
+                <executions>
74
+                    <execution>
75
+                        <id>compile</id>
76
+                        <phase>compile</phase>
77
+                        <goals>
78
+                            <goal>compile</goal>
79
+                        </goals>
80
+                    </execution>
81
+                    <execution>
82
+                        <id>testCompile</id>
83
+                        <phase>test-compile</phase>
84
+                        <goals>
85
+                            <goal>testCompile</goal>
86
+                        </goals>
87
+                    </execution>
88
+                </executions>
89
+                <configuration>
90
+                    <source>1.8</source>
91
+                    <target>1.8</target>
92
+                    <encoding>UTF-8</encoding>
93
+                    <generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory>
94
+                </configuration>
95
+            </plugin>
96
+        </plugins>
97
+    </build>
98
+</project>
0 99
\ No newline at end of file
1 100
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+package com.company.main;
2
+
3
+import clojure.lang.ARef;
4
+
5
+public abstract class AIdLens extends ARef {
6
+
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,54 @@
1
+package com.company.main;
2
+
3
+import clojure.lang.IDeref;
4
+import clojure.lang.Settable;
5
+
6
+import java.util.function.Function;
7
+
8
+public class Bound<S, T, A, B> implements IDeref, Settable {
9
+    private final Lens<S, T, A, B> lens;
10
+    private final A rec;
11
+
12
+    public Bound(Lens<S, T, A, B> lens, A rec) {
13
+        this.lens = lens;
14
+        this.rec = rec;
15
+    }
16
+
17
+    public T get() {
18
+        return lens.get(rec);
19
+    }
20
+
21
+    public B set(S v) {
22
+        return lens.set(rec, v);
23
+    }
24
+
25
+    public B over(Function<T, S> fun) {
26
+        return lens.over(rec, fun);
27
+    }
28
+
29
+
30
+    public <W, X> Bound<W, X, A, B> compose(Lens<W, X, T, S> b) {
31
+        return new Bound<>(lens.compose(b), rec);
32
+    }
33
+
34
+    @Override
35
+    public T deref() {
36
+        return this.get();
37
+    }
38
+
39
+    @Override
40
+    public B doSet(Object o) {
41
+        if (o != null) {
42
+            return this.set((S)o);
43
+        }
44
+        return null;
45
+    }
46
+
47
+    @Override
48
+    public B doReset(Object o) {
49
+        if (o != null) {
50
+            return this.set((S)o);
51
+        }
52
+        return null;
53
+    }
54
+}
0 55
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+package com.company.main;
2
+
3
+public class IdBound<S, A> extends Bound<S, S, A, A> {
4
+    public IdBound(Lens<S, S, A, A> lens, A rec) {
5
+        super(lens, rec);
6
+    }
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,5 @@
1
+package com.company.main;
2
+
3
+public interface IdLens<S,A> extends Lens<S,S,A,A> {
4
+}
5
+
0 6
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+package com.company.main
2
+
3
+import com.company.main.Main.Person.*
4
+import java.util.concurrent.atomic.AtomicReference
5
+
6
+fun main() {
7
+    val x = Main.Person("Bob", "Johnson")
8
+    val y = Main.Person("Alice", "Johnson")
9
+    val xy = Main.Relationship(x, y, "spouse")
10
+
11
+    val xyYouName = xy.you.._name
12
+
13
+    println("name: ${x.name()}")
14
+    println("status: ${x.status()}")
15
+    println("youLens name: ${xyYouName()}")
16
+
17
+    println("youLens name: ${xyYouName()}")
18
+
19
+    val personRef = AtomicReference(x)
20
+
21
+    println("ref name: ${personRef.get().name()}")
22
+    personRef.updateAndGet { t ->
23
+        t.status.set("feelin'g good")
24
+    }
25
+    println("ref name: ${personRef.get().name()}")
26
+}
0 27
\ No newline at end of file
1 28
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+package com.company.main;
2
+
3
+import java.util.concurrent.atomic.AtomicReference;
4
+import java.util.function.Function;
5
+
6
+public interface Lens<S, T, A, B> extends java.util.function.BiFunction<A, Function<T,S>, B> {
7
+    B over(A rec, Function<T, S> fun);
8
+
9
+    @Override
10
+    default B apply(A a, Function<T, S> tsFunction) {
11
+        return over(a, tsFunction);
12
+    }
13
+
14
+    default B set(A rec, S value) {
15
+        return over(rec, Constant.ly(value));
16
+    }
17
+
18
+    default T get(A rec) {
19
+        final AtomicReference<T> store = new AtomicReference<>();
20
+        over(rec, (T v) -> {
21
+            store.set(v);
22
+            return null;
23
+        });
24
+        return store.get();
25
+    }
26
+
27
+    default <W, X> Lens<W, X, A, B> compose(Lens<W, X, T, S> b) {
28
+        return Lens.staticCompose(this, b);
29
+    }
30
+
31
+    static <S, T, A, B, U, V> Lens<S, T, A, B> staticCompose(Lens<V, U, A, B> a, Lens<S, T, U, V> b) {
32
+        return (rec, fun) -> a.set(rec, b.set(a.get(rec), fun.apply(b.get(a.get(rec)))));
33
+    }
34
+}
35
+
36
+
37
+class Constant<T, R> implements Function<T, R> {
38
+    private R v;
39
+
40
+    private Constant(R v) {
41
+        this.v = v;
42
+    }
43
+
44
+    @Override
45
+    public R apply(T o) {
46
+        return v;
47
+    }
48
+
49
+    static <T, R> Constant<T, R> ly(R v) {
50
+        return new Constant<>(v);
51
+    }
52
+}
53
+
0 54
new file mode 100644
... ...
@@ -0,0 +1,13 @@
1
+package com.company.main
2
+
3
+operator fun <S,T,A,B> Lens<S,T,A,B>.invoke(r: A): T {
4
+    return this.get(r)
5
+}
6
+
7
+operator fun <S,T,A,B> Bound<S,T,A,B>.invoke(): T {
8
+    return this.get()
9
+}
10
+
11
+operator fun <W,X,S,T,A,B> Bound<S,T,A,B>.rangeTo(other: Lens<W,X,T,S>): Bound<W,X,A,B> {
12
+    return this.compose<W, X>(other)
13
+}
0 14
new file mode 100644
... ...
@@ -0,0 +1,74 @@
1
+package com.company.main;
2
+
3
+import java.util.function.Function;
4
+
5
+public class Main {
6
+
7
+    public static class Person {
8
+        private String name_;
9
+        public static final IdLens<String, Person> _name = (rec, fun) ->
10
+                new Person(fun.apply(rec.name_), rec.status_);
11
+        public final IdBound<String, Person> name;
12
+
13
+
14
+        private String status_;
15
+        public static final IdLens<String, Person> statusLens = (rec, fun) ->
16
+                new Person(rec.name_, fun.apply(rec.status_));
17
+        public final IdBound<String, Person> status;
18
+
19
+        public Person(String name, String status) {
20
+            name_ = name;
21
+            status_ = status;
22
+            this.name = new IdBound<>(_name, this);
23
+            this.status = new IdBound<>(statusLens, this);
24
+        }
25
+
26
+        @Override
27
+        public String toString() {
28
+            return "Person{" +
29
+                    "name_='" + name_ + '\'' +
30
+                    ", status_='" + status_ + '\'' +
31
+                    '}';
32
+        }
33
+    }
34
+
35
+    public static class Relationship {
36
+        private Person me_;
37
+        public static final IdLens<Person, Relationship> meLens = (Relationship rec, Function<Person, Person> xform) ->
38
+                new Relationship(xform.apply(rec.me_), rec.you_, rec.description_);
39
+        public final IdBound<Person, Relationship> me = new IdBound<>(meLens, this);
40
+
41
+        private Person you_;
42
+        public static final IdLens<Person, Relationship> youLens = (Relationship rec, Function<Person, Person> xform) ->
43
+                new Relationship(rec.me_, xform.apply(rec.you_), rec.description_);
44
+        public final IdBound<Person, Relationship> you = new IdBound<>(youLens, this);
45
+
46
+        private String description_;
47
+        public static final IdLens<String, Relationship> descriptionLens = (Relationship rec, Function<String, String> xform) ->
48
+                new Relationship(rec.me_, rec.you_, xform.apply(rec.description_));
49
+        public final IdBound<String, Relationship> description = new IdBound<>(descriptionLens, this);
50
+
51
+        public Relationship(Person me, Person you, String description) {
52
+            me_ = me;
53
+            you_ = you;
54
+            description_ = description;
55
+        }
56
+
57
+        @Override
58
+        public String toString() {
59
+            return "Relationship{" +
60
+                    "me_=" + me_ +
61
+                    ", you_=" + you_ +
62
+                    ", description_='" + description_ + '\'' +
63
+                    '}';
64
+        }
65
+    }
66
+
67
+    public static void main(String[] args) {
68
+      Person a = new Person("name", "hi");
69
+
70
+      Person renamed_person = a.name.set("new name");
71
+      renamed_person.name.get();
72
+    }
73
+
74
+}
0 75
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+(ns com.company.main.lens-test
2
+  (:import (com.company.main Lens Main$Relationship)
3
+           (clojure.lang IDeref)))
4
+
5
+(defrecord CljLens [field]
6
+  Lens
7
+  (over [_ value fun]
8
+    (update value field #(fun %))))
0 9
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>groupId</groupId>
8
+    <artifactId>java_lenses</artifactId>
9
+    <packaging>pom</packaging>
10
+    <version>1.0-SNAPSHOT</version>
11
+    <modules>
12
+        <module>lensify_processor</module>
13
+        <module>main</module>
14
+    </modules>
15
+
16
+    <dependencies>
17
+        <dependency>
18
+            <groupId>org.projectlombok</groupId>
19
+            <artifactId>lombok</artifactId>
20
+            <version>1.18.6</version>
21
+            <scope>provided</scope>
22
+        </dependency>
23
+
24
+    </dependencies>
25
+
26
+    <build>
27
+        <plugins>
28
+            <plugin>
29
+                <groupId>org.apache.maven.plugins</groupId>
30
+                <artifactId>maven-compiler-plugin</artifactId>
31
+                <version>3.7.0</version>
32
+                <configuration>
33
+                    <source>1.8</source>
34
+                    <target>1.8</target>
35
+                    <encoding>UTF-8</encoding>
36
+                    <generatedSourcesDirectory>
37
+                        ${project.build.directory}/generated-sources/
38
+                    </generatedSourcesDirectory>
39
+                </configuration>
40
+            </plugin>
41
+        </plugins>
42
+    </build>
43
+</project>
0 44
\ No newline at end of file