From 85bafd0ef9b36a7f807651e73131d0a41bb43373 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 6 Nov 2023 23:08:09 +0100 Subject: [PATCH] globals: Add pipe, find-index and unwrap-nil --- src/globals.apfl | 49 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/globals.apfl b/src/globals.apfl index 5cdc1c8..28c1c79 100644 --- a/src/globals.apfl +++ b/src/globals.apfl @@ -205,7 +205,7 @@ if result { result } { orelse ~fs } } - find-first := { l f -> + ifind-first := { f l -> out := nil i := 0 n := len l @@ -214,7 +214,7 @@ Some:x -> out = Some::x nil -> - } (f l@i) + } (f i l@i) i = ++ i } @@ -222,12 +222,36 @@ out } + find-first := { f l -> + ifind-first { _ x -> f x } l + } + + find-index := { pred l -> + ifind-first { i x -> + if (pred x) { Some::i } { nil } + } + } + unwrap-some := { Some:x then _ -> then x x _ else -> else x x then -> unwrap-some x then identity } + unwrap-nil := { + nil _ else -> (else) + x then _ -> then x + x then -> unwrap-nil x then {nil} + } + + pipe := { + val -> val + val [f ~args] ~fs -> + pipe (f ~args val) ~fs + val f ~fs -> + pipe (f val) ~fs + } + has-key := { k container -> { Some: _ -> true @@ -235,6 +259,13 @@ } (get-optional k container) } + get-or-default := { k container default -> + { + Some:x -> x + nil -> def + } (get-optional k container) + } + run-file := { f -> ((load-file f)) } @@ -273,11 +304,11 @@ } import := { name -> - maybe-mod := find-first searchers { s -> + maybe-mod := find-first { s -> unwrap-some (s name) { loader -> loader name } - } + } searchers { Some:mod -> @@ -310,6 +341,12 @@ [(f x) ~(map f xs)] } + foldl := { + f [x ~xs] -> foldl f x xs + _ carry [] -> carry + f carry [x ~xs] -> foldl f (f x carry) xs + } + splice := { a off -> splice a off nil a off len -> splice a off len [] @@ -386,10 +423,14 @@ 'andalso -> andalso 'orelse -> orelse 'find-first -> find-first + 'find-index -> find-index 'unwrap-some -> unwrap-some + 'unwrap-nil -> unwrap-nil 'import -> modules.import 'map -> map 'splice -> splice 'slice -> slice + 'pipe -> pipe + 'foldl -> foldl ] }