From 0f958b08b3446a866418aa485bb60c208d952033 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 8 Oct 2022 14:47:21 -0400 Subject: 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 --- src/main/java/bjc/data/CollectionUtils.java | 54 +++++++++++++++++++++++++++++ src/main/java/bjc/data/Holder.java | 4 ++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/bjc/data/CollectionUtils.java (limited to 'src/main/java/bjc/data') 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 . + */ +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 The type of the values in the map + * @param The type of the keys in the map + * @param 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 Function, Map> indexBy(Function keyFunc, + Function valFunc) { + return (coll) -> { + Map 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 * The type of value held. */ -public interface Holder extends Functor { +public interface Holder extends Functor, Container> { + // note: to really work, this should also take the binding parameter /** * Bind a function across the value in this container. * -- cgit v1.2.3