forked from Orchid/orchid
Backup commit before crunch
This commit is contained in:
20
examples/calculator/main.orc
Normal file
20
examples/calculator/main.orc
Normal file
@@ -0,0 +1,20 @@
|
||||
import std::(parse_float, to_string)
|
||||
import std::(readline, print)
|
||||
|
||||
export main := do{
|
||||
cps data = readline;
|
||||
let a = parse_float data;
|
||||
cps op = readline;
|
||||
cps print ("\"" ++ op ++ "\"\n");
|
||||
cps data = readline;
|
||||
let b = parse_float data;
|
||||
let result = (
|
||||
if op == "+" then a + b
|
||||
else if op == "-" then a - b
|
||||
else if op == "*" then a * b
|
||||
else if op == "/" then a / b
|
||||
else "Unsupported operation" -- dynamically typed shenanigans
|
||||
);
|
||||
cps print (to_string result ++ "\n");
|
||||
0
|
||||
}
|
||||
26
examples/list-processing/main.orc
Normal file
26
examples/list-processing/main.orc
Normal file
@@ -0,0 +1,26 @@
|
||||
import std::(to_string, print)
|
||||
import super::list
|
||||
import fn::*
|
||||
|
||||
export main := do{
|
||||
let foo = list::new[1, 2, 3, 4, 5, 6];
|
||||
let bar = list::map foo n => n * 2;
|
||||
let sum = bar
|
||||
|> list::skip 2
|
||||
|> list::take 3
|
||||
|> list::reduce 0 (a b) => a + b;
|
||||
cps print $ to_string sum ++ "\n";
|
||||
0
|
||||
}
|
||||
|
||||
--[
|
||||
export main := do{
|
||||
let n = 1;
|
||||
let acc = 1;
|
||||
loop r on (n acc) with (
|
||||
if n == 5
|
||||
then print acc
|
||||
else r (n + 1) (acc * 2)
|
||||
)
|
||||
}
|
||||
]--
|
||||
@@ -1,54 +0,0 @@
|
||||
import std::(parse_float, to_string)
|
||||
import std::(readline, print, debug)
|
||||
import std::(concatenate)
|
||||
import super::list
|
||||
import fn::*
|
||||
|
||||
--[ export main := do{
|
||||
cps data = readline;
|
||||
let a = parse_float data;
|
||||
cps op = readline;
|
||||
cps print ("\"" ++ op ++ "\"\n");
|
||||
cps data = readline;
|
||||
let b = parse_float data;
|
||||
let result = (
|
||||
if op == "+" then a + b
|
||||
else if op == "-" then a - b
|
||||
else if op == "*" then a * b
|
||||
else if op == "/" then a / b
|
||||
else "Unsupported operation" -- dynamically typed shenanigans
|
||||
);
|
||||
cps print (to_string result ++ "\n");
|
||||
0
|
||||
} ]--
|
||||
|
||||
export main := do{
|
||||
let foo = list::new[1, 2, 3, 4, 5, 6];
|
||||
let bar = list::map foo n => n * 2;
|
||||
let sum = bar
|
||||
|> list::skip 2
|
||||
|> list::take 3
|
||||
|> list::reduce 0 (a b) => a + b;
|
||||
cps print $ to_string sum ++ "\n";
|
||||
0
|
||||
}
|
||||
|
||||
--[
|
||||
export main := do{
|
||||
let n = 1;
|
||||
let acc = 1;
|
||||
loop r on (n acc) with (
|
||||
if n == 5
|
||||
then print acc
|
||||
else r (n + 1) (acc * 2)
|
||||
)
|
||||
}
|
||||
]--
|
||||
--[
|
||||
export main := do{
|
||||
let n = 1;
|
||||
loop r on (n) with (
|
||||
debug r
|
||||
)
|
||||
}
|
||||
]--
|
||||
15
examples/maps/fn.orc
Normal file
15
examples/maps/fn.orc
Normal 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
48
examples/maps/list.orc
Normal 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
22
examples/maps/main.orc
Normal 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
74
examples/maps/map.orc
Normal 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
9
examples/maps/option.orc
Normal 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
|
||||
Reference in New Issue
Block a user