Backup commit before crunch

This commit is contained in:
2023-05-16 18:32:25 +01:00
parent 33413b2b0f
commit 126494c63f
59 changed files with 847 additions and 236 deletions

15
examples/maps/fn.orc Normal file
View File

@@ -0,0 +1,15 @@
export Y := \f.(\x.f (x x))(\x.f (x x))
export loop $r on (...$parameters) with ...$tail =0x5p512=> Y (\$r.
bind_names (...$parameters) (...$tail)
) ...$parameters
-- bind each of the names in the first argument as a parameter for the second argument
bind_names ($name ..$rest) $payload =0x2p1000=> \$name. bind_names (..$rest) $payload
bind_names () (...$payload) =0x1p1000=> ...$payload
export ...$prefix $ ...$suffix:1 =0x1p130=> ...$prefix (...$suffix)
export ...$prefix |> $fn ..$suffix:1 =0x2p130=> $fn (...$prefix) ..$suffix
export (...$argv) => ...$body =0x2p512=> (bind_names (...$argv) (...$body))
$name => ...$body =0x1p512=> (\$name. ...$body)

48
examples/maps/list.orc Normal file
View File

@@ -0,0 +1,48 @@
import option
import super::fn::*
pair := \a.\b. \f. f a b
-- Constructors
export cons := \hd.\tl. option::some (pair hd tl)
export end := option::none
export pop := \list.\default.\f.list default \cons.cons f
-- Operators
export reduce := \list.\acc.\f. (
loop r on (list acc) with
pop list acc \head.\tail. r tail (f acc head)
)
export map := \list.\f. (
loop r on (list) with
pop list end \head.\tail. cons (f head) (r tail)
)
export skip := \list.\n. (
loop r on (list n) with
if n == 0 then list
else pop list end \head.\tail. r tail (n - 1)
)
export take := \list.\n. (
loop r on (list n) with
if n == 0 then end
else pop list end \head.\tail. cons head $ r tail $ n - 1
)
export get := \list.\n. (
loop r on (list n) with
pop list option::none \head.\tail.
if n == 0 then option::some head
else r tail (n - 1)
)
new[...$item, ...$rest:1] =0x2p333=> (cons (...$item) new[...$rest])
new[...$end] =0x1p333=> (cons (...$end) end)
new[] =0x1p333=> end
export ::(new)

22
examples/maps/main.orc Normal file
View File

@@ -0,0 +1,22 @@
import list
import map
import option
import fn::*
export main := do{
let foo = map::new[
"foo" = 1,
"bar" = 2,
"baz" = 3,
"bar" = 4
];
map::get foo "bar"
|> option::unwrap
}
--[
export main := do{
let foo = list::new[1, 2, 3];
map::fst foo
}
]--

74
examples/maps/map.orc Normal file
View File

@@ -0,0 +1,74 @@
import list
import option
import fn::*
import std::to_string
import std::debug
-- utilities for using lists as pairs
export fst := \l. (
list::get l 0
(panic "nonempty expected")
\x.x
)
export snd := \l. (
list::get l 1
(panic "2 elements expected")
\x.x
)
export print_pair := \l. (
to_string (fst l) ++ " = " ++ to_string (snd l)
)
-- constructors
export empty := list::end
export add := \m.\k.\v. (
list::cons
list::new[k, v]
m
)
-- queries
-- return the last occurrence of a key if exists
export get := \m.\k. (
loop r on (m) with
list::pop m option::none \head.\tail.
if fst head == k
then option::some $ snd head
else r tail
)
-- commands
-- remove one occurrence of a key
export del := \m.\k. (
loop r on (m) with
list::pop m list::end \head.\tail.
if fst head == k then tail
else list::cons head $ r tail
)
-- remove all occurrences of a key
export clear := \m.\k. (
loop r on (m) with
list::pop m list::end \head.\tail.
if (fst head) == k then r tail
else list::cons head $ r tail
)
-- replace at most one occurrence of a key
export set := \m.\k.\v. (
m
|> del k
|> add k v
)
new[...$tail:2, ...$key = ...$value:1] =0x2p333=> (
set new[...$tail] (...$key) (...$value)
)
new[...$key = ...$value:1] =0x1p333=> (add empty (...$key) (...$value))
new[] =0x1p333=> empty
export ::(new)

9
examples/maps/option.orc Normal file
View File

@@ -0,0 +1,9 @@
import std::panic
export some := \v. \d.\f. f v
export none := \d.\f. d
export map := \option.\f. option none f
export flatten := \option. option none \opt. opt
export flatmap := \option.\f. option none \opt. map opt f
export unwrap := \option. option (panic "value expected") \x.x