globals: Add pipe, find-index and unwrap-nil
This commit is contained in:
parent
7e8a897f08
commit
85bafd0ef9
1 changed files with 45 additions and 4 deletions
|
|
@ -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
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue