From a0850f8d07b589ca5c8ce842da49e8d2b89014c6 Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Fri, 3 May 2024 14:38:46 -0400 Subject: [PATCH] Define unrolled_findonly --- Project.toml | 2 +- src/UnrolledUtilities.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 62f5f0a..864c564 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "UnrolledUtilities" uuid = "0fe1646c-419e-43be-ac14-22321958931b" authors = ["CliMA Contributors "] -version = "0.1.2" +version = "0.1.3" [compat] julia = "1.10" diff --git a/src/UnrolledUtilities.jl b/src/UnrolledUtilities.jl index dc69559..8772989 100644 --- a/src/UnrolledUtilities.jl +++ b/src/UnrolledUtilities.jl @@ -11,6 +11,7 @@ export unrolled_any, unrolled_in, unrolled_unique, unrolled_filter, + unrolled_findonly, unrolled_split, unrolled_flatten, unrolled_flatmap, @@ -104,6 +105,12 @@ struct NoInit end ntuple(i -> itr[N + i], Val(length(itr) - N)) # When its second argument is a Val, ntuple is unrolled via Base.@ntuple. +function unrolled_findonly(f::F, values) where {F} + filtered_values = unrolled_filter(f, values) + return length(filtered_values) == 1 ? filtered_values[1] : + error("unrolled_findonly requires that exactly 1 value makes f true") +end + @static if hasfield(Method, :recursion_relation) # Remove recursion limits for functions whose arguments are also functions. for func in ( @@ -115,6 +122,7 @@ struct NoInit end unrolled_reduce, unrolled_mapreduce, unrolled_filter, + unrolled_findonly, unrolled_split, unrolled_flatmap, unrolled_applyat,