@@ -3,8 +3,8 @@ module CliMonad exposing
33 , run, stepOrFail
44 , succeed, succeedWith, fail, fromResult
55 , map, map2, map3
6- , andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl
7- , errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCache
6+ , andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl, any , findMap
7+ , errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCacheIsRecursive , getOrCacheType
88 , withPath, withWarning, withExtendedWarning, withRequiredPackage
99 , todo, todoWithDefault
1010 , withFormat
@@ -18,8 +18,8 @@ module CliMonad exposing
1818@docs run, stepOrFail
1919@docs succeed, succeedWith, fail, fromResult
2020@docs map, map2, map3
21- @docs andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl
22- @docs errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCache
21+ @docs andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl, any, findMap
22+ @docs errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCacheIsRecursive, getOrCacheType
2323@docs withPath, withWarning, withExtendedWarning, withRequiredPackage
2424@docs todo, todoWithDefault
2525@docs withFormat
@@ -99,7 +99,9 @@ type CliMonad a
9999
100100
101101type alias Cache =
102- FastDict . Dict String Common . Type
102+ { typeCache : FastDict . Dict String Common . Type
103+ , isRecursiveCache : FastDict . Dict String ( Maybe Common . UnsafeName )
104+ }
103105
104106
105107type alias Output =
@@ -200,26 +202,54 @@ run oneOfDeclarations input (CliMonad x) =
200202
201203emptyCache : Cache
202204emptyCache =
203- FastDict . empty
205+ { typeCache = FastDict . empty
206+ , isRecursiveCache = FastDict . empty
207+ }
208+
209+
210+ getOrCacheType : Common .RefTo Common .Schema -> (() -> CliMonad Common .Type ) -> CliMonad Common .Type
211+ getOrCacheType ref compute =
212+ let
213+ ( Common . UnsafeName key) =
214+ Common . refToString ref
215+ in
216+ CliMonad
217+ ( \ input cache ->
218+ case FastDict . get key cache. typeCache of
219+ Nothing ->
220+ let
221+ ( CliMonad inner) =
222+ compute ()
223+ in
224+ case inner input cache of
225+ Ok ( computed, output, cache2 ) ->
226+ Ok ( computed, output, { cache2 | typeCache = FastDict . insert key computed cache2. typeCache } )
227+
228+ Err e ->
229+ Err e
230+
231+ Just found ->
232+ Ok ( found, emptyOutput, cache )
233+ )
204234
205235
206- getOrCache : Common .RefTo Common .Schema -> (() -> CliMonad Common .Type ) -> CliMonad Common .Type
207- getOrCache ref compute =
236+ getOrCacheIsRecursive : Common .RefTo Common .Schema -> (() -> CliMonad ( Maybe Common .UnsafeName )) -> CliMonad ( Maybe Common .UnsafeName )
237+ getOrCacheIsRecursive ref compute =
208238 let
209239 ( Common . UnsafeName key) =
210240 Common . refToString ref
211241 in
212242 CliMonad
213243 ( \ input cache ->
214- case FastDict . get key cache of
244+ case FastDict . get key cache. isRecursiveCache of
215245 Nothing ->
216246 let
217247 ( CliMonad inner) =
218248 compute ()
219249 in
220250 case inner input cache of
221251 Ok ( computed, output, cache2 ) ->
222- Ok ( computed, output, FastDict . insert key computed cache2 )
252+ Ok ( computed, output, { cache2 | isRecursiveCache = FastDict . insert key computed cache2. isRecursiveCache } )
223253
224254 Err e ->
225255 Err e
@@ -443,6 +473,49 @@ map4 f (CliMonad x) (CliMonad y) (CliMonad z) (CliMonad w) =
443473 )
444474
445475
476+ any : (a -> CliMonad Bool ) -> List a -> CliMonad Bool
477+ any f xs =
478+ CliMonad
479+ ( \ input cache ->
480+ Result . Extra . foldlWhileOk
481+ ( \ x ( a, o, c ) ->
482+ if a then
483+ Ok ( a, o, c )
484+
485+ else
486+ let
487+ ( CliMonad i) =
488+ f x
489+ in
490+ i input c
491+ )
492+ ( False , emptyOutput, cache )
493+ xs
494+ )
495+
496+
497+ findMap : (a -> CliMonad (Maybe b )) -> List a -> CliMonad (Maybe b )
498+ findMap f xs =
499+ CliMonad
500+ ( \ input cache ->
501+ Result . Extra . foldlWhileOk
502+ ( \ x ( a, o, c ) ->
503+ case a of
504+ Just _ ->
505+ Ok ( a, o, c )
506+
507+ Nothing ->
508+ let
509+ ( CliMonad i) =
510+ f x
511+ in
512+ i input c
513+ )
514+ ( Nothing , emptyOutput, cache )
515+ xs
516+ )
517+
518+
446519andThen : (a -> CliMonad b ) -> CliMonad a -> CliMonad b
447520andThen f ( CliMonad x) =
448521 CliMonad
0 commit comments