summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2022-10-08 14:47:21 -0400
committerBen Culkin <scorpress@gmail.com>2022-10-08 14:47:21 -0400
commit0f958b08b3446a866418aa485bb60c208d952033 (patch)
tree2a54b64036bacb2f1fbc60d17511eb904b15fe7d /src/main/java/bjc/data
parent08885862bee89602d7edc55144ea9b6af780bfa4 (diff)
Add a whole bunch of Optics
This adds a whole bunch of types/functions related to optics. With this batch, I've mainly gone for the concrete representation types, instead of var Laarhoven or profunctor representations. The concrete ones require less infrastructure, though they are not as easy to compose There's also a number of misc. things in here
Diffstat (limited to 'src/main/java/bjc/data')
-rw-r--r--src/main/java/bjc/data/CollectionUtils.java54
-rw-r--r--src/main/java/bjc/data/Holder.java4
2 files changed, 57 insertions, 1 deletions
diff --git a/src/main/java/bjc/data/CollectionUtils.java b/src/main/java/bjc/data/CollectionUtils.java
new file mode 100644
index 0000000..08ef711
--- /dev/null
+++ b/src/main/java/bjc/data/CollectionUtils.java
@@ -0,0 +1,54 @@
+/*
+ * esodata - data structures of varying utility
+ * Copyright 2022, Ben Culkin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package bjc.data;
+
+import java.util.*;
+import java.util.function.Function;
+
+/**
+ * Various utilities for dealing w/ collections
+ * @author bjcul
+ *
+ */
+public class CollectionUtils {
+ /**
+ * Create a function that converts a collection into a map.
+ *
+ * @param <Val> The type of the values in the map
+ * @param <Idx> The type of the keys in the map
+ * @param <Ent> The type of the values in the collection
+ *
+ * @param keyFunc The function that determines keys
+ * @param valFunc The function that determines values
+ *
+ * @return A function which uses the provided function to create a map from a
+ * collection.
+ */
+ public static <Val, Idx, Ent> Function<Collection<Ent>, Map<Idx, Val>> indexBy(Function<Ent, Idx> keyFunc,
+ Function<Ent, Val> valFunc) {
+ return (coll) -> {
+ Map<Idx, Val> mep = new HashMap<>();
+
+ for (Ent ent : coll) {
+ mep.put(keyFunc.apply(ent), valFunc.apply(ent));
+ }
+
+ return mep;
+ };
+ }
+}
diff --git a/src/main/java/bjc/data/Holder.java b/src/main/java/bjc/data/Holder.java
index 4a1de75..57a5d6c 100644
--- a/src/main/java/bjc/data/Holder.java
+++ b/src/main/java/bjc/data/Holder.java
@@ -26,6 +26,7 @@ import bjc.data.internals.WrappedLazy;
import bjc.data.internals.WrappedOption;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.theory.Functor;
+import bjc.typeclasses.Container;
/**
* A holder of a single value.
@@ -35,7 +36,8 @@ import bjc.funcdata.theory.Functor;
* @param <ContainedType>
* The type of value held.
*/
-public interface Holder<ContainedType> extends Functor<ContainedType> {
+public interface Holder<ContainedType> extends Functor<ContainedType>, Container<ContainedType, Holder<ContainedType>> {
+ // note: to really work, this should also take the binding parameter
/**
* Bind a function across the value in this container.
*